使用Ant组合文件集

时间:2011-07-12 05:11:07

标签: ant

我在Ant中定义了两个不同的文件集,如下所示:

<fileset id="fileset1" dir="${classes.dir}">
</fileset>

<zipfileset id="fileset2" src="myArchive.zip" includes="**/*.class">
</zipfileset>

我想创建第三个文件集,它是上述两个文件集的联合

<fileset id="merged">
</fileset>

有人能告诉我怎么做吗?甚至可以做那样的事情吗? 提前谢谢!

3 个答案:

答案 0 :(得分:18)

一种方法是使用Ant resource collections,特别是union

<fileset id="fileset1" dir="${classes.dir}" />
<zipfileset id="fileset2" src="myArchive.zip" includes="**/*.class" />

<union id="onion">
    <resources refid="fileset1" />
    <resources refid="fileset2" />
</union>

然后你可以在任何你可能使用文件集的地方引用'洋葱',例如

<copy todir="dest">
    <resources refid="onion" />
</copy>

我建议使用通用resources元素而不是文件集,以获得最大的灵活性。

答案 1 :(得分:2)

试试这个:我认为它应该有用,因为<fileset>是一个隐含的<patternset>

<fileset id="fileset1" dir="${classes.dir}">
</fileset>

<zipfileset id="fileset2" src="myArchive.zip" includes="**/*.class">
</zipfileset>
编辑:奇怪。这也许呢?

<patternset id="merged">
  <patternset refid="fileset1" />
  <patternset refid="fileset2" />
</patternset>

答案 2 :(得分:0)

文件集的问题是,它需要目录作为应用patternset的基础。这意味着您必须找到所有文件集共享的公共基本目录。

<pathconvert>任务可以通过refid获取文件集。您可以放置​​几个文件集(例如,从各个构建目标中将文件集组合到模块化构建环境的根/主目标中):


<project name="root" basedir="." xmlns:if="ant:if" xmlns:unless="ant:unless">
<!-- 
it's important to take the xmlns:features in your project head 
otherwhise this code won't work 
-->
    <target name="init">
        <!-- set some common prerequisites -->
        <property name="prerequisite.property.xyz" value="xyz" />
    </target>

    <target name="targetA" depends="init">
        <fileset dir="${common.basedir}${file.separator}${targetA.subdir}" id="targetA.fileset">
            <include name="**/*.html" />
        </fileset>
        <property name="targetA.fileset.exists" value="true" />
    </target>

    <target name="targetB" depends="init">
        <fileset dir="${common.basedir}${file.separator}${targetB.subdir}" id="targetB.fileset">
            <include name="**/*.java" />
        </fileset>
        <property name="targetB.fileset.exists" value="true" />
    </target>

    <target name="targetC" depends="init">
        <fileset dir="${common.basedir}${file.separator}${targetC.subdir}" id="targetC.fileset">
            <include name="**/*.class" />
        </fileset>
        <property name="targetC.fileset.exists" value="true" />
    </target>

    <target name="root" depends="init">
        <pathconvert property="all.files.as.commaseparated.path" pathsep="," dirsep="/">
            <fileset refid="targetA.fileset" if:true="${targetA.fileset.exists}" />
            <fileset refid="targetB.fileset" if:true="${targetB.fileset.exists}" />
            <fileset refid="targetC.fileset" if:true="${targetC.fileset.exists}" />
            <map from="${common.basedir}/" to="" />
        </pathconvert>
        <!-- assemble new fileset from paths as comma separated property string -->
        <fileset id="new.refid" dir="${common.basedir}" includes="${all.files.as.commaseparated.path}" />
    </target>
</project>

这可以通过以下命令行来调用:

ant targetA targetB targetC root

ant targetA root

请注意,root始终是被调用的最后一个目标。