Update fileset using ant dynamically - ant

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.

Related

how to excludes and include task in ant script

I want to copy only specific jar file using ant script. It is very easy task but i am not able to do this. I have written the below ant script for this task. I want to excludes all jar file except aopalliance-.jar file. But when i run this script it excludes all the jar file. can someone correct this script .
<?xml version="1.0" encoding="UTF-8"?>
<project default="mvcexample" name="MVCExample" basedir=".">
<property name="web.dir" value="WebContent" />
<property name="webinf.dir" value="WebContent/WEB-INF" />
<property name="lib.dir" value="WebContent/WEB-INF/lib" />
<property name="lib2.dir" value="WebContent/WEB-INF/lib2" />
<target name="copyjar">
<copy todir="${lib2.dir}">
<fileset dir="${lib.dir}" excludes="**/*.jar">
<include name="${lib.dir}/aopalliance-.jar"></include>
</fileset>
</copy>
</target>
</project>
Here's what you need to do:
<target name="copyjar">
<copy todir="${lib2.dir}">
<fileset dir="${lib.dir}">
<!-- <include name="${lib.dir}/aopalliance-.jar"/> -->
<include name="aopalliance-.jar"/> <!-- Don't put the dir name! -->
</fileset>
</copy>
</target>
When you specify a directory, you merely specify the name pattern under that directory. What you were asking for was the jar ${lib.dir}/${lib.dir}/aopalliance-.jar. By the way, is there suppose to be a version number in there somewhere? Like aopalliance-3.4.jar? If so, you need <include name="aopalliance-*.jar"/>.
Also notice that if I used <include .../> instead of <include...></include>.

copy a file from inside of a directory in a filelist

I have a filelist that contains a list of directories. I need to iterate through that filelist and get a file from each directory and copy it to another location.
I was attempting to use filelist in combination with foreach, but foreach does not support filelist. Is there any other way of doing this in ANT?
My attempt with for.
This fails at the <copy> task saying that it cannot find a directory. The directory it is looking for is ${basedir}/bin/ instead of the directory that is first listed in the filelist it is given. I am assuming it is because it is not reading my filelist correctly. My foreach version gives the same error.
<for
list="devBuild"
delimiter=","
param="dirName"
trim="true">
<sequential>
<copy
todir="./apks/"
failonerror="true"
verbose="true">
<fileset dir="#{dirName}/../bin/">
<include name="*/*.apk"/>
</fileset>
</copy>
</sequential>
</for>
Partially Working
Right now, the for loop is working... kinda. The way i want it to work is shown directly below. However, this fails saying the devBuild reference can not be found. However, that filelist is created earlier in a different target and is created with that reference. Why can i not use that reference ID in a different target?
<for
delimiter=","
param="dirName"
trim="true">
<path>
<filelist
refid="devBuild"></filelist>
</path>
<sequential>
<echo>#{dirName}</echo>
<copy
todir="./apks/"
failonerror="true"
verbose="true">
<fileset dir="#{dirName}/../bin/">
<include name="*/*.apk"/>
</fileset>
</copy>
</sequential>
</for>
The following way does work, but i would like to use the reference ID instead of creating the filelist again.
<for
delimiter=","
param="dirName"
trim="true">
<path>
<filelist
id="devBuild"
dir="${basedir}"
files="${build1},${build2},${build3},${build4}"
/>
</path>
<sequential>
<echo>#{dirName}</echo>
<copy
todir="./apks/"
failonerror="true"
verbose="true">
<fileset dir="#{dirName}/../bin/">
<include name="*/*.apk"/>
</fileset>
</copy>
</sequential>
</for>
I figured out the problem. When i am making my call to this other target i was not setting the flag for inheritrefs. This needs to be true so the other target will get the references.

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>.

Ant: copy the same fileset to multiple places

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.

Resources