Delete fileset filenames, but in different directory - ant

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}"/>

Related

How exclude in fileset checking if property is set?

I need to create a fileset but I need to exclude a folder if some property is set (project.name).
<fileset dir="${build.folder}">
<include name="**/*"/>
<exclude name="${project.name}/**/*"/>
</fileset>
How can I do it?
Assuming you are using a recent version of ANT (1.9.1+), try this:
<project name="tryit" xmlns:if="ant:if" xmlns:unless="ant:unless">
<target name="try">
<fileset id="my-fileset" dir="${build.folder}">
<include name="**/*"/>
<exclude if:set="project.name" name="${project.name}/**/*"/>
</fileset>
<echo message="My fileset is ${toString:my-fileset}" />
</target>
</project>

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>

Is it possible to have a fileset with multiple source directories?

I have some ant logic that looks like this:
<copy todir="src_#{version}" flatten="true">
<fileset dir="${generic-libs.source}\prolo">
<include name="**/*.c"/> <!-- all .c files including subfolders -->
<include name="**/*.h"/> <!-- all .h files including subfolders -->
<exclude name="**/test/*.*" /> <!-- exclude test (sub)folders -->
<exclude name="**/test*.*" /> <!-- exclude remaining test sources -->
</fileset>
<fileset dir="${generic-libs.source}\genlo">
<include name="**/*.c"/>
<include name="**/*.h"/>
<exclude name="**/test/*.*" />
<exclude name="**/test*.*" />
</fileset>
<fileset dir="${generic-libs.source}\ptclo">
<include name="**/*.c"/>
<include name="**/*.h"/>
<exclude name="**/test/*.*" />
<exclude name="**/test*.*" />
</fileset>
...
</copy>
The ... has 10 more identical constructs.
The contents of the fileset tag are always the same, so I guess there has to be a way to shorten this?
Ant 1.9.4 introduced multirootfileset for that purpose :
A new resourcecollection type acts like a union of <fileset>s and <dirset>s that share the same configuration but have different base directories.
In your case something like :
<multirootfileset basedirs="${generic-libs.source}\prolo,${generic-libs.source}\genlo,${generic-libs.source}\ptclo">
<include name="**/*.c"/>
<include name="**/*.h"/>
<exclude name="**/test/*.*"/>
<exclude name="**/test*.*"/>
</multirootfileset>

How to get all the filenames from a directory using ant

Using ant I want to get all the filenames from a directory and create a property with value as comma separated file names.
Example: If we have 3 files in a directory (i.e. 1.txt, 2.txt, 3.txt) then we have to create a property & its value should be 1.txt,2.txt,3.txt.
Thanks,
Mansoor MD.
First, you need to create a path with the files:
<path>
<fileset dir="${dir.name}">
<include name="*"/>
</fileset>
</path>
This can be combined with a <pathconvert> task:
<pathconvert pathsep=","
property="my.files">
<path>
<fileset dir="${dir.name}">
<include name="*"/>
</fileset>
</path>
</pathconvert>
The property ${my.files} will contain a comma separated list of files.
If you prefer, you can do this in two steps:
<path id="mypath">
<fileset dir="${dir.name}">
<include name="*"/>
</fileset>
</path>
<pathconvert pathsep=","
property="my.files"
refid="mypath"/>
Word 'o Warning: It will also contain the full path to these files.

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>

Resources