eleshmistry
eleshmistry

Reputation: 31

Ant - Run a Build.xml for all subdirectories

I have a build.xml sitting at the top level and I want the script to run a target for each subdirectory and pass in the subdirectory name as a parameter to the ANT target.

Can you help ?/??

Thanks

Upvotes: 3

Views: 10652

Answers (4)

bakoyaro
bakoyaro

Reputation: 2558

If I read the question correctly, this may be what you are looking for instead.

So for your example...

<target name="do-all">
    <antcall target="do-first">
       <param name="dir-name" value="first"/>
       <param name="intented-target" value="init"/>
    </antcall>
    <antcall target="do-first">
        <param name="dir-name" value="second"/>
        <param name="intented-target" value="build"/>
    </antcall>
    <antcall target="do-first">
        <param name="dir-name" value="third"/>
        <param name="intented-target" value="compile"/>
    </antcall>
</target>
<target name="do-first">
    <echo>Hello from ${dir-name} ${intented-target}</echo>
    <ant antfile="${dir-name}/build.xml" target="${intented-target}"/> 
</target>

When you are calling this from Ant, you would enter this at the command line:

ant do-all

and your output should look like this:

do-all:

do-first:

[echo] Hello from first init

do-first:

[echo] Hello from second build

do-first:

[echo] Hello from third compile

BUILD SUCCESSFUL Total time: 1 second

You will of course need to make sure that the directory name that you are using as a param actually exists, or the build will fail.

You can also always feed the variable that you want to use by adding the value to the build.properties file.

Upvotes: 1

artplastika
artplastika

Reputation: 1982

If you would like to do it in ant build file, you could use Ant Contrib's for task to iterate over list of subdirectories and execute ant task for each of them.

<for param="subdir">
  <dirset dir="${build.dir}">
    <include name="./**"/>
  </dirset>
  <sequential>
    <subant target="${target}">
      <property name="subdir.name" value="@{subdir}"/>
    </subant>
  </sequential>
</for>

I didn't test this code since don't have ant installed, but it is close to what you're trying to do I suppose.

Upvotes: 1

David W.
David W.

Reputation: 107030

Take a look at the subant task. From that page:

    <project name="subant" default="subant1">
        <property name="build.dir" value="subant.build"/>
        <target name="subant1">
            <subant target="">
                <property name="build.dir" value="subant1.build"/>
                <property name="not.overloaded" value="not.overloaded"/>
                <fileset dir="." includes="*/build.xml"/>
            </subant>
        </target>
    </project>

this snippet build file will run ant in each subdirectory of the project directory, where a file called build.xml can be found. The property build.dir will have the value subant1.build in the ant projects called by subant.

Upvotes: 4

NoodleX
NoodleX

Reputation: 749

this is might be what you looking for,

put this as one of your target in your parent build.xml

<target name="executeChildBuild">

    <ant antfile="sub1/build.xml" target="build" />
    <ant antfile="sub2/build.xml" target="build" />

</target>

Upvotes: 2

Related Questions