我可以从一个构建文件构建多个项目吗?示例:
<project basedir="." default="all" name="app1">
...
</project>
<project basedir="." default="all" name="app2">
...
</project>
目前我输入 ant -f build1.xml compile
并构建我的应用程序,我必须使用两个单独的构建文件。有什么方法可以让它以我让两个项目定义一个公共(public)构建文件的方式运行,我可以输入类似 ant app1 compile
或 ant app2 compile
?
这是我的构建文件的样子:
<?xml version="1.0" encoding="UTF-8"?>
<project name="azebooster" default="dist" basedir=".">
<!-- Globals -->
<property name="src" location="src/com/aelitis"/>
<property name="build" location="build/azebooster"/>
<property name="jar" location="jar/azebooster"/>
<property name="resources" location="res/azebooster"/>
<!-- Paths -->
<path id="classpath">
<fileset dir="." includes="**/*.jar"/>
</path>
<!-- Start it -->
<target name="init">
<tstamp/>
<mkdir dir="${build}"/>
<mkdir dir="${jar}"/>
</target>
<!-- Build it -->
<target name="compile" depends="init" description="compile the source" >
<javac srcdir="${src}" destdir="${build}">
<classpath>
<path refid="classpath"/>
</classpath>
</javac>
</target>
<!-- Jar it -->
<target name="jar" depends="compile">
<jar destfile="${jar}/${ant.project.name}.jar">
<fileset dir="${build}"/>
<fileset dir="${resources}" />
</jar>
</target>
<!-- Clean it -->
<target name="clean" description="clean up" >
<tstamp/>
<delete dir="${build}"/>
<delete dir="${jar}"/>
</target>
</project>
谢谢。
最佳答案
是的,你可以创建一个 default.build 文件(这样你就不需要指定文件,因为它是默认使用的)。您可以在其上创建以下目标:
<target name="all" depends="app1, app2" />
<target name="app1">
<ant antfile=<app1file> target="compile" />
</target>
<target name="app2">
<ant antfile=<app2file> target="compile" />
</target>
通过这种方式,您可以使用一个唯一文件中的两个 ant 文件。
如果您将 app1 和 app2 目标替换为编译它们所需的目标(您在单独的文件中拥有这些目标),那么您可以只在一个文件中完成所有操作。
编辑:
您可以通过不同的方式将它们都包含在一个文件中,也许最简单的方法是为每个目标上的每个项目包含一个后缀。您可以调用特定的项目目标或两者的目标。
我给你举了一个编译目标的例子(还有 init 目标)。
您可以使用 compile 来编译这两个项目(我将另一个项目称为 other),并使用 compileazeboster 来编译 azeboster 项目。
您可以搜索公共(public)事物以避免不必要的重复代码(公共(public)路径、目标等)
<property name="srcazebooster" location="src/com/aelitis"/>
<property name="buildazebooster" location="build/azebooster"/>
<property name="jarazebooster" location="jar/azebooster"/>
<property name="srcother" location="src/other"/>
<property name="buildother" location="build/other"/>
<property name="jarother" location="jar/other"/>
<!-- Start it -->
<target name="init" depends="initazebooster, initother"/>
<!-- Build it -->
<target name="compile" depends="compileazebooster, compileother" description="compile the source for all" />
<!-- Start azebooster-->
<target name="initazebooster">
<tstamp/>
<mkdir dir="${buildazebooster}"/>
<mkdir dir="${jarazebooster}"/>
</target>
<!-- Build azeboster-->
<target name="compileazebooster" depends="initazebooster" description="compile the source for azebooster" >
<javac srcdir="${srcazebooster}" destdir="${buildazebooster}">
<classpath>
<path refid="classpath"/>
</classpath>
</javac>
</target>
<!-- Start other-->
<target name="initother">
<tstamp/>
<mkdir dir="${buildotherr}"/>
<mkdir dir="${jarother}"/>
</target>
<!-- Build other-->
<target name="compileother" depends="initother" description="compile the source for other" >
<javac srcdir="${srcother}" destdir="${buildother}">
<classpath>
<path refid="classpath"/>
</classpath>
</javac>
</target>
关于java - 如何使用一个构建文件在 Ant 中构建多个项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4733923/