How can I process only files that have changed? - ant

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

Related

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.

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 exclude files from a reference path in Ant?

In a project we have several source paths, so we defined a reference path for them:
<path id="de.his.path.srcpath">
<pathelement path="${de.his.dir.src.qis.java}"/>
<pathelement path="${de.his.dir.src.h1.java}"/>
...
</path>
Using the reference works fine in the <javac> tag:
<src refid="de.his.path.srcpath" />
In the next step, we have to copy non-java files to the classpath folder:
<copy todir="${de.his.dir.bin.classes}" overwrite="true">
<fileset refid="de.his.path.srcpath">
<exclude name="**/*.java" />
</fileset>
</copy>
Unfortunately, this does not work because "refid" and nested elements may not be mixed.
Is there a way I can get a set of all non-java files in my source path without copying the list of source paths into individual filesets?
Here's an option. First, use the pathconvert task to make a pattern suitable for generating a fileset:
<pathconvert pathsep="/**/*,"
refid="de.his.path.srcpath"
property="my_fileset_pattern">
<filtermapper>
<replacestring from="${basedir}/" to="" />
</filtermapper>
</pathconvert>
Next make the fileset from all the files in the paths, except the java sources. Note the trailing wildcard /**/* needed as pathconvert only does the wildcards within the list, not the one needed at the end:
<fileset dir="." id="my_fileset" includes="${my_fileset_pattern}/**/*" >
<exclude name="**/*.java" />
</fileset>
Then your copy task would be:
<copy todir="${de.his.dir.bin.classes}" overwrite="true" >
<fileset refid="my_fileset" />
</copy>
For portability, instead of hard-coding the unix wildcard /**/* you might consider using something like:
<property name="wildcard" value="${file.separator}**${file.separator}*" />

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