使用 Ant 构建其他依赖项目 [英] Building other, dependent projects with 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.
但我想要的是能够从项目 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)中的目标.但是,似乎在使用
时,它从调用文件夹的上下文中运行.这意味着项目 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 的目标,由目标目录等一些属性参数化.subant
和ant
任务优先用于触发其他项目的构建
- 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
andant
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屋!