Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
209 views
in Technique[技术] by (71.8m points)

java - Building other, dependent projects with 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

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.

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 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?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

  • 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

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>

See http://ant.apache.org/manual/Tasks/ant.html


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...