Loop through directory structure in ant

落爺英雄遲暮 提交于 2019-12-11 15:33:10

问题


We want to loop through directory structure in ant without using foreach . Is there any elegant way to do the same ?


回答1:


The apply task can iterate over a set of directories or files

<target name="run-apply">
    <apply executable="echo">
        <dirset dir="src"/>
    </apply>
</target>

I personally like the groovy ANT task

<target name="run-groovy">
    <taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy"/>
    <dirset id="dirs" dir="src"/>
    <groovy>
        project.references.dirs.each {
            ant.echo it
        }
    </groovy>
</target>

The installation of the task jar is easily automated:

<target name="install-groovy">
  <mkdir dir="${user.home}/.ant/lib"/>
  <get dest="${user.home}/.ant/lib/groovy-all.jar" src="http://search.maven.org/remotecontent?filepath=org/codehaus/groovy/groovy-all/2.1.1/groovy-all-2.1.1.jar"/>
</target>

Finally if you're iterating thru other build files, the subant task is very useful:

<target name="run-subant">
    <subant>
        <fileset dir="src" includes="**/build.xml"/>
    </subant>
</target>



回答2:


Short answer: Not really. There are ways around this, but I prefer the ant-contrib <for/> task for clarity and simplicity. With the <local/> task, you can now localize values of variables. Before, you sometimes had to use ant-contrib's <var/> task to reset the values, so you could loop through them over and over.

<for param="directory">
    <fileset dir="${some.dir}"/>
    <sequential>
        <local name="foo"/>
        <local name="bar"/>  <!-- Properties that may change with each iteration -->
        <!-- Here be dragons -->
    </sequential>
</for>

It's clean, simple, and easy to understand. The big issue many people have with Ant Contrib is that not everyone may have it installed in their $ANT_HOME/lib directory. Far enough. So, if you use ant-contrib, put it as part of your project.

I'll put the ant-contrib jar in ${basedir}/antlib/antcontrib and then put this in my program:

<taskdef resource="net/sf/antcontrib/antlib.xml">
    <classpath>
        <fileset dir="${basedir}/antlib/antcontrib"/>
    </classpath>
</taskdef>

Now, when someone checks out my project, they have ant-contrib already installed (since it's inside my project) and accessible (since I point my <taskdef> task at the location of ant-contrib.jar in my project).



来源:https://stackoverflow.com/questions/15245283/loop-through-directory-structure-in-ant

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