使用Path而不是FileSet的Ant复制任务

时间:2011-08-08 07:49:06

标签: ant path copy

我正在使用Ant 1.7,想要从不同路径复制文件(它们没有任何关系,因此我无法使用 include 选择器将它们从根目录中过滤掉)。我尝试使用<path>内的<copy>代替<fileset>,因为<path>我可以指定<fileset>中不可能的多路径。我的蚂蚁脚本看起来像这样,但它不起作用。

<target name="copytest">
    <!-- copy all files in test1 and test2 into test3 -->
    <copy todir="E:/test3">
        <path>
            <pathelement path="C:/test1;D:/test2"></pathelement>
        </path>
    </copy>
</target>

有人知道如何在<path>内使用<copy>吗?或者也许任何人都有关于如何在没有选择器的情况下从不同来源复制文件的建议?

顺便说一句,我不想​​对源目录进行硬编码,它们将从属性文件中读取,因此不应考虑在<fileset>内编写多个<copy>

提前感谢!

4 个答案:

答案 0 :(得分:5)

仅当flatten属性设置为true时才有效:

<copy todir="E:/test3" flatten="true">
    <path>
        <pathelement path="C:/test1;D:/test2"></pathelement>
    </path>
</copy>

Ant Copy task documentationExamples部分记录了这一点。

答案 1 :(得分:0)

<pathelement>通常使用它的path属性作为对classpath或其他预定义位置的引用,如果您想在类路径之外提供的特定文件位置尝试使用location属性

<pathelement location="D:\lib\helper.jar"/>
  

location属性指定单个文件或目录相对   到项目的基目录(或绝对文件名),而   path属性接受以冒号或分号分隔的列表   位置。 path属性旨在与预定义一起使用   paths - 在任何其他情况下,具有位置属性的多个元素   应该是首选。

答案 2 :(得分:0)

我们遇到同样的问题

有点复杂,我们需要为从路径

转换的每个文件集添加指定的模式集

例如,这是传入数据

<path id="myDirList" path="C:/test1;D:/test2" />
<patternset id="myPatterns" includes="*.html, *.css, etc, " />

我们写了一个脚本来解决这个问题

<resources id="myFilesetGroup">
    <!-- mulitiple filesets to be generated here 
    <fileset dir="... dir1, dir2 ...">
        <patternset refid="myPatterns"/>
    </fileset>
    -->
</resources>
<script language="javascript"><![CDATA[
    (function () {
        var resources = project.getReference("myFilesetGroup");
        var sourceDirs = project.getReference("myDirList").list();
        var patterRef = new Packages.org.apache.tools.ant.types.Reference(project, "myPatterns");
        for (var i = 0; i < sourceDirs.length; i++) {
            var fileSet = project.createDataType("fileset");
            fileSet.dir = new java.io.File(sourceDirs[i]);
            fileSet.createPatternSet().refid = patterRef;
            resources.add(fileSet);
        }
    })();
]]></script>

现在您可以在复制任务中使用此资源

<!-- copy all files in test1 and test2 into test3 -->
<copy todir="E:/test3">
    <resources refid="myFilesetGroup">
</copy>

答案 3 :(得分:-1)

I tried this and works fine

<fileset file="${jackson.jaxrs.lib}"/>
相关问题