How to remap a bunch of directories in Ant? - ant

I have the following :
<dirset dir="../release">
<include name="*/src/**"/>
</dirset>
But I want the "*/src" lobbed out. How is this done? I haven't been able to figure out how to do this with <mapper/> or <pathconvert/>.

If I understand your question correctly, you just need to add an exclude, i.e.:
<dirset dir="../release">
<include name="*/src/**"/>
<exclude name="*/src"/>
</dirset>
... or perhaps:
<dirset dir="../release" id="ds">
<include name="*/src/**"/>
</dirset>
<pathconvert refid="ds" property="ds.prop">
<mapper type="regexp" from=".*/src/(.*)" to="\1" />
</pathconvert>

Related

how to reuse the definition of a number of filesets?

I have a number of tasks in my build.xml that all utilize the same set of filesets. E.g. something like the following (I've retain the actual names of the tasks - related to the cobertura coverage tool - but the nature of the enveloping task is immaterial to this question).
<target name="coverage-report">
<cobertura:cobertura-report destdir="${coverage.xml.dir}" format="xml">
<fileset dir="${src.dir}">
<include name="**/*.java" />
</fileset>
<fileset dir="${componentFoo.dir}/src">
<include name="**/*.java" />
</fileset>
</cobertura:cobertura-report>
</target>
<target name="summary-coverage-report">
<cobertura:cobertura-report destdir="${coverage.summaryxml.dir}" format="summaryXml">
<fileset dir="${src.dir}">
<include name="**/*.java" />
</fileset>
<fileset dir="${componentFoo.dir}/src">
<include name="**/*.java" />
</fileset>
</cobertura:cobertura-report>
</target>
<target name="alternate-coverage-report">
<cobertura:cobertura-report destdir="${coverage.html.dir}">
<fileset dir="${src.dir}">
<include name="**/*.java"/>
</fileset>
<fileset dir="${componentFoo.dir}/src">
<include name="**/*.java" />
</fileset>
</cobertura:cobertura-report>
</target>
What I would like to do is to be able to define once this recurring set of filesets and reuse it. Now, there's a related SO question on how to use the same fileset in multiple places but that's not working here as I have a number of filesets, not just one. I also tried wrapping the filesets inside a macrodef and expanding the macrodef when needed but I get the message that the these tasks "don't support the nested [name of macrodef] element". So I guess macrodefs in Ant can only be used as high-level tasks and cannot be expanded in arbitrary places.
So is there a way to reuse a definition of a number of filesets in Ant?
This would be easy if the cobertura tasks accepted arbitrary resource collections instead of being hard-coded to just fileset - it may be worth submitting a feature request to this end. For example with a copy task:
<resources id="filesets">
<fileset dir="${src.dir}">
<include name="**/*.java" />
</fileset>
<fileset dir="${componentFoo.dir}/src">
<include name="**/*.java" />
</fileset>
</resources>
<copy todir="dest">
<resources refid="filesets"/>
</copy>
You can fall back to the purely XML-level and use an entity reference:
<!DOCTYPE project [
<!ENTITY cobFilesets '<fileset dir="${src.dir}">
<include name="**/*.java" />
</fileset>
<fileset dir="${componentFoo.dir}/src">
<include name="**/*.java" />
</fileset>'>
]>
<project ....>
<target name="coverage-report">
<cobertura:cobertura-report destdir="${coverage.xml.dir}" format="xml">
&cobFilesets;
</cobertura:cobertura-report>
</target>
</project>

Delete fileset filenames, but in different directory

So I have a fileset containing files in one directory:
<fileset id="modules" dir="${modules.dir}">
<include name="core*.jar"/>
<include name="fileset*.jar"/>
<include name="upgrader*.jar"/>
<include name="hello*.jar"/>
</fileset>
However, these files are copied into the ${lib.dir}, i.e, the ${lib.dir} contains copies of core*.jar, fileset*.jar, etc.
How do I delete these copied files?
Also, please note I can't use external libraries like ant-contrib.
Use a PatternSet to define the set of names. Then reference that PatternSet in any number of FileSets.
<patternset id="module.patterns">
<include name="core*.jar"/>
<include name="fileset*.jar"/>
<include name="upgrader*.jar"/>
<include name="hello*.jar"/>
</patternset>
<fileset id="modules" dir="${modules.dir}" >
<patternset refid="module.patterns"/>
</fileset>
UPDATE:
Given your comment that you want only the original files, try this:
<pathconvert pathsep="," property="flattened.modules" refid="modules">
<mapper type="flatten" />
</pathconvert>
<filelist id="libmodules" dir="${lib.dir}" files="${flattened.modules}"/>

ANT: several filesets for the javac task

I have a ANT build file with two different filesets:
<fileset id="fs1"> <include name="source1\**" /> </fileset>
<fileset id="fs2"> <include name="source2\**" /> </fileset>
and want to use both of them in the same javac task.
Of course, I could write
<javac ...>
<include name="source1\**" />
<include name="source2\**" />
</javac>
but I want the selection of the files occurs outside, because my javac-task will be called several times with different parameters.
A solution would be to copy all the files in a temp directory and call javac on it. But you will admit it is not very satisfying...
More over: is there a solution to concatenate two filesets in a single one?
I've found a solution, using patternset.
<patternset id="ps1"> <include name="source1\**" /> </patternset>
<patternset id="ps2"> <include name="source2\**" /> </patternset>
<javac ...>
<patternset>
<patternset refid="ps1" />
<patternset refid="ps2" />
</patternset>
</javac>

Excluding some classes from the cobertura report doesn't work

I tried to exclude some classes from cobertura using:
<cobertura-instrument todir="${voldemort.instrumented.dir}" datafile="${cobertura.instrument.file}">
<classpath refid="tools-classpath" />
<ignore regex=".*\.xsd" />
<fileset dir="${voldemort.dist.dir}/classes">
<include name="**/*.class" />
<exclude name="**/client/protocol/pb/*.class"/>
<exclude name="**/server/http/*.class"/>
</fileset>
</cobertura-instrument>
However, that doesn't work. I don't know where the problem is. Could you help me?
You must do it like this:
<cobertura-instrument todir="${voldemort.instrumented.dir}" datafile="${cobertura.instrument.file}">
<classpath refid="tools-classpath" />
<ignore regex=".*\.xsd" />
<fileset dir="${voldemort.dist.dir}/classes">
<include name="**/*.class" />
</fileset>
<fileset dir="${voldemort.dist.dir}/classes">
<exclude name="**/client/protocol/pb/*.class"/>
</fileset>
<fileset dir="${voldemort.dist.dir}/classes">
<exclude name="**/server/http/*.class"/>
</fileset>
</cobertura-instrument>

How can I print a fileset to a file, one file name per line?

I have a populated fileset and I need to print the matching filenames into a text file.
I tried this:
<fileset id="myfileset" dir="../sounds">
<include name="*.wav" />
<include name="*.ogg" />
</fileset>
<property name="sounds" refid="myfileset" />
<echo file="sounds.txt">${sounds}</echo>
which prints all the files on a single line, separated by semicolons. I need to have one file per line. How can I do this without resorting to calling OS commands or writing Java code?
UPDATE:
Ah, should have been more specific - the list must not contain directories. I'm marking ChssPly76's as the accepted answer anyway, since the pathconvert command was exactly what I was missing. To strip the directories and list only the filenames, I used the "flatten" mapper.
Here is the script that I ended up with:
<fileset id="sounds_fileset" dir="../sound">
<include name="*.wav" />
<include name="*.ogg" />
</fileset>
<pathconvert pathsep="
" property="sounds" refid="sounds_fileset">
<mapper type="flatten" />
</pathconvert>
<echo file="sounds.txt">${sounds}</echo>
Use the PathConvert task:
<fileset id="myfileset" dir="../sounds">
<include name="*.wav" />
<include name="*.ogg" />
</fileset>
<pathconvert pathsep="${line.separator}" property="sounds" refid="myfileset">
<!-- Add this if you want the path stripped -->
<mapper>
<flattenmapper />
</mapper>
</pathconvert>
<echo file="sounds.txt">${sounds}</echo>
Since Ant 1.6 you can use toString:
<echo file="sounds.txt">${toString:myfileset}</echo>

Resources