How to rename files and folders with Ant - ant

How to rename many files and folders with Ant? For files I know I can do it like this; question
How to do the same thing for folders?
For e.g.
Set of folders (Input)
com.google.appengine.eclipse.sdkbundle_1.5.2.r37v201107211953
com.google.gdt.eclipse.designer.doc.user_2.3.2.r37x201107161328
com.google.gdt.eclipse.designer.hosted.2_0.webkit_win32_2.3.2.r37x201107161253
org.eclipse.acceleo.common_3.1.0.v20110607-0602.jar
Output:
com.google.appengine.eclipse.sdkbundle_1.5.2
com.google.gdt.eclipse.designer.doc.user_2.3.2
com.google.gdt.eclipse.designer.hosted.2_0.webkit_win32_2.3.2
org.eclipse.acceleo.common_3.1.0.jar

For complex operations I use the groovy ANT task.
The following example will rename your files and directories, using regular expressions:
<project name="demo" default="rename">
<target name="bootstrap">
<mkdir dir="${user.home}/.ant/lib"/>
<get dest="${user.home}/.ant/lib/groovy-all.jar" src="http://search.maven.org/remotecontent?filepath=org/codehaus/groovy/groovy-all/2.0.6/groovy-all-2.0.6.jar"/>
</target>
<target name="rename">
<taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy"/>
<fileset id="filesToBeRenamed" dir="build"/>
<dirset id="dirsToBeRenamed" dir="build"/>
<groovy>
project.references.filesToBeRenamed.each {
String origName = it
String newName = origName.replaceAll(/_[0-9\.]+[a-z0-9\-]+/, "")
if (origName != newName) {
ant.move(file:origName, tofile:newName, verbose:"true")
}
}
project.references.dirsToBeRenamed.each {
String origName = it
String newName = origName.replaceAll(/_[0-9\.]+[a-z0-9\-]+/, "")
if (origName != newName) {
ant.move(file:origName, tofile:newName, verbose:"true")
}
}
</groovy>
</target>
</project>
NOTES:
The "bootstrap" target only needs to be run once. It will download the groovy jar from Maven central

This will move the directory and all of its sub-directories and files.
<move todir="${toDir}">
<fileset dir="${fromDir}"/>
</move>

Take a look at the Ant-Contrib tasks. One is the <for> task. This will allow you to specify multiple directories, directory patterns, etc. This way, you can loop through the directories and files.
You can copy the files and directories to another location and use the Mapper to map the file names. (<move> task will also work with mappers.)
I recommend you download the Ant-Contrib Jar file to the directory ${basedir}/antlib/ac in your project. Then do this in the beginning of your build file:
<taskdef resource="net/sf/antcontrib/antlib.xml">
<classpath>
<fileset dir="${basedir}/antilb/ac"/>
</classdef>
<taskdef>
This will define the ant-contrib tasks and allow you to use them. If you're using a version control system and check everything in, someone can checkout your project and do a build without having to install Ant-Contrib first.

Related

ant to exclude some files

I am trying to use ant to copy files from sourceDir to destDir, and I also don't want to copy some files, I keep the file names I want to exclude in file: excludelist.txt. How could I implements this is an ant target ?
Use copy task with nested fileset using nested exludesfile.
From ant manual FileSet :
excludesfile => the name of a file; each line of this file is taken to
be an exclude pattern.
snippet :
<target name="foo">
<copy todir="path/to/destDir">
<fileset dir="path/to/sourceDir">
<excludesfile name="excludelist.txt"/>
</fileset>
</copy>
</target>

Move a ant dir project after the ant or subant task completes

Currently i'm using a shell script to do the following:
cd myproject1/
ant
cd ..
if grep 'sucessful' myproject/buil.log then move myproject ../backup/today/
And so on for myproject2, myproject3.
If some error happens, the project stay in the current dir to be reprocessed but the whole process continues.
I want to migrate this process to an ant build script but i have no clue on how to do this.
I have looked at ant and subant tasks. Ant looks more suitable to the job but i can't find a way to loop through a directory list using ant and move task togheter, checking if the ant task completes or not.
Thank you.
Checkout this answer:
running specific target in different ant scripts in different directories
I recommend that your submodule builds should throw an error rather than try and duplicate the log parsing logic.
Update
If this is designed to support deployment, perhaps you should consider a groovy script?
Would better support exception conditions:
def ant = new AntBuilder()
scanner = ant.fileScanner {
fileset(dir:".", includes:"test*/build.xml")
}
scanner.each { f ->
try {
ant.ant(antfile:f)
}
catch (e) {
ant.mkdir(dir:"backup")
ant.move(todir:"backup", file:f.parent)
}
}
Groovy has excellent ANT integration and can also be embedded within your ANT build:
<target name="run">
<taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy" classpathref="build.path"/>
<fileset id="buildFiles" dir="." includes="test*/build.xml"/>
<groovy>
project.references.buildFiles.each {
def f = new File(it.toString())
try {
ant.ant(antfile:f)
}
catch(e) {
ant.mkdir(dir:"backup")
ant.move(todir:"backup", file:f.parent)
}
}
</groovy>
</target>
Something along these lines may be what you're looking for :
<target name="compile" >
<javac srcdir="${src.dir}" destdir="${class.dir}" />
</target >
<target name="copy" depends="compile" >
<mkdir dir="${dest.dir}" />
<copy todir="${dest.dir}" overwrite="true">
<fileset dir="${class.dir}" includes="**" />
<fileset dir="${src.dir}" includes="**" />
...
</copy>
</target>

How to avoid running ant tasks on source files that have not changed?

I have an ant task that executes some command on a list of files.
I would like, on consecutive builds, to avoid from re-running the command on files that have passed the command with success and haven't changed.
For example: (here the command is xmllint)
<target name="xmllint-files">
<apply executable="xmllint">
<srcfile/>
<fileset dir="." includes="*.xml">
<modified/>
</fileset>
</apply>
</target>
The problem is that even the files where xmlint fails are considered as modified and therefore xmllint will not be re-run on them on consecutive builds. Obviously, this is not the desired behavior.
Two remarks:
I am looking for a general solution and not only a solution for xmllint.
I want to solve the problem totally inside ant without creating
external scripts.
This code uses the Groovy ANT task to do the following:
Implement a custom groovy selector, selecting the XML files to be processed based on a MD5 checksum check.
Invoke xmllint on each file and store it's checksum upon successful completion (This prevents re-execution of xmllint unless the file's contents are changed.
Example:
<project name="demo" default="xmllint">
<!--
======================
Groovy task dependency
======================
-->
<path id="build.path">
<pathelement location="jars/groovy-all-1.8.6.jar"/>
</path>
<taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy" classpathref="build.path"/>
<!--
==============================================
Select files to be processed
MD5 checksums located in "checksums" directory
==============================================
-->
<target name="select-files">
<fileset id="unprocessedfiles" dir=".">
<include name="*.xml"/>
<exclude name="build.xml"/>
<scriptselector language="groovy" classpathref="build.path">
def ant = new AntBuilder()
ant.checksum(file:filename, toDir:"checksums", verifyProperty:"isMD5ok")
self.selected = (ant.project.properties.isMD5ok == "false") ? true : false
</scriptselector>
</fileset>
</target>
<!--
=============================================================
Process each file
Checksum is saved upon command success, prevents reprocessing
=============================================================
-->
<target name="xmllint" depends="select-files">
<groovy>
project.references.unprocessedfiles.each { file ->
ant.exec(executable:"xmllint", resultproperty:"cmdExit") {
arg(value:file)
}
if (properties.cmdExit == "0") {
ant.checksum(file:file.toString(), toDir:"checksums")
}
}
</groovy>
</target>
</project>
Note:
This complex requirement cannot be implemented using the original apply ANT task. One call to xmllint command might succeed whereas another might fail.
A subdirectory called "checksums" is created to store the MD5 checksum files.
The groovy jar can be downloaded from Maven Central
Original answer
Use the ANT modified selector
<project name="demo" default="xmllint">
<target name="xmllint">
<apply executable="xmllint">
<srcfile/>
<fileset dir="." includes="*.xml">
<modified/>
</fileset>
</apply>
</target>
</project>
A property file called "cache.properties" will be created in the build directory. It records file digests, used determine if the file has been changed since the last build run.

running specific target in different ant scripts in different directories

We have a large amount of apps. They all have a build.xml file located in the projects base directory. I am trying to create an ant script that will go through and call a specific target on each of the build.xml files in all the projects.
Here are the issues:
Some of the projects are in deeper directories than others.
Only some of the projects need to be built at a time.
I was trying to use subant + antfile and defining a CSV of file paths in a properties file, but this did not work. Below is what i have and the error i am getting.
If there is a better way to do this or you know what my problem is, please let me know! Thanks!
This is the property defined in a property file. I am wanting the person running the script to add the file paths in here that are relative to the current location of the script they are running.
projects.to.build=
This is the subant task i am trying to use in the main build script.
<filelist
id="projectNames"
dir="${basedir}"
files="${projects.to.build}"
/>
<target name="debugAll" description="Builds all the projects listed in the projectNames.properties file.">
<subant target="debug" antfile="${projects.to.build}">
</subant>
</target>
Here is the error i get when i try to run the build script when there are projects defined in the properties file. I am using the relative path. For example: ..\Apps\AnApp1\build.xml,..\Apps\AnApp2\build.xml,..\OtherApps\foo\AnotherApp1\build.xml
"No Build Path Specified" (at my subant task)
You specified the antfile attribute, so ANT was expecting to a single build.xml file.
The subant documentation describes how you can use a fileset as child parameter.
Here's an example:
<project name="Subant demo" default="run-debug-target">
<target name="run-debug-target">
<subant target="debug">
<fileset dir="." includes="**/build.xml" excludes="build.xml"/>
</subant>
</target>
</project>
Update
Alternatively a filelist could be used:
<project name="Dry run" default="run">
<target name="run">
<subant target="test">
<filelist dir="projects" files="one/build.xml,two/build.xml,three/build.xml,four/build.xml"/>
</subant>
</target>
</project>
Processing the following build files:
projects/one/build.xml
projects/two/build.xml
projects/three/build.xml
projects/four/build.xml
Is it possible to run the target in the all the build files concurrently ?
E.g.
<project name="Dry run" default="run">
<target name="run">
<subant target="test">
<filelist dir="projects" files="one/build.xml,two/build.xml,three/build.xml,four/build.xml"/>
</subant>
</target>
</project>
In this example, is there any way to run target "test" present in all the build files (one/build.xml,two/build.xml,three/build.xml,four/build.xml) concurrently ?

Exclude empty directories with Jar Jar Links

I am using an Ant task from Jar Jar Links to embed classes from a 3rd-party jar file (objenesis) in my distributable jar file (example.jar). Jar Jar will translate classes from the original package (org.objenesis) to one of my choosing.
It works but it leaves empty directories in the distributable jar.
Here is a simplified build.xml:
<target name="jar" depends="compile">
<taskdef name="jarjar" classname="com.tonicsystems.jarjar.JarJarTask"
classpath="lib/jarjar-1.1.jar"/>
<jarjar jarfile="dist/example.jar" excludes="org.objenesis.**">
<fileset dir="build/classes/main"/>
<zipfileset src="lib/objenesis-1.2.jar"/>
<rule pattern="org.objenesis.**" result="org.thirdparty.#1"/>
</jarjar>
</target>
A sample of contents of the example.jar includes (as expected):
org/thirdparty/Objenesis.class
org/thirdparty/ObjenesisBase.class
but also these empty directories (undesirable):
org/objenesis/
org/objenesis/instantiator/
org/objenesis/instantiator/basic/
My question: how to I exclude these empty directories?
I tried the "zap" option (listed in the doc), but that didn't work.
This appears to be a known issue in Jar Jar, listed in their issue tracker: http://code.google.com/p/jarjar/issues/detail?q=empty&id=32
Given that this was raised almost three years ago and doesn't appear to have got any traction, I suppose your options are to contribute a fix, or to work around it.
An example Ant target to work around it, taking advantage of Ant's support for removing empty directories on copy, would be:
<target name="unpolluted-jarjar" description="JarJars without empty directories">
<taskdef name="jarjar" classname="com.tonicsystems.jarjar.JarJarTask" classpath="${location.lib}/build/jarjar-1.2.jar"/>
<jarjar basedir="${location.classes}" destfile="${location.dist.binaries}/my-app.jar">
<zipfileset src="${location.lib}/shipped/dependency.jar"/>
<rule pattern="com.example.dependency.**" result="com.example.my-app.jarjar.com.example.dependency.#1"/>
</jarjar>
<mkdir dir="${location.dist.binaries}/exploded"/>
<unzip src="${location.dist.binaries}/my-app.jar" dest="${location.dist.binaries}/exploded/my-app.jar"/>
<copy includeemptydirs="false" todir="${location.dist.binaries}/unpolluted/my-app.jar">
<fileset dir="${location.dist.binaries}/exploded/my-app.jar"/>
</copy>
<jar destfile="${location.dist.binaries}/my-app-unpolluted.jar">
<fileset dir="${location.dist.binaries}/unpolluted/my-app.jar"/>
</jar>
</target>
It's a bit grungy, but it achieves what you want.

Resources