2011-11-09 179 views
4

我有一個名爲source-locations的屬性,它包含逗號分隔的可以找到源代碼的文件夾列表。創建包含文件夾子文件夾的dirset

source-locations=src,other_src_dir,yet_another_dir 

在我的Ant任務之一,我用的是dirset這樣的:

<dirset dir="${basedir}" includes="${source-locations}"/> 

我的問題是,在這種情況下,只有在源位置屬性中列出的目錄將是dirset的一部分我也需要這些目錄的所有子目錄。我怎樣才能做到這一點?

任何輸入讚賞!謝謝!

回答

1

你可以試試這個:

<taskdef resource="net/sf/antcontrib/antcontrib.properties" classpathref="path.antcontrib"/> 

<target name="test"> 
    <property name="source-locations" value="src,other_src_dir,yet_another_dir"/> 

    <!--Replace comma symbol to the `/**,` string and save new expression in to the source-locations_mod property--> 
    <propertyregex property="source-locations_mod" 
      input="${source-locations}" 
      regexp="," 
      replace="/**," 
      global="true" /> 

    <!--Add finally `/**` string to the source-locations_mod property. Was used var task to prevent property immutable --> 
    <var name="source-locations_mod" value="${source-locations_mod}/**"/> 

    <!--New source-locations_mod property was used--> 
    <dirset id="source.set" dir="${root.folder}" includes="${source-locations_mod}"/> 

    <!--Check the result--> 
    <pathconvert pathsep="${line.separator}" 
      property="dir.name" 
      refid="source.set"> 
     <mapper type="identity" /> 
    </pathconvert> 
    <echo>Folders: ${dir.name}</echo> 
</target> 

我用propertyregexAnt-Contribvar任務。

+0

優秀!這工作!非常感謝Alex! – Liz

1

以下示例。

<mkdir dir="dir1" /> 
<mkdir dir="dir2" /> 
<mkdir dir="dir3" /> 
<mkdir dir="dir1/dir1a" /> 
<mkdir dir="dir1/dir1b" /> 
<mkdir dir="dir1/dir1c" /> 
<mkdir dir="dir2/dir2e" /> 
<mkdir dir="dir2/dir2f" /> 
<mkdir dir="dir2/dir2g" /> 
<mkdir dir="dir3/dir3h" /> 
<mkdir dir="dir3/dir3i" /> 
<mkdir dir="dir3/dir3j" /> 

<property name="source-locations" value="dir1,dir2,dir3" /> 

<pathconvert property="source-locations_mod" pathsep=","> 
    <regexpmapper from="^(.*)$" to="\1/\*\*" handledirsep="true" /> 
    <map from="${basedir}/" to="" /> 
    <dirset dir="${basedir}" includes="${source-locations}" /> 
</pathconvert> 
<echo message="source-locations_mod: ${source-locations_mod}" /> 

<dirset id="dirset" dir="${basedir}" includes="${source-locations_mod}"/> 

<property name="dirs" refid="dirset" /> 
<echo message="dirs: ${dirs}" /> 

+0

這將工作,但我不知道如何做到這一點(我不能只改變屬性文件中的屬性的值)。你會用一個propertregex來添加/ **部分嗎? – Liz

相關問題