如何使用一个构建文件在Ant中构建多个项目?

Mri*_*lla 16 java ant

我可以从一个构建文件构建多个项目.例:

<project basedir="." default="all" name="app1">
    ...
</project>

<project basedir="." default="all" name="app2">
    ...
</project>
Run Code Online (Sandbox Code Playgroud)

目前我输入ant -f build1.xml compile并构建我的应用程序,我必须使用两个单独的构建文件.有没有办法让它运行的方式,我有两个项目定义了一个公共的构建文件,我可以键入类似ant app1 compileant 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>
Run Code Online (Sandbox Code Playgroud)

谢谢.

Bor*_*rja 17

是的,您可以创建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>
Run Code Online (Sandbox Code Playgroud)

通过这种方式,您可以使用来自一个唯一文件的两个ant文件.

如果您将app1和app2目标替换为所需的目标,则可以在一个文件中完成所有操作,以便在单独的文件中编译它们.

编辑:

您可以使用不同的方法将它们都包含在一个文件中,也许最简单的方法是为每个目标上的每个项目包含一个后缀.您可以调用特定的项目目标或两者的目标.

我给你一个编译目标的例子(对于init目标也是如此).

您可以使用compile编译两个项目(我将其他项目称为其他项目),并使用compileazeboster编译azeboster项目.

您可以搜索常见的东西以避免不必要的重复代码(常见路径,目标等)

<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>
Run Code Online (Sandbox Code Playgroud)

  • <ant>标记不支持buildfile属性,而是使用antfile属性. (2认同)