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

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>

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>

Create specific WAR file

In a ANT script, is there a way to include a file from 'environnement' directory into the different war ?
My filesystem tree :
environnementDEVweb.xmllog4j.propertiesINTweb.xmllog4j.propertiesWebContentWEB-INFweb.xmllog4j.properties
Extract from build.xml :
<target name="createForDEV">
<delete file="environnement/DEV/${timeStampDay}/${warfile}.war" />
<war destfile="environnement/DEV/${timeStampDay}/${warfile}.war" webxml="environnement/DEV/web.xml" update="true">
<classes dir="build/classes" />
<fileset dir="WebContent">
<exclude name="WEB-INF/web.xml" />
<exclude name="**/Thumbs.db" />
</fileset>
</war>
</target>
<target name="createForINT">
<delete file="environnement/INT/${timeStampDay}/${warfile}.war" />
<war destfile="environnement/INT/${timeStampDay}/${warfile}.war" webxml="environnement/INT/web.xml" update="true">
<classes dir="build/classes" />
<fileset dir="WebContent">
<exclude name="WEB-INF/web.xml" />
<exclude name="**/Thumbs.db" />
<exclude name="**/test.jsp" />
</fileset>
</war>
</target>
I have two configuration files :
for DEV environment
for INT environment
When I make the WAR file, I would like to ignore some files and replace them by others specific files from 'environnement' directory ?
When making WAR in createForDEV target, I would like to take file from environnement/DEV and replace corresponding files
When making WAR in createForINT target, I would like to take file from environnement/INT and replace corresponding files
The trick here is to utilize the duplicate attribute of the war task, and to include multiple fileset elements. The value preserve for duplicate tells it to ignore duplicate entries. The files in the first fileset (from either DEV or INT) will be placed in the war first. Any additional files in WebContent will be included by the second fileset, but any files already included from DEV or INT will be ignored.
<target name="createForDEV">
<delete file="environnement/DEV/${timeStampDay}/${warfile}.war" />
<war destfile="environnement/DEV/${timeStampDay}/${warfile}.war" webxml="environnement/DEV/web.xml" update="true" duplicate="preserve">
<classes dir="build/classes" />
<fileset dir="environnement/DEV">
<exclude name="web.xml" />
</fileset>
<fileset dir="WebContent">
<exclude name="WEB-INF/web.xml" />
<exclude name="**/Thumbs.db" />
</fileset>
</war>
</target>
<target name="createForINT">
<delete file="environnement/INT/${timeStampDay}/${warfile}.war" />
<war destfile="environnement/INT/${timeStampDay}/${warfile}.war" webxml="environnement/INT/web.xml" update="true" duplicate="preserve">
<classes dir="build/classes" />
<fileset dir="environnement/INT">
<exclude name="web.xml" />
</fileset>
<fileset dir="WebContent">
<exclude name="WEB-INF/web.xml" />
<exclude name="**/Thumbs.db" />
<exclude name="**/test.jsp" />
</fileset>
</war>
</target>

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>

Resources