【问题标题】:How can I automate compiling a large Java project?如何自动编译大型 Java 项目?
【发布时间】:2010-10-26 14:22:08
【问题描述】:

我正在为我的雇主开发一个自动化项目。我们的源代码的每个修订版都有一个池。当你下载一个修订版时,你需要创建一个包含一堆第三方包含的目录结构来最终构建项目。我已经自动化了整个过程,直到让我的脚本 (.bat) 编译每个特定的可运行 Java 应用程序。这个项目有很多应用程序,目录列表如下所示:

Proj Name
   -variousincludesfolder1
   -variousincludesfolder2
   -variousincludesfolder3
   -variousincludesfolder4
   -runnableapplicationsandmoreincludes
       -con.java

现在,我想做一个 con.java 的自动编译,但我不知道从哪里开始。人们建议我尝试使用 Ant,但我使用 Eclipse 获得的任何自动 Ant 文件生成似乎仅足以在存在活动项目文件的情况下构建 con.java。有没有办法在不使用 eclipse 的情况下自动执行此操作,以至于让批处理文件自己生成一个 .jar?

【问题讨论】:

    标签: eclipse compiler-construction ant jar javac


    【解决方案1】:

    这绝对是Ant 的工作。不要依赖 Eclipse 生成的 Ant 文件;通读manual 并自己写一个。 (您可能会发现 Ant 也做了您在构建脚本中没有想到的事情。)

    更具体地说,here is the documentation for the jar task

    【讨论】:

    • 但是 Ant 是否可以用来自动查找和编译所有可能需要的关联类,但是这些关联经常会发生变化,并且通常很难找到需要的关联类?谢谢。
    • 我不太清楚你的意思,所以我会用另一个问题反驳:你现在是怎么做的?
    • 嗯,好吧,基本上在我编译任何东西或使用 eclipse 之前,我有一个包含 java 文件的目录结构。其中一些可以编译成 Java 应用程序。但是由于项目的大小,一个 Java 应用程序可能会相互依赖数百甚至 1000 多个其他源文件。所以到目前为止,我将我的目录结构导入到一个新的 Eclipse 项目中。然后我编译我的应用程序(con.java),它负责编译所有其他类文件。但我想从命令行完成这一切。
    • 听起来你最好模块化你的源代码。但我会在 Ant 的手册中看到我能挖掘到的内容(不幸的是,我不是 Ant 专家)。
    • 谢谢,非常感谢!我也会这样做。
    【解决方案2】:

    您可以定义通配符和模式匹配以在构建中包含/排除各种文件和文件夹。查看Ant manual,了解filesets 之类的内容如何使用包含和排除过滤器。

    另外,请阅读tutorial

    这是一个简单的构建文件,它可以编译所有 java 文件并引用所有 jar。放在顶层目录:

    <?xml version="1.0" encoding="UTF-8"?>
    <?xml-stylesheet type="text/xsl" 
        href="http://www.ibm.com/developerworks/xml/library/x-antxsl/examples/example2/ant2html.xsl"?>
    <project name="Proj Name" default="build" basedir=".">
        <property name="src.dir" value="${basedir}" description="base folder where the source files will be found.  Typically under /src, but could be anywhere.  Defaulting to root directory of the project" />
        <property name="build.dir" value="build" description="Where to put build files, separate from src and resource files." />
    
        <path id="master-classpath">
            <fileset dir="${basedir}" description="looks for any jar file under the root directory">
                <include name="**/*.jar" />
            </fileset>
        </path>
    
        <target name="build" description="Compile all JAVA files in the project">
            <javac srcdir="${src.dir}" 
                destdir="${build.dir}/classes" 
                debug="true" 
                deprecation="true" 
                verbose="false" 
                optimize="false"  
                failonerror="true">
                <!--master-classpath is defined above to include any jar files in the project subdirectories(can  be customized to include/exclude)-->
                <classpath refid="master-classpath"/>
                <!--If you want to define a pattern of files/folders to exclude from compilation...-->
                <exclude name="**/realm/**"/>
            </javac>  
        </target>
    
    </project>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-07
      • 1970-01-01
      • 2016-03-05
      • 2022-09-26
      相关资源
      最近更新 更多