我如何“扩展”一个ant路径(使用refId = ..访问)到路径中的所有文件,除了一些?

时间:2009-03-26 10:41:19

标签: ant

我正在尝试让ant4eclipse工作,我使用了一点ant,但并不比简单的脚本语言高。我们的Eclipse项目中有多个源文件夹,因此ant4eclipse文档中的示例需要适应:

目前我有以下内容:

<target name="build">

  <!-- resolve the eclipse output location -->
  <getOutputpath property="classes.dir" workspace="${workspace}" projectName="${project.name}" />

  <!-- init output location -->
  <delete dir="${classes.dir}" />
  <mkdir dir="${classes.dir}" />

  <!-- resolve the eclipse source location -->
  <getSourcepath pathId="source.path" project="." allowMultipleFolders='true'/>

  <!-- read the eclipse classpath -->
  <getEclipseClasspath pathId="build.classpath" 
                          workspace="${workspace}" projectName="${project.name}" />

  <!-- compile -->
  <javac destdir="${classes.dir}" classpathref="build.classpath" verbose="false" encoding="iso-8859-1">
    <src refid="source.path" />
    </javac>

  <!-- copy resources from src to bin -->
  <copy todir="${classes.dir}" preservelastmodified="true">
    <fileset refid="source.path">
        <include name="**/*"/>
        <!--
        patternset refid="not.java.files"/>
        -->
    </fileset>
  </copy>
</target>

任务成功运行,但我无法开始工作 - 它应该复制所有非java文件,以模拟eclipse的行为。

所以,我有一个名为source.path的pathId,它包含多个目录,我不知何故需要按照复制任务的方式进行按摩。我尝试过无效的嵌套,以及其他一些猜测。

我怎么能这样做 - 提前谢谢。

2 个答案:

答案 0 :(得分:3)

您可以考虑使用pathconvert来构建fileset includes可以使用的模式。

<pathconvert pathsep="/**/*," refid="source.path" property="my_fileset_pattern">
    <filtermapper>
        <replacestring from="${basedir}/" to="" />
    </filtermapper>
</pathconvert>

这将使用以下字符串填充${my_fileset_pattern}

1/**/*,2/**/*,3

如果source.path由basedir下的三个目录1,2和3组成。我们正在使用pathsep插入将在以后扩展为完整文件集的通配符。

该属性现在可用于生成所有文件的文件集。请注意,扩展集合中的最后一个目录需要额外的尾随/**/*。此时可以应用排除。

<fileset dir="." id="my_fileset" includes="${my_fileset_pattern}/**/*">
    <exclude name="**/*.java" />
</fileset>

然后,所有非java文件的副本变为:

<copy todir="${classes.dir}" preservelastmodified="true">
    <fileset refid="my_fileset" />
</copy>

这将复制源文件而不是保留todir下的源目录结构。如果需要,可以将复制任务的flatten属性设置为将所有源文件直接复制到todir

请注意,此处的pathconvert示例适用于unix文件系统,而不是Windows。如果需要可移植的东西,则应使用file.separator属性来构建模式:

<property name="wildcard" value="${file.separator}**${file.separator}*" />
<pathconvert pathsep="${wildcard}," refid="source.path" property="my_fileset">
...

答案 1 :(得分:2)

您可以使用foreach库中的ant-contrib任务:

<target name="build">
    ...

    <!-- copy resources from src to bin -->
    <foreach target="copy.resources" param="resource.dir">
        <path refid="source.path"/>
    </foreach>
</target>

<target name="copy.resources">
    <copy todir="${classes.dir}" preservelastmodified="true">
        <fileset dir="${resource.dir}" exclude="**/*.java">
    </copy>
</target>

如果您的source.path包含文件路径,那么您可以执行if任务(也来自ant-contrib)以防止尝试复制文件路径的文件,例如

<target name="copy.resources">
    <if>
        <available file="${classes.dir}" type="dir"/>
        <then>
            <copy todir="${classes.dir}" preservelastmodified="true">
                <fileset dir="${resource.dir}" exclude="**/*.java">
            </copy>
        </then>
    </if>
</target>