简体   繁体   中英

How can I build multiple projects in Ant with one build file?

Can I build multiple projects from one build-file. Example:

<project basedir="." default="all" name="app1">
    ...
</project>

<project basedir="." default="all" name="app2">
    ...
</project>

Currently I type ant -f build1.xml compile and it builds my application and I have to use two separate build files. Is there some way to get it running in a way that i have both the projects defined a common build-file and I can type something like ant app1 compile or ant app2 compile ?

Here's what my build-file looks like:

<?xml version="1.0" encoding="UTF-8"?>
<project name="azebooster" default="dist" basedir=".">

    <!-- Globals -->
    <property name="src" location="src/com/aelitis"/>
    <property name="build" location="build/azebooster"/>
    <property name="jar" location="jar/azebooster"/>
    <property name="resources" location="res/azebooster"/>

    <!-- Paths -->
    <path id="classpath">
        <fileset dir="." includes="**/*.jar"/>
    </path>

    <!-- Start it -->
    <target name="init">
      <tstamp/>
      <mkdir dir="${build}"/>
      <mkdir dir="${jar}"/>
    </target>

    <!-- Build it -->
    <target name="compile" depends="init" description="compile the source" >
        <javac srcdir="${src}" destdir="${build}">
            <classpath>
                <path refid="classpath"/>
            </classpath>
        </javac>
    </target>

    <!-- Jar it -->
    <target name="jar" depends="compile">
        <jar destfile="${jar}/${ant.project.name}.jar">
            <fileset dir="${build}"/>
            <fileset dir="${resources}" />
        </jar>
    </target>

    <!-- Clean it -->
    <target name="clean" description="clean up" >
        <tstamp/>
        <delete dir="${build}"/>
        <delete dir="${jar}"/>
    </target>

</project>

Thank you.

Yes you can create a default.build file (in this way you don't need to specify the file, because it's used by default). On it you can create the following targets:

<target name="all" depends="app1, app2" />

<target name="app1">
    <ant antfile=<app1file> target="compile" />        
</target>

<target name="app2">
    <ant antfile=<app2file> target="compile" />        
</target>

On this way you can use both ant file from one unique file.

And you can do all in only one file, if you replace the app1 and app2 targets, with the needed targets to compile them that you have in the separate files.

EDITED:

You have different ways to include both of them in only one file, maybe the easiest one is include a suffix for each project on each target. And you can call the specific project target or the target for both.

I put you an example with the compile target (and for init target too).

You can use compile for compile both projects (I call the other project other), and compileazeboster to compile the azeboster project.

You can search the common things to avoid the innecesary duplicated code (common paths, targets and so on)

<property name="srcazebooster" location="src/com/aelitis"/>
<property name="buildazebooster" location="build/azebooster"/>
<property name="jarazebooster" location="jar/azebooster"/>
<property name="srcother" location="src/other"/>
<property name="buildother" location="build/other"/>
<property name="jarother" location="jar/other"/>


<!-- Start it -->
<target name="init" depends="initazebooster, initother"/>

<!-- Build it -->
<target name="compile" depends="compileazebooster, compileother" description="compile the source for all" />



<!-- Start azebooster-->
<target name="initazebooster">
    <tstamp/>
    <mkdir dir="${buildazebooster}"/>
    <mkdir dir="${jarazebooster}"/>
</target>

<!-- Build azeboster-->
<target name="compileazebooster" depends="initazebooster" description="compile the source for azebooster" >
    <javac srcdir="${srcazebooster}" destdir="${buildazebooster}">
        <classpath>
            <path refid="classpath"/>
        </classpath>
    </javac>
</target>

<!-- Start other-->
<target name="initother">
    <tstamp/>
    <mkdir dir="${buildotherr}"/>
    <mkdir dir="${jarother}"/>
</target>

<!-- Build other-->
<target name="compileother" depends="initother" description="compile the source for other" >
    <javac srcdir="${srcother}" destdir="${buildother}">
        <classpath>
            <path refid="classpath"/>
        </classpath>
    </javac>
</target>

I would like to add to the excellent answer of Borja some important steps.

How to organize the common ant files?

Let's say we have some big Project consisting of many Eclipse projects. All of them are in a worspace folder. Thus, the workspace itself makes the global Project. (Notice P/p difference). Very often you already have an Ant build file in the workspace folder. Or you have created the common ant files (build and properties) in it by yourself.

Later you want to launch that global build file. How? Do you want to go into the workspace folder, change to the command line or shell window and run the ant from there, having the output in the external console and switching from Eclipse to that window and back? And the same problem for editing? Having two additional windows? No, surely you want to have all windows, editing and output, in Eclipse. But how can we reference those global build files from inside the Eclipse?

  • Go to Package Explorer .
  • Make an empty project(not Java one), let's name it _Global (we want to see it always on the top).
  • Right click on its name. Choose Import -> General -> File System.
  • Press Advanced . Check Create links in Workspace , Create virtual folders , create link locations . The last choose for WORKSPACE_LOC .
  • In From Directory go to the workspace or where you have created the common build. After all, it could be even the common one for several workspaces.
  • You will see a dialogue with file names on the right with checkfields. Check the files you need. (You will need to import every new common file you created thereafter).
  • Finish .

Now you see your project with references to the global files. As for properties files, you are ready. But for build files you need some more steps:

  • Right click your global build file, -> Run -> Run Configurations . You are in the Ant Build group of configurations.
  • Press the New Launch Configuration button with plus on it (above the launch configurations list). Now you have the run configuration for that global build.
  • Set its parameters and run/debug it.

You will probably be able to do what you want using macros (though it depends precisely on what is common between your two projects), though your command is more likely to be

ant compile app1

or

ant compile app2

where compile is a target which calls a macro, using app1/app2 as a parameter to either decide which macro to call or to pass into the macro itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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