How to get a list of files using Apache ANT - ant

I am basically looking to convert
array=($(find /foo/bar/ -name class.java))
into ant script .

This can be accomplished with a fileset:
<fileset dir="/foo/bar" includes="**/class.java" id="java.files" />

Related

How to copy all contents under one kind of directory in ant?

I'd like to copy files and subdirectories under directory dir_java, but not the dir_java directory itself and all its parent directories. a.k.a I'd like to get all directory dir_com and its subdirectories to todir in ant copy task.
Is there a way to do this?
temp
\--dir_1
|
\--dir_java
|
\--dir_com
\--dir_2
|
\--dir_java
|
\--dir_com
\--dir_3
|
\--dir_foobar
There is no regularity for dir_1,dir_2,dir_3
I tried below
<copy todir="to_dir" verbose="true" overwrite="true">
<fileset dir="temp" includes="*/dir_java/**" />
</copy>
But I got directories like to_dir/dir_1/dir_java/dir_com.
I want directories like to_dir/dir_com
My best trying was to add a exec task with invoking cp command after above copy task, but only for non windows platform:
<exec executable="/bin/sh">
<arg value="-c"/>
<arg value="cp -R to_dir/*/dir_java/* to_dir"/>
</exec>
Are all of the dir_java directories at the same level in the hierarchy? If so, consider using a <cutdirsmapper>...
[cutdirsmapper] strips a configured number of leading directories from the source file name.
After <cutdirsmapper> removes the leading directories, it will preserve whatever remains of the directory structure...
<copy todir="to_dir" verbose="true" overwrite="true">
<fileset dir="temp" includes="*/dir_java/**" />
<cutdirsmapper dirs="2" />
</copy>
Before running Ant
$ find . -type f
./temp/dir_1/dir_java/dir_com/1.txt
./temp/dir_2/dir_java/dir_com/2.txt
./temp/dir_3/dir_foobar/3.txt
After running Ant
$ find . -type f
./temp/dir_1/dir_java/dir_com/1.txt
./temp/dir_2/dir_java/dir_com/2.txt
./temp/dir_3/dir_foobar/3.txt
./to_dir/dir_com/1.txt
./to_dir/dir_com/2.txt
Notice how 1.txt and 2.txt are still under dir_com after the mapping has been applied.

Translate `cp -r $BASEDIR/*/src/main/resource/* $BASEDIR/target/classes/.` to ant's copy command

I tried something like this:
<copy todir="target/classes">
<fileset dir="${basedir}" includes="*/src/main/resource/**"/>
</copy>
but that copies stuff like target/classes/sub-dir/src/main/resource/file.txt and I'd like it to be target/classes/file.txt. How do I do this? Do I have to use a mapper?
EDIT: I added <mapper type="regexp" from="^.*/src/main/resource/(.*)$" to="\1"/> and that seemed to do the trick. (NOTE: I'll clean up the regexp to be tighter later.)
Is that the best way to do it?
A nested <cutdirsmapper> will do the trick...
[cutdirsmapper] strips a configured number of leading directories from the source file name.
After <cutdirsmapper> removes the leading directories, it will preserve whatever remains of the directory structure...
<copy todir="target/classes">
<fileset dir="${basedir}" includes="*/src/main/resource/**"/>
<cutdirsmapper dirs="4"/>
</copy>
Before running Ant
$ find . -type f
./build.xml
./sub-dir/src/main/resource/another-sub-dir/file2.txt
./sub-dir/src/main/resource/file1.txt
After running Ant
$ find . -type f
./build.xml
./sub-dir/src/main/resource/another-sub-dir/file2.txt
./sub-dir/src/main/resource/file1.txt
./target/classes/another-sub-dir/file2.txt
./target/classes/file1.txt
Notice how file2.txt is still under another-sub-dir after the mapping has been applied.

Can ANT parametrize InnoSetup script?

The final step of building our java application (using ANT script) involves Inno Setup to package everything in a nice windows installer.
We are now upgrading our ANT script to generate both a 32-bit and a 64-bit version of our application. Our question is thus: how can we parametrize our Inno Setup config file so that it can generate both a x86 and a x64 version (it would thus be called 2x by the ANT script, with a parameter indicating the x86/x64).
In the Inno Setup config file, there is only 1 line that needs to be changed based on this parameter:
ArchitecturesInstallIn64BitMode=x64
And this is how we call Inno Setup command line from ANT:
<exec executable="C:\Program Files (x86)\Inno Setup 5\iscc.exe">
<arg value="/cc" />
<arg value="${dir.create_setup}/CreateSetup.iss" />
</exec>
Any help / hint on how to do this would be greatly appreciated !
Thanks,
Thomas
Use copy task with filtering, may be used for other dynamic values also.
your iss configfile template has :
ArchitecturesInstallIn64BitMode=#32or64#
your build.xml has :
<filter token="32or64" value="${32or64}"/>
<copy file="foobar.iss" tofile="foobaz.iss" filtering="true" overwrite="true"/>
<exec executable="C:\Program Files (x86)\Inno Setup 5\iscc.exe">
<arg value="/cc" />
<arg value="foobaz.iss" />
</exec>
then start your ant file with userproperty 32or64 like that :
ant -f build.xml -D32or64=x64
or
ant -f build.xml -D32or64=x86
copy task with filtering will replace the token #32or64# with the value of userproperty 32or64, so foobaz.iss has either :
ArchitecturesInstallIn64BitMode=x64
or
ArchitecturesInstallIn64BitMode=x86

Ant: All Directory under Current Directory

I am trying to figure out all the directory under my current directory. Current directory may contain files and directories.
In this scenario, I am looking for existing task which can provide me all the sub-directories under current directory.
You need to use the -path option and the -sbudirectory option, like this:
find . -type d -path "./dir1/*.1" -"subdirectory*name" -o -print
<project name="demo" default="run">
<target name="run">
<dirset id="dirs" dir="${basedir}/.."/>
<pathconvert targetos="unix" property="path.unix" refid="dirs"/>
<echo message="${path.unix}"/>
</target>
</project>

PHP_CodeBrowser directory settings for more than one directory

I have project structure like:
application
tests
docs
library
Zend
My_Lib
I setup my JenkinsCLI to make PHP_Code_Sniffer analysis and so on application and library/My_Lib. And now I need setup PHP_CodeBrowser to browse applivation and library/My_Lib too. But only this two I dont want to setup PHP_CodeBrowser to browse all files in project.
Is it possible and any clue how to do it?
My PHP_CodeBrowser ant task.
<target name="phpcb"
description="Aggregate tool output with PHP_CodeBrowser">
<exec executable="phpcb">
<arg line="--log ${basedir}/build/logs
--source ${source}
--output ${basedir}/build/code-browser" />
</exec>
</target>
Thanks for replies.
From phpcb --help:
-s source, --source=source Path to the project source
code. . . .
Can be given multiple times
So try
<target name="phpcb"
description="Aggregate tool output with PHP_CodeBrowser">
<exec executable="phpcb">
<arg line="--log ${basedir}/build/logs
--source ${source}/application
--source ${source}/library/My_Lib
--output ${basedir}/build/code-browser" />
</exec>
</target>

Resources