ANT fileset includes in all filesystem - java

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.

Related

ant fixcrlf include/exclude does not work

I am having little problem with fixcrlf from ant. When I try to compile for unix target with fixcrlf filter, it modifies all files in the target folder, no matter of includes and excludes. I expect it should not convert any files that are not in the includes patterns, or at least I could force some files to not be converted via excludes attribute. None of that I tried been able to exclude anything. Even some binary files are modified and as result screwed up.
According to fixcrlf documentation from apache site, these parameters should work like this:
includes: comma- or space-separated list of patterns of files that must be included.
excludes: comma- or space-separated list of patterns of files that must be excluded.
When I run ant with parameters -verbose and -debug, there is no information on that fixcrlf did.
<copy todir="target">
<fileset dir="source1"><exclude name="**/*.bak"/></fileset>
<fileset dir="source2"><exclude name="**/*.bak"/></fileset>
<filterchain>
<fixcrlf srcdir="${src}" includes="**/*.sh,**/*.properties" excludes="**/*.xml" eol="lf" />
</filterchain>
</copy>

Missing class errors running Java built with Ant

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>

Using ant, find files matching a regular expression

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.

How to use Ant include non-java resource files with compiled classes

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.

Ant task for easier deployment

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>

Categories

Resources