如何“动态”生成文件集?

时间:2022-11-22 23:35:12

How to dynamically generate a fileset based on user input?

如何根据用户输入动态生成文件集?

Given this directories :

鉴于此目录:

root
--dir1
----filesA.txt
----subdir1_1
--dir2
----filesB.xml
--dir3
----filesC.java
----subdir3_1
--dir4
----filesD.txt
----subdir4_1
------subdir4_1_1

and that command-line call :

和那个命令行调用:

ant -Ddirectory="dir1 dir3"

<target name="zip">
  <zip destfile="${root}/archive.zip">
    <fileset dir="${root}">
      <include name="**/*"/>
    </fileset>
  </zip>
</target>

I want to zip only the directory (and theirs subfiles) specified by the user. I have thought using the PropertyRegex tasks but I thought that's an ugly way to do it.

我想只压缩用户指定的目录(及其子文件)。我曾经想过使用PropertyRegex任务,但我认为这是一种丑陋的方式。

1 个解决方案

#1


Use foreach from antcontrib foreach:

使用来自antcontrib foreach的foreach:

ant -Ddirectory="dir1,dir3"

<project name="build" default="zip" basedir=".">
<!-- declare ant-contrib -->
<taskdef resource="net/sf/antcontrib/antcontrib.properties">
  <classpath>
    <pathelement location="${basedir}/ant-contrib-1.0b3.jar"/>
  </classpath>
</taskdef>

<property name="root" value ="folder"/>
<target name="zip">
    <delete file="${root}/archive.zip"/>
    <foreach list="${directory}" param="folder" target="zipdir"/>
</target>

<target name="zipdir">
    <echo>${folder}</echo>
    <zip destfile="${root}/archive.zip" update="true">
        <fileset dir="${root}">
            <include name="${folder}/**/*"/>
        </fileset>
    </zip>
</target>

#1


Use foreach from antcontrib foreach:

使用来自antcontrib foreach的foreach:

ant -Ddirectory="dir1,dir3"

<project name="build" default="zip" basedir=".">
<!-- declare ant-contrib -->
<taskdef resource="net/sf/antcontrib/antcontrib.properties">
  <classpath>
    <pathelement location="${basedir}/ant-contrib-1.0b3.jar"/>
  </classpath>
</taskdef>

<property name="root" value ="folder"/>
<target name="zip">
    <delete file="${root}/archive.zip"/>
    <foreach list="${directory}" param="folder" target="zipdir"/>
</target>

<target name="zipdir">
    <echo>${folder}</echo>
    <zip destfile="${root}/archive.zip" update="true">
        <fileset dir="${root}">
            <include name="${folder}/**/*"/>
        </fileset>
    </zip>
</target>