Ant: copy the same fileset to multiple places - ant

I need an Ant script that will copy one folder to several other places. As a good obedient programmer, I want not to repeat myself. Is there any way of taking a fileset like this:
<copy todir="${target}/path/to/target/1">
<fileset dir="${src}">
<exclude name='**/*svn' />
</fileset>
</copy>
And storing the fileset in a variable so it can be re-used?

Declare an id attribute on the fileset and then reference it in each copy task.
For example:
<project name="foo">
<fileset id="myFileSet" dir="${src}">
<exclude name='**/*svn' />
</fileset>
...
<target name="copy1">
<copy todir="${target}/path/to/target/1">
<fileset refid="myFileSet"/>
</copy>
</target>
<target name="copy2">
<copy todir="${target}/path/to/target/2">
<fileset refid="myFileSet"/>
</copy>
</target>
</project>

Rich's answer is probably better for your specific problem, but the generic way of reusing code in Ant is a <macrodef>.
<macrodef name="copythings">
<attribute name="todir"/>
<sequential>
<copy todir="#{todir}">
<fileset dir="${src}">
<exclude name='**/*svn' />
</fileset>
</copy>
</sequential>
</macrodef>
<copythings todir="/path/to/target1"/>
<copythings todir="/path/to/target2"/>

Upvoted first answer already, but you can also use a mapper to copy to multiple destinations.

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>

Ant war and ear is not generating

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>

Update fileset using ant dynamically

I would like to create a jar file dynamically depending on selected java modules
Here is the part of the ant script which does that.
<property name="modules.selected" value="A,C,F" />
<for list="${modules.selected}" param="module">
<sequential>
<echo>Module chosen ${basedir}/#{module}/src</echo>
<copy todir="${build.dir.src}" overwrite="true">
<fileset dir="${basedir}/#{module}/src">
<include name="**/*.${src.valid.exts}" />
</fileset>
</copy>
</sequential>
</for>
In the above script I am selecting a module and then constructing a directory and copying all the modules present in the directory to a location (build/src).
But I really dont like the logic mentioned above would like to change to include all the required modules in a fileset and use the populated fileset to copy.
Here is the logic I am looking for
<fileset id="required-modules" dir="${basedir}/#{module}/src">
<for list="${modules.selected}" param="module">
<sequential>
<echo>Module chosen ${basedir}/#{module}/src</echo>
<include name="**/*.${src.valid.exts}" />
</sequential>
</for>
</fileset>
<copy todir="${build.dir.src}" overwrite="true">
<fileset refid="required-modules" />
</copy>
Could anyone update the above script to make it work.

Ant release script

Please see the blow script for doing the backup of current ear and replacing it with new version.
<move todir="/usr/local/jboss/server/default/ear_bk/" includeEmptyDirs="yes" verbose="true">
<fileset dir="/usr/local/jboss/release/server/default/deploy/tgr_10_10L_0036.ear" >
<include name="**/*" />
</fileset>
</move>
<copy todir="/usr/local/jboss/server/default/deploy/">
<fileset dir="/usr/local/jboss/release/server/default/deploy/tgr_10_10L_0037.ear"/>
</copy>
</target>
Issue:
1) Only the content of the tgr_10_10L_0036.ear is moved to ear_bk. How to move the tgr_10_10L_0036.ear?.
2) How to copy the complete tgr_10_10L_0037.ear directory to usr/local/jboss/server/default/deploy/ instead only the content ?
Simple quick fix is providing tgr_10_10L_0037.ear in todir
<copy todir="/usr/local/jboss/server/default/deploy/tgr_10_10L_0037.ear/">
<fileset dir="/usr/local/jboss/release/server/default/deploy/tgr_10_10L_0037.ear"/>
</copy>
for this you need to create tgr_10_10L_0037.ear before copying files
<mkdir dir="/usr/local/jboss/server/default/deploy/tgr_10_10L_0037.ear/"/>

How can I process only files that have changed?

At the moment I'm doing this:
<delete dir="${RSA.dir}/file1" />
<copy todir="${RSA.dir}/file1" >
<fileset dir="${CLEARCASE.dir}/file1" />
</copy>
and repeating the same thing for other files - but it takes a long time.
I only want to delete and copy files that have been updated, with their modified date in clearcase later than that in RSA.
How can I do that?
Look into the sync task.
If you want to do it based on file contents, and ignore the timestamp, I think this macro will do that:
<macrodef name="mirror" description="Copy files only if different; remove files that do not exist in dir. This works similiar to robocopy /MIR." >
<attribute name="dir"/>
<attribute name="todir"/>
<sequential>
<copy overwrite="true" todir="#{todir}">
<fileset dir="#{dir}">
<different targetdir="${todir}"/>
</fileset>
</copy>
<delete includeemptydirs="true">
<fileset dir="#todir}">
<present targetdir="${dir}" present="srconly"/>
</fileset>
</delete>
</sequential>
</macrodef>
You need to use a selector on a FileSet. Something like this:
<fileset dir="${your.working.dir}/src/main" includes="**/*.java">
<different targetdir="${clean.clearcase.checkout}/src/main"
ignoreFileTimes="false"
ignoreContents="true" />
</fileset>
That compares your working dir to a clean checkout you have elsewhere, and returns the files whose last modified times have changed. You can use the fileset as the argument for a <delete> or a <copy>.

Resources