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>
Related
I am new to this ant I wrote athe following build.xml file for generating war and ear. it is showing build successfull. but it not generating any war/ear file. I mentioned my script below. please help me what changes i have to do..
Thanks in advance....
<property name="src" value="src"/>
<property name="dst" value="web"/>
<property name="classes" value="WEB-INF/classes"/>
<property name="archiveName" value="medcardets"/>
<property name="archive" value="BuildArchive"/>
<fileset id="lib" dir="${dst}/WEB-INF/lib">
<include name="*.jar"/>
</fileset>
<fileset id="war.file" dir="/">
<include name="${archiveName}.war"/>
</fileset>
<fileset id="ear.file" dir="/">
<include name="${archiveName}.war"/>
</fileset>
<fileset id="lib.rules" dir="${dst}/WEB-INF/lib/rules">
<include name="*.jar"/>
</fileset>
<fileset id="lib.j2ee" dir="j2ee">
<include name="*.jar"/>
</fileset>
<target name="clear">
<delete dir="${dst}/${classes}"/>
<delete dir="${archive}"/>
</target>
<target name="build" depends="clear">
<mkdir dir="${dst}/${classes}"/>
<javac srcdir="${src}" destdir="${dst}/${classes}" debug="on" debuglevel="lines,vars,source">
<classpath>
<fileset refid="lib"/>
<fileset refid="lib.rules"/>
<fileset refid="lib.j2ee"/>
</classpath>
</javac>
<copy todir="${dst}/${classes}">
<fileset dir="${src}">
<exclude name="**/CVS"/>
<exclude name="**/*.java"/>
</fileset>
</copy>
</target>
Your question implies that you have written code that you expect to generate a WAR, but you have no instruction to do so. I see from your comment you are actually asking what the command should be.
Most important: I hope we are all new to things, after 35 years in the industry I learn something every day. Why did you ask the question rather than google? Learning to search is the most important skill. Google ant task WAR gives this task with an example:
<war destfile="myapp.war" webxml="src/metadata/myapp.xml">
<fileset dir="src/html/myapp"/>
<fileset dir="src/jsp/myapp"/>
<lib dir="thirdparty/libs">
<exclude name="jdbc1.jar"/>
</lib>
<classes dir="build/main"/>
<zipfileset dir="src/graphics/images/gifs"
prefix="images"/>
</war>
and a similar entry for EAR:
<ear destfile="${build.dir}/myapp.ear" appxml="${src.dir}/metadata/application.xml">
<fileset dir="${build.dir}" includes="*.jar,*.war"/>
</ear>
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>
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>
I have 2 different filesets defined in Ant as follows:
<fileset id="fileset1" dir="${classes.dir}">
</fileset>
<zipfileset id="fileset2" src="myArchive.zip" includes="**/*.class">
</zipfileset>
I want to create a third fileset which is the union of both the above filesets
<fileset id="merged">
</fileset>
Can someone tell me how to do this ? Is it even possible to do something like that ?
Thanks in advance!
One way to do this is with Ant resource collections, in particular a union.
<fileset id="fileset1" dir="${classes.dir}" />
<zipfileset id="fileset2" src="myArchive.zip" includes="**/*.class" />
<union id="onion">
<resources refid="fileset1" />
<resources refid="fileset2" />
</union>
Then you can refer to the 'onion' anywhere you might use a fileset, e.g.
<copy todir="dest">
<resources refid="onion" />
</copy>
I recommend using generic resources elements rather than filesets for maximum flexibility.
Try this: I think it should work, since <fileset> is an implicit <patternset>.
<fileset id="fileset1" dir="${classes.dir}">
</fileset>
<zipfileset id="fileset2" src="myArchive.zip" includes="**/*.class">
</zipfileset>
EDIT: odd. This perhaps?
<patternset id="merged">
<patternset refid="fileset1" />
<patternset refid="fileset2" />
</patternset>
problem with fileset is, that it requires a directory as a base upon it applies the patternset. Which means you have to find a common base directory that is shared by all filesets.
A <pathconvert> Task can take filesets via refid. You can put several filesets (e.g. from various build targets to assemble a compound set in a root/main target for a modular build environment):
<project name="root" basedir="." xmlns:if="ant:if" xmlns:unless="ant:unless">
<!--
it's important to take the xmlns:features in your project head
otherwhise this code won't work
-->
<target name="init">
<!-- set some common prerequisites -->
<property name="prerequisite.property.xyz" value="xyz" />
</target>
<target name="targetA" depends="init">
<fileset dir="${common.basedir}${file.separator}${targetA.subdir}" id="targetA.fileset">
<include name="**/*.html" />
</fileset>
<property name="targetA.fileset.exists" value="true" />
</target>
<target name="targetB" depends="init">
<fileset dir="${common.basedir}${file.separator}${targetB.subdir}" id="targetB.fileset">
<include name="**/*.java" />
</fileset>
<property name="targetB.fileset.exists" value="true" />
</target>
<target name="targetC" depends="init">
<fileset dir="${common.basedir}${file.separator}${targetC.subdir}" id="targetC.fileset">
<include name="**/*.class" />
</fileset>
<property name="targetC.fileset.exists" value="true" />
</target>
<target name="root" depends="init">
<pathconvert property="all.files.as.commaseparated.path" pathsep="," dirsep="/">
<fileset refid="targetA.fileset" if:true="${targetA.fileset.exists}" />
<fileset refid="targetB.fileset" if:true="${targetB.fileset.exists}" />
<fileset refid="targetC.fileset" if:true="${targetC.fileset.exists}" />
<map from="${common.basedir}/" to="" />
</pathconvert>
<!-- assemble new fileset from paths as comma separated property string -->
<fileset id="new.refid" dir="${common.basedir}" includes="${all.files.as.commaseparated.path}" />
</target>
</project>
This can be called via command line like:
ant targetA targetB targetC root
or
ant targetA root
Be aware that root is always the last target being called.
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>