使用 Ant 构建当前构建目标的依赖项

Using Ant to build dependencies of the current build target

假设我有一个库和一个二进制目标,我们称它们为 MyLibMyBinMyBin 取决于 MyLib.

我正在尝试为 MyBin 创建一个 Ant 构建文件,它首先构建 MyLib,然后在构建 MyBin 时将其包含在类路径中。

我试过使用 Building other, dependent projects with Ant 中的 Ant 任务。

然而,它没有工作,从 ant -v 我认为 MyBin build-deps 目标甚至没有构建 MyLib。似乎混淆了 MyBinMyLib 属性?不过我不确定如何防止这种情况。

我只在下面转储 MyBin/build.xml,但是 MyLib 几乎相同,只是它没有 build-deps 目标。

<project name="MyBin" default="main" basedir=".">
    <property name="projectName" value="MyBin" />
    <property name="src.dir" location="src" />
    <property name="build.dir" location="bin" />
    <property name="dist.dir" location="dist" />
    <property name="dist.lib.dir" location="dist/lib" />
    <property name="lib.dir" value="lib" />


<target name="build-deps" depends="init">
<!-- MyLib main target does clean -> build -> jar to dist folder -->
<!-- Its build.xml uses many of the same property values as above -->
        <ant antfile="../MyLib/build.xml" target="main"/>
</target>

<path id="classpath">
    <fileset dir="${basedir}/">
        <include name="../MyLib/dist/**/*.jar" />
    </fileset>
</path>

<!-- Need classpath to run this -->
<target name="compile" depends="build-deps" description="compile the source ">
    <javac includeantruntime="false" srcdir="${src.dir}"
                   destdir="${build.dir}" classpathref="classpath" />
</target>

<!-- Group all dependencies into a big dependency-all.jar -->
<target name="copy-dependencies">

    <mkdir dir="${dist.lib.dir}" />

    <jar jarfile="${dist.lib.dir}/dependencies-all.jar">
        <zipgroupfileset dir="${lib.dir}">
            <include name="**/*.jar" />
        </zipgroupfileset>
    </jar>

</target>

<!-- jar it, extract above dependency-all.jar and zip it with project files -->
<target name="jar" depends="compile, copy-dependencies"
            description="package, output to JAR">

    <mkdir dir="${dist.dir}" />
    <mkdir dir="${dist.lib.dir}" />

    <jar jarfile="${dist.dir}/${projectName}.jar" basedir="${build.dir}">
        <manifest>
            <attribute name="Main-Class" value="${main-class}" />
        </manifest>
        <zipfileset src="${dist.lib.dir}/dependencies-all.jar"
                            excludes="META-INF/*.SF" />
    </jar>

</target>

<target name="clean" description="clean up">
    <delete dir="${build.dir}" />
    <delete dir="${dist.dir}" />
</target>

<!-- Default, run this -->
<target name="main" depends="clean, compile, jar" />
</project>

我在 ant -v in MyBin 中看到的是:

build-deps:
Project base dir set to: /MyBin
      [ant] calling target(s) [main] in build file /MyLib/build.xml
parsing buildfile /MyLib/build.xml with URI = file:/MyLib/build.xml
Project base dir set to: /MyBin
Override ignored for property "projectName"
Override ignored for property "build.dir"
Override ignored for property "dist.dir"
Override ignored for property "dist.lib.dir"
Override ignored for property "lib.dir"
[pathconvert] Set property classpath.name = 
      [ant] Entering /MyLib/build.xml...
Build sequence for target(s) `main' is [clean, init, copy-dependencies, jar, main]
Complete build sequence is [clean, init, copy-dependencies, jar, main, ]

clean:
   [delete] Deleting directory /MyBin/bin
   [delete] Deleting directory /MyBin/bin

init:
    [mkdir] Created dir: /MyBin/bin

copy-dependencies:
      [ant] Exiting /MyLib/build.xml.

关于您的具体问题:

seems like it's confusing MyBin and MyLib properties? I'm not sure how to prevent this though.

您正在使用它来调用 MyLib 构建:

<ant antfile="../MyLib/build.xml" target="main" />

默认情况下,通过 <ant> 任务调用的构建会继承调用者的所有属性,包括 basedir,因此构建 运行 在错误的位置。

您可以改为使用,例如:

<ant dir="../MyLib" />

即运行build.xml在指定目录下,设置basedir属性,调用默认目标,如果是main正如您所说,您正在为库使用非常相似的构建文件。如果您不想在执行 MyLib 任务时从 MyBin 继承属性,请在任务中指定 inheritAll=false

来自 <ant> 属性的 dir 任务文档:

the directory to use as a basedir for the new Ant project (unless useNativeBasedir is set to true). Defaults to the current project's basedir, unless inheritall has been set to false, in which case it doesn't have a default value. This will override the basedir setting of the called project. Also serves as the directory to resolve the antfile and output attribute's values (if any).