简体   繁体   English

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

[英]Using Ant to build dependencies of the current build target

Say I have a library and a binary target, let's call them MyLib and MyBin , MyBin depends on MyLib . 假设我有一个库和二进制目标,我们称之为MyLibMyBinMyBin依赖于MyLib

I'm trying to create an Ant buildfile for MyBin that first builds MyLib and then includes it in the classpath when building MyBin . 我正在尝试为MyBin创建一个Ant构建文件,它首先构建MyLib ,然后在构建MyBin时将其包含在类路径中。

I've tried using Ant tasks as in Building other, dependent projects with Ant . 我已经尝试过使用Ant任务,就像使用Ant 构建其他依赖项目一样

However, it's not working, and from ant -v I think the MyBin build-deps target is not even building MyLib . 但是,它没有用,而且从ant -v我认为MyBin build-deps目标甚至没有构建MyLib Seems like it's confusing MyBin and MyLib properties? 好像它混淆了MyBinMyLib属性? I'm not sure how to prevent this though. 我不知道如何防止这种情况。

I'm dumping only MyBin/build.xml below, but the MyLib is almost identical, except it does not have the build-deps target. 我只在下面转储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>

What I see with ant -v in MyBin is something along the lines of: ant -v in MyBin看到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.

On your specific question: 关于你的具体问题:

seems like it's confusing MyBin and MyLib properties? 看起来它混淆了MyBin和MyLib属性? I'm not sure how to prevent this though. 我不知道如何防止这种情况。

You are using this to invoke the MyLib build: 您正在使用它来调用MyLib构建:

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

A build invoked via the <ant> task this way by default inherits all properties from the caller, including basedir , hence the build is run in the wrong place. 默认情况下,通过<ant>任务调用的构建会从调用方继承所有属性,包括basedir ,因此构建在错误的位置运行。

Instead you could use, for example: 相反,您可以使用,例如:

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

That will run build.xml in the specified directory, set the basedir property, and call the default target, which should be main if you are using a very similar buildfile for the library as you say. 这将在指定的目录中运行build.xml ,设置basedir属性,并调用默认目标,如果您正在使用非常类似的库构建文件,则应该是main If you don't want to inherit properties from MyBin when executing the MyLib task, specify inheritAll=false in the task. 如果您不想在执行MyLib任务时从MyBin继承属性, MyBin在任务中指定inheritAll=false

From the <ant> task docs for the dir attribute: 来自dir属性的<ant>任务文档:

the directory to use as a basedir for the new Ant project (unless useNativeBasedir is set to true). 用作新Ant项目的basedir的目录(除非useNativeBasedir设置为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. 默认为当前项目的basedir,除非inheritall设置为false,在这种情况下它没有默认值。 This will override the basedir setting of the called project. 这将覆盖被调用项目的basedir设置。 Also serves as the directory to resolve the antfile and output attribute's values (if any). 还用作解析antfile和输出属性值(如果有)的目录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM