假设我有一个库和二进制目标,我们称之为MyLib
和MyBin
,
MyBin
取决于MyLib
。
我正在尝试为MyBin
创建一个首先构建MyLib
的Ant构建文件,然后在构建MyBin
时将其包含在类路径中。
我尝试过使用The Swift Programming Language (Swift 3.0.1)中的Ant任务。
但是,它不起作用,而ant -v
我认为MyBin
build-deps
目标甚至不构建MyLib
。好像它让MyBin
和MyLib
属性混淆了?我不知道如何防止这种情况。
我只在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.
答案 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属性的值(如果有的话)。