tags:

views:

555

answers:

3

We have a bunch of Ant scripts, one for each of our subprojects. In the end we want to run all of these from a master build script and do some other housecleaning to package the whole project. Each of the individual Ant scripts is named ant.xml and is in the subdirectory of its project. Each uses an Ant import to import an ant-commons.xml, and then overrides some of the specific targets there.

For example, one of the projects overrides the compile target, to set the classpath appropriately, and to set the source and target to 1.5 (the ant-commons uses 1.4)

Now, in theory, this all sounds easy. Here's the master build script:

<project name="Retain" basedir="." default="main">
  <target name="main">
    <ant dir="SharedJava" antfile="ant.xml" target="clean"/>
    <ant dir="SharedJava" antfile="ant.xml" target="copy_current"/>
    <ant dir="GWEasySoap" antfile="ant.xml" target="clean"/>
    <ant dir="GWEasySoap" antfile="ant.xml" target="copy_current"/>
    <ant dir="RetainLib" antfile="ant.xml" target="clean"/>
    <ant dir="RetainLib" antfile="ant.xml" inheritAll="false"  target="copy_current"/>
    <ant dir="RetainIndex" antfile="ant.xml" target="clean"/>
    <ant dir="RetainIndex" antfile="ant.xml" target="copy_current"/>
    <ant dir="RetainPersist" antfile="ant.xml" target="clean"/>
    <ant dir="RetainPersist" antfile="ant.xml" target="copy_current"/>
  </target>
</project>

What actually happens:

The first few subprojects run fine. RetainLib, which, in fact, needs to have a reference to the SharedJava's jar, then fails, whining about how it cannot find it. When I removed RetainLib, RetainIndex failed, whining about how you shouldn't use generics in a 1.4 target file.

After playing around I determined quite simply these were insisting on running the compile target inside ant-commons instead of the overridden one.

Why? How can I get around this? (Elegantly I mean - obviously I could remove the use of ant-commons altogether and I bet things would work.)

A: 

I would suggest using the inheritAll="false" attribute on the tasks. The problem you are seeing is related to the fact that the basedir and other properties of the master file are causing issues with the relative paths of the sub-projects.

I would also loose the dir=".." attribute to insure that the sub-project basedirs win.

So this is what I would do ...

<project name="Retain" basedir="." default="main">
  <target name="main">
    <ant antfile="SharedJava/ant.xml" target="clean" inheritAll="false"/>
    <ant antfile="SharedJava/ant.xml" target="copy_current" inheritAll="false"/>
    <ant antfile="GWEasySoap/ant.xml" target="clean" inheritAll="false"/>
    <ant antfile="GWEasySoap/ant.xml" target="copy_current" inheritAll="false"/>
    ...
  </target>
</project>
raiglstorfer
Nope. Sorry. Your advice is exactly wrong, at least from the outcome.1. With dir removed and inheritFalse, the build fails immediately because the ant.xml script called is now confused as to it's directory.2. With dir restored to it's glorious and rightful place :) and inheritFalse, the build fails, exactly as described.
A: 

Every calls opens a new ant instance. Even if "SharedJava" does some overrides in ant-commons.xml, the other scripts do not see it. You could try to include ant-commons.xml in the base script and use the values in the subants, because some are inherited. For non-inheritable values, like paths you can use something like named parameters:

<ant antfile="${check.build.file}" target="validateXml">
   <property name="p.check_dir" location="${project.base.dir}" />
   <property name="p.dtd_dir" location="${build.data.dir}" />
</ant>

I choose to have no dependencies in tasks of external scripts but the ones expressed as parameters to their calls.

Peter Kofler
+1  A: 

I have worked on a project similar to this and what was done was create a build dir where the output of all of the ant tasks was placed, it worked fine. Without additional information regarding what is going on inside of the build files, it will be difficult to provide additional insight

Aaron Saunders