【问题标题】:Creating dirset that includes the subfolders of the folders创建包含文件夹子文件夹的 dirset
【发布时间】:2011-12-25 07:34:48
【问题描述】:

我有一个名为 source-locations 的属性,它包含一个以逗号分隔的文件夹列表,可以在其中找到源代码。

    source-locations=src,other_src_dir,yet_another_dir

在我的一个 ant 任务中,我使用了这样的 dirset:

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

我的问题是,在这种情况下,只有 source-locations 属性中列出的目录才会成为 dirset 的一部分,而且我还需要这些目录的所有子目录。我怎样才能做到这一点?

感谢任何输入!谢谢!

【问题讨论】:

    标签: ant directory subdirectory


    【解决方案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}" />
    

    【讨论】:

    • 这会起作用,但我不太清楚该怎么做(我不能只更改属性文件中的属性值)。您会使用属性正则表达式来添加 /** 部分吗?
    【解决方案2】:

    你可以试试这个:

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

    我使用了来自Ant-Contrib 库的propertyregexvar 任务。

    【讨论】:

      猜你喜欢
      • 2021-09-17
      • 1970-01-01
      • 2018-03-13
      • 2015-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-15
      • 1970-01-01
      相关资源
      最近更新 更多