Ant - Run a Build.xml for all subdirectories

天涯浪子 提交于 2019-12-09 22:01:32

问题


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


回答1:


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.




回答2:


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>



回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/6098463/ant-run-a-build-xml-for-all-subdirectories

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!