tags:

views:

182

answers:

1

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.

+2  A: 

Use foreach from antcontrib 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>

ILikeCoffee
Awesome! I haven't thought of the zip task update property, great idea! Thanks!
madgnome