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



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

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

我尝试使用 Ant 任务,就像使用 Ant 构建其他依赖项目一样。

但是,它不起作用,从ant -v来看,我认为MyBinbuild-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.

关于您的具体问题:

似乎混淆了MyBin和MyLib属性?我不确定如何 不过,为了防止这种情况。

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

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

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

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

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

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

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

用作新 Ant 项目的 basedir 的目录(除非 useNativeBasedir 设置为 true)。默认为当前项目的 basedir,除非 inheritall 已设置为 false,在这种情况下,它 没有默认值。这将覆盖 basedir 设置 被调用的项目。还用作解析 Antfile 和输出属性的值(如果有)。

相关内容

  • 没有找到相关文章

最新更新