I have a directory containing 2 sub directories one of this directory has a file called
dictionary_FR.properties
dictionary_EN.properties
I want to search for these files with ant
Thanks
<fileset dir="loc_of_dir_containing_the_sub_dirs" id="xyz">
<include name="**/dictionary_*.properties"/>
</fileset>
<pathconvert pathsep="," property="my_files" refid="xyz"/>
${my_files} will contain the address of all files named in the pattern dictionary_XX.properties.
Related
I have some code that I revised.
When I try to just run Ant in the directory it fails with missing classes. I can specify the location to the existing classes by using the -lib option to ant. The compile then works fine, however dist ZIP file that is created appears to have missing libraries, as when I try to run it, I see errors relating to missing classes which are the classes that I specified with the -lib option, so this is probably due to the way I have used the -lib option.
How can I force the regular Ant command to include the additional classes specified with the -lib command?
You can write a target that will copy your lib directory/files in your zip file.
Let's say create a temp dir then copy your files then execute target for copying lib directory and then zip temp dir.
<target name="copyLib">
<copy todir="${temp.dir}">
<fileset dir="${lib.dir}">
<include name="*.jar" />
</fileset>
</copy>
</target>
Update paths and call this target into your create zip target.
Compiler task could look like this:
<javac srcdir="${base}/src"
destdir="${base}/classes"
classpath="${base}/lib">
</javac>
And the zip task could look like this:
<zip
destfile="${base}/dist.jar"
basedir="${base}/classes"
includes="..."
excludes="...">
</zip>
So sources are compiled in classes and zipped in a jar, but libraries used for compile are not included in the jar, they are runtime dependencies.
I would suggest that you declare your paths at the top of your ANT scripts as follows, using a fileset.
<path id="build.path">
<fileset dir="lib" includes="*.jar"/>
</path>
Less error prone compared to forcing users to specify the correct "-lib" parameter.
Finally the same fileset can then be used to include the same jars inside the zip file you're creating:
<zip destfile="${dist.dir}/mycode.zip">
..
..
<fileset dir="lib" includes="*.jar"/>
</zip>
I have a Java project with all source files in the src folder. This src folder contains several packages. Some of these packages/folders do not include any Java code. They just contain some images that I use in the application as icons.
The directory structure is similar to the following.
src/
com/
example/
pk1/ # This contains only some Java files.
pk2/ # This will contain some XML and Java code.
im1/ # This subfolder will contain just the images
Now, when I use the javac task, it compiles all the Java files into class files and stores them in the destination directory that I specify. However, an non-java files remain as is in the source folder.
I referred this Question on SO. However, I think in my case I want to include all non-Java resource files (without the need to explicitly specifying what files to be moved) automatically into the build directory (one that contains all the class files) while following the same directory structure as in source.
Pardon me if I am missing something very trivial, but how do I ask Ant to do this?
Use the ant copy task to copy your resources into the classes directory (or wherever your .class files are stored). By default, ant keeps the directory structure.
For example:
<copy todir="classes">
<fileset dir="com/example">
<exclude name="**/*.java"/>
</fileset>
</copy>
Just to complete the previous answer, the block
<copy todir="classes">
<fileset dir="com/example">
<exclude name="**/*.java"/>
</fileset>
</copy>
goes before the task javac
<javac [...]
and inside the target compile
<target name="compile" [...]
then
<target name="compile" [...]>
<copy todir="&&&" [...]>
[...]
</copy>
<javac srcdir="[...]" destdir="&&&" >
</javac>
</target>
in my case the attribute "todir" of tag "copy" it's the same of the attribute "destdir" of tag "javac"; I wanted to detailing the answer in order to give you a resolution as immediate as possible.
We have quite a big tree of source code, parts of it are deployed as two separate jar files. We need an easy control of what goes to which jar.
So far we do this by <exclude name="" /> and <include name="" /> tags, but this is quite inconvenient. The best option would be a separate config file with all the packages listed which we could comment out when needed, say with a '#' character.
Does something similar exist or do we have to write a new ant task that reads such a file and runs a <jar> task?
The best option would bee to seperate the code into different modules which can be build on their own (of course with dependencies to each other). Doing this also makes cyclic dependencies obvious and gives you the chance to optimize your code base.
ANT includes and excludes can be managed with external files and referenced in a fileset using includesfile and excludesfile attributes.
includesfile the name of a file; each
line of this file is taken to be an
include pattern.
excludesfile the
name of a file; each line of this file
is taken to be an exclude pattern.
For example:
<jar destfile="${dist}/lib/app1.jar">
<fileset dir=".">
<includesfile name="app1.properties"/>
</fileset>
</jar>
<jar destfile="${dist}/lib/app2.jar">
<fileset dir=".">
<includesfile name="app2.properties"/>
</fileset>
</jar>
I want to create a fileset that contains the jdk classpath. The problem is that fileset is composed with files in all of the filesystem, not in a specific directory.
For example:
jdk.classpath.includes = ${jdk.home}/lib/**/*.jar, \
${jdk.home}/../Classes/**/*.jar, \
/Library/Java/Extensions/**/*.jar, \
/System/Library/Java/Support/Deploy.bundle/Contents/Resources/Java/**/*.jar
So, when i create the fileset i need to do not specify the dir, or to specify the filesystem root directory, like:
<fileset dir="INeedFileSystemRootDirectory" includes="${jdk.classpath.includes}"/>
How can this be accomplished?
(I didn't find a built-in property to get the filesystem root)
Thanks.
You can make your fileset be composed of many tags, each with a different path:
example:
<fileset dir="${INeedFileSystemRootDirectory}">
<include name="${jdk.home}/lib/**/*.jar"/>
<include name="${jdk.home}/../Classes/**/*.jar"/>
...
</fileset>
Documentation and more examples here.
I'm trying to create a comma-delimited list of files or directories under the current directory. For instance, suppose I have the following folder structure:
Root
-- Directory1
-- Directory2
...
I want to generate a variable or property that contain "Directory1,Directory2." I've tried iterating (using ant-contrib "for" task) over a <dirset dir="." includes="*">, but this generates absolute paths; I've then extracted the file names using the "basename" task, however that in turn generates an output property. Since properties are immutable, what I get in practice is "Directory1,Directory1,..."
Is there a saner way of doing this, or will I have to write a Java extension to do this for me?
The pathconvert task can be used to format a dirset with arbitrary separators:
<dirset id="dirs" dir="." includes="*"/>
<pathconvert dirsep="/" pathsep="," property="dirs" refid="dirs"/>
<echo message="${dirs}"/>
Just confirming Jörn's answer was exactly what I needed (as a starting point) as well.
<dirset id="dirset.sandbox" dir="${sandbox.dir}" includes="*">
<exclude name="output"/>
</dirset>
<pathconvert pathsep=" " property="dirs.sandbox" refid="dirset.sandbox">
<mapper type="flatten"/>
</pathconvert>
<echo message="[*** the sandbox dir list is ${dirs.sandbox} ***]"/>
sandbox.dir is an absolute path similar to /root/build/workspace and contains several subdirectories.
The output is a space-separated list of those directories.