2013-03-06 58 views
0

我們希望在不使用foreach的情況下遍歷ant中的目錄結構。 有沒有什麼優雅的方法可以做到這一點?在ant中循環目錄結構

+0

你在做什麼目錄結構?如果您正在爲每個目錄構建一個ant項目,您可以通過[subant](http://ant.apache.org/manual/Tasks/subant.html) – David 2013-03-06 16:17:50

回答

3

apply task可以遍歷一組目錄或文件

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

我個人比較喜歡的任務罐子的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> 

安裝易於實現自動化的:

<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> 

最後如果你通過其他構建fil迭代es,subant任務是非常有用的:

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

申請獲得所需的功能是不會爲我工作的,因爲我不想爲每個元素開始一個新的過程 – 2015-03-19 18:14:06

+0

@ConstantinoCronemberger Fine .... Groovy任務是更有效的循環機制。 – 2015-03-19 19:35:31

+0

實際上我使用的是maven-antrun-plugin,在這之後我可以看看groovy-maven-plugin。 – 2015-03-20 12:12:14

0

簡而言之:不是真的。有辦法解決這個問題,但我更喜歡清晰和簡單的ant-contrib <for/>任務。通過<local/>任務,您現在可以對變量的值進行本地化。之前,您有時必須使用ant-contrib的<var/>任務來重置值,因此您可以反覆遍歷它們。

<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> 

它乾淨,簡單,易於理解。很多人在Ant Contrib中遇到的最大問題是,並不是每個人都可能將它安裝在$ANT_HOME/lib目錄中。遠遠不夠。所以,如果你使用ant-contrib,把它作爲你的項目的一部分。

我把螞蟻的contrib一罐${basedir}/antlib/antcontrib,然後把這個在我的計劃:

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

現在,當有人取出我的項目,他們已經安裝了螞蟻的contrib(因爲它是內部我的項目)並且可以訪問(因爲我在我的項目中將我的<taskdef>任務指向了ant-contrib.jar的位置)。

+0

需要包裝在元素中 – pstanton 2017-05-31 06:55:56