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

时间:2017-01-21 03:08:26

标签: java ant

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

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

我尝试过使用The Swift Programming Language (Swift 3.0.1)中的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.

1 个答案:

答案 0 :(得分:1)

关于你的具体问题:

  

似乎让MyBin和MyLib属性混淆了?我不知道怎么做   但要防止这种情况。

您正在使用它来调用MyLib版本:

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

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

相反,您可以使用,例如:

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

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

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

  

用作新Ant项目的basedir的目录(除非   useNativeBasedir设置为true)。默认为当前项目   basedir,除非inheritall设置为false,在这种情况下它   没有默认值。这将覆盖basedir设置   所谓的项目。也可以作为解析目录   antfile和output属性的值(如果有的话)。