首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何“展开”ant路径(使用refId=访问)到路径中除某些文件以外的所有文件?

如何“展开”ant路径(使用refId=访问)到路径中除某些文件以外的所有文件?
EN

Stack Overflow用户
提问于 2009-03-26 10:41:19
回答 2查看 4.3K关注 0票数 3

我正在尝试让ant4eclipse正常工作,我也用过一些ant,但并不比简单的脚本语言高出多少。我们在Eclipse项目中有多个源文件夹,因此ant4eclipse文档中的示例需要进行调整:

目前我有以下内容:

代码语言:javascript
复制
<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,其中包含多个目录,我需要以某种方式将其转换为复制任务所需的内容。我已经尝试了无效的嵌套,以及其他一些胡乱猜测。

我该怎么做呢--提前谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2009-03-26 12:22:34

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

代码语言:javascript
复制
<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)来防止试图复制文件路径的文件,例如

代码语言:javascript
复制
<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>
票数 2
EN

Stack Overflow用户

发布于 2010-10-04 05:31:41

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

代码语言:javascript
复制
<pathconvert pathsep="/**/*," refid="source.path" property="my_fileset_pattern">
    <filtermapper>
        <replacestring from="${basedir}/" to="" />
    </filtermapper>
</pathconvert>

它将用如下所示的字符串填充${my_fileset_pattern}

代码语言:javascript
复制
1/**/*,2/**/*,3

如果source.path由basedir下的3个目录1、2和3组成。我们使用pathsep插入通配符,这些通配符稍后将扩展到整个文件集。

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

代码语言:javascript
复制
<fileset dir="." id="my_fileset" includes="${my_fileset_pattern}/**/*">
    <exclude name="**/*.java" />
</fileset>

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

代码语言:javascript
复制
<copy todir="${classes.dir}" preservelastmodified="true">
    <fileset refid="my_fileset" />
</copy>

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

请注意,这里的路径转换示例是针对unix文件系统的,而不是针对windows的。如果需要一些可移植的东西,那么应该使用file.separator属性来构建模式:

代码语言:javascript
复制
<property name="wildcard" value="${file.separator}**${file.separator}*" />
<pathconvert pathsep="${wildcard}," refid="source.path" property="my_fileset">
...
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/685237

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档