How can I exclude files from a reference path in Ant?

北战南征 提交于 2019-12-23 09:01:40

问题


In a project we have several source paths, so we defined a reference path for them:

<path id="de.his.path.srcpath">
    <pathelement path="${de.his.dir.src.qis.java}"/>
    <pathelement path="${de.his.dir.src.h1.java}"/>
    ...
</path>

Using the reference works fine in the <javac> tag:

<src refid="de.his.path.srcpath" />

In the next step, we have to copy non-java files to the classpath folder:

<copy todir="${de.his.dir.bin.classes}" overwrite="true">
    <fileset refid="de.his.path.srcpath">
       <exclude name="**/*.java" />
    </fileset>
</copy>

Unfortunately, this does not work because "refid" and nested elements may not be mixed.

Is there a way I can get a set of all non-java files in my source path without copying the list of source paths into individual filesets?


回答1:


Here's an option. First, use the pathconvert task to make a pattern suitable for generating a fileset:

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

Next make the fileset from all the files in the paths, except the java sources. Note the trailing wildcard /**/* needed as pathconvert only does the wildcards within the list, not the one needed at the end:

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

Then your copy task would be:

<copy todir="${de.his.dir.bin.classes}" overwrite="true" >
    <fileset refid="my_fileset" />
</copy>

For portability, instead of hard-coding the unix wildcard /**/* you might consider using something like:

<property name="wildcard" value="${file.separator}**${file.separator}*" />


来源:https://stackoverflow.com/questions/3839596/how-can-i-exclude-files-from-a-reference-path-in-ant

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!