使用Ant构建其他依赖项目 [英] Building other, dependent projects with Ant

查看:297
本文介绍了使用Ant构建其他依赖项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我作为序言说我是Ant的新手.就像这样,我只是在两天前才开始学习它,以完成此任务.

Let me preface by saying that I am new to Ant. As in, I just started learning it 2 days ago in order to accomplish this task.

我想要做的是创建一个主"蚂蚁脚本,该脚本调用其他几个蚂蚁脚本.例如:

What I'm trying to do is create a "master" ant script that calls several other ant scripts. For example:

Project A
 - contains build.xml, target "makeProjectAjar"
 - output: A.jar

Project B
 - contains build.xml, target "makeProjectBjar"
 - output: B.jar

Project C
 - contains build.xml, target "makeProjectCjar"
 - output: C.jar

Project D
 - contains build.xml, target "finalPackage"
 - Must first build Project A, Project B, Project C
 - Copy A.jar, B.jar, C.jar to Project D
 - Package all of this into D.jar

现在,我已经完成了A,B和C的所有单独构建.通过这个,我的意思是我可以从这些文件夹中的任何一个运行"ant",它将构建项目并生成一个jar文件.如果以这种方式构建A,B和C,则我有3个jar文件.然后,我可以将它们复制到D中并将其包装到最终的jar中.很好.

Right now I have all of the individual builds for A, B, and C working. By this, I mean I can run 'ant' from any of these folders and it will build the project and produce a jar file. If I build A, B, and C this way, I have 3 jar files. I can then copy them into D and package it into the final jar. That works fine.

但是我想要能够从Project D的build.xml中触发A,B和C构建.我已经尝试过:

But what I'd like is to be able to trigger the A, B, and C builds from within build.xml of Project D. I've tried this:

<?xml version="1.0" encoding="UTF-8"?>

<project name="CommonJava" default="makeCommonJavaJar" basedir=".">

    <import file="../Common/build.xml"/>
    <import file="../CommonAndroid/build.xml"/>

    <target name="makeCommonJavaJar" description="Create a jar for the CommonJava project"
        depends="clean">
        <mkdir dir="build" />
        <jar jarfile="./build/commonjava.jar" includes="*.class" />
    </target>

    <target name="clean">
        <delete dir="build" />
    </target>

    <target name="build+package" description="Build all prerequisites and package them">
        <antcall target="makeCommonJar"/>
        <antcall target="makeAndroidJar"/>
    </target>
</project>

makeCommonJar是Common/build.xml(项目A)中的目标,而makeAndroidJar是../CommonAndroid/build.xml(项目B)中的目标.但是,似乎在使用<import>时,它是从调用文件夹的上下文中运行的.意味着项目B引用的资源不可用,因为它是从项目D运行的.这有意义吗?

makeCommonJar is a target in Common/build.xml (Project A) and makeAndroidJar is a target in ../CommonAndroid/build.xml (Project B). However, it seems that when using <import>, it runs from the context of the calling folder. Meaning resources referenced by Project B are unavailable since it's running from Project D. Does that make sense?

简短的问题是...使用Ant,我如何在其他项目中调用build.xml文件以便首先构建那些项目?

Long question short... using Ant, how can I call build.xml files in other projects in order to build those projects first?

推荐答案

以下是一些通用准则(不是规则,Ant相当自由):

Here are some general guidelines (not rules, Ant is quite liberal):

  • build.xml通常设计为在特定项目上运行,因此不会导入"其他版本中.
  • import任务通常用于共享一些常见的构建机制,而不是像您正在执行的那样构建工作流.例如,通过一些属性(例如目标目录)对要构建jar的目标进行了参数化.
  • 优选使用subantant任务来触发其他项目的构建
  • a build.xml is generally designed to be run on a specific project, so it is not 'imported' in other builds
  • the import task is generally used to share some comme piece of build mechanic, not a piece of build workflow like you are doing. For instance a target to build jar, parameterized by some properties like the target directory.
  • the subant and ant tasks are preferred used to trigger builds of other project

这就是项目D的build.xml:

So here is what would look like a build.xml for project D:

<project name="CommonJava" default="makeCommonJavaJar" basedir=".">

    <target name="build-deps">
        <ant antfile="../Common/build.xml" target="makeCommonJar"/>
        <ant antfile="../CommonAndroid/build.xml" target="makeAndroidJar"/>
    </target>

    <target name="makeCommonJavaJar" depends="build-deps">
        <mkdir dir="build" />
        <jar jarfile="./build/commonjava.jar" includes="*.class" />
    </target>

</project>

请参见 http://ant.apache.org/manual/Tasks/ant.html

这篇关于使用Ant构建其他依赖项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆