What does pathelement signify in a buildfile? - ant

What does pathelement signify?
<path id="master-classpath">
<fileset dir="${web.dir}/WEB-INF/lib">
<include name="*.jar"/>
</fileset>
<pathelement path="${build.dir}"/>
</path>

See path-like structures in the Ant documentation. A pathelement is part of a path.
If you think of a path as being a list of locations - e.g. directories - you can compose a path out of locations, or shorter paths, or a combination of both:
Path X: a\1:b\2 # Two locations / one path
Path Y: c\3 # One location / one path
Path XY: a\1:b\2:c\3 # Three locations / one path
A pathelement may specify a path (i.e. one or more locations), or a single location:
<path id=X">
<pathelement path="a\1:b\2"/>
</path>
<path id=Y">
<pathelement location="c\3"/>
</path>

Related

How to get all the filenames from a directory using ant

Using ant I want to get all the filenames from a directory and create a property with value as comma separated file names.
Example: If we have 3 files in a directory (i.e. 1.txt, 2.txt, 3.txt) then we have to create a property & its value should be 1.txt,2.txt,3.txt.
Thanks,
Mansoor MD.
First, you need to create a path with the files:
<path>
<fileset dir="${dir.name}">
<include name="*"/>
</fileset>
</path>
This can be combined with a <pathconvert> task:
<pathconvert pathsep=","
property="my.files">
<path>
<fileset dir="${dir.name}">
<include name="*"/>
</fileset>
</path>
</pathconvert>
The property ${my.files} will contain a comma separated list of files.
If you prefer, you can do this in two steps:
<path id="mypath">
<fileset dir="${dir.name}">
<include name="*"/>
</fileset>
</path>
<pathconvert pathsep=","
property="my.files"
refid="mypath"/>
Word 'o Warning: It will also contain the full path to these files.

How to add to classpath all classes from set of directories in ant?

How to add to classpath all classes from set of directories?
I have following property:
class.dirs=lib1dir,lib2dir,lib3dir
There are classes under these directories.
Is it possible to add all classes under these directories to classpath?
Something like:
<classpath>
<dirset dir="${root.dir}" includes="${class.dirs}/**/*.class"/>
</classpath>
or
<classpath>
<pathelement location="${class.dirs}" />
</classpath>
But this example does not work, of course.
You can set up a path to include all .class files from your specific directories:
<path id="mypath">
<fileset dir="${root.dir}">
<include name="lib1dir/**/*.class lib2dir/**/*.class lib3dir/**/*.class"/>
</fileset>
</path>
However, if you want to use this path as a classpath, you only need to reference the root folders, otherwise you will get ClassNotFoundErrors as the package names translate into directories:
<path id="build.classpath">
<dirset dir="${root.dir}">
<include name="lib1dir lib2dir lib3dir"/>
</dirset>
</path>
Then reference the path by its id when using (e.g. for classpath):
<javac srcdir="${src.dir}" destdir="${build.dir}" classpathref="build.classpath" />

how to load all supporting jar in classpath in build.xml of apache ant

I have make build.xml file for each projects which create jar,war,ear files
to call this build.xml i have make one more master build.xml
Now I have configure this project in Hudson
The problem is while building project from hudson , hudson can't convert relative path to absolute path.
So I want to do some global declaration and add all jar path and load all jar path in build.xml 's classpath
<fileset dir="${class.dir}" includes="**/*.jar"/>
<fileset dir="${AllJar.dir}" includes="**/*.jar"/>
The key is to define individual filesets with some id and define your master path with refid
Quote from the link:
A path-like structure can include a reference to another path-like
structure (a path being itself a resource collection) via nested
elements:
<path id="base.path">
<pathelement path="${classpath}"/>
<fileset dir="lib">
<include name="**/*.jar"/>
</fileset>
<pathelement location="classes"/>
</path>
<path id="tests.path" cache="true">
<path refid="base.path"/>
<pathelement location="testclasses"/>
</path>
In this test.path includes base.path which in turn include ${classpath} and all jars from lib folder.

What's the difference between a nested path and fileset?

I have been googling for the "Differences between fileset and path" article for some time, but have found nothing useful.
For example, what is the difference between the following (say, there is a someDir directory, which contains .jar files and has no subdirectories):
<path id="somePathId">
<pathelement path="someDir"/>
</path>
<path id="someId">
<path refid="somePathId" />
</path>
and
<path id="someId">
<fileset dir="someDir">
<include name="*.*">
</fileset>
</path>
?
They are used in different situations.
fileset is used to specify a group of files. You can use selectors and patternsets to get only the files you want.
classpath is used to specify classpath references. classpath can be specified with a single jar (location="..."), a ; or : separated list of jars (path="...") or with nested resource collections (like fileset).
Also if you want to debug them, it is different:
<echo message="Build-path: ${toString:build-path}" />
vs
<property name="debug.classpath" refid="classpath"/>
<echo message="Classpath = ${debug.classpath}"/>
As for your scripts,
<path id="somePathId">
<pathelement location="someDir"/>
</path>
I did not test it but according to the documentation path= expects a ; or : separated list of jars. This is not the same as your second example.
The major difference between a <path> and a <fileset> is that in <fileset> you can specify if you want to include or exclude certain type of files (Basically, its a group of files within a path... not necessary all the files), for eg:
<path id="someId">
<fileset dir="someDir">
<include name="*.java">
<include name="*.properties">
</fileset>
</path>

How does ant treat a pathelement that does not exist?

How does ant behave if I define a path with a pathelement which points to a non-existent directory?
<path id="foo.bar">
<pathelement location="this/might/not/exist/">
</path>
The scenario is that the ant file is used for several projects - some have this additional folder, and some do not.
Does ant just ignore it, or does it fail?
It depends on the context.
When used as a classpath for the javac task, the missing directories are simply ignored:
This task will drop all entries that point to non-existent
files/directories from the classpath it passes to the compiler.
But if you use a path containing a non-existent directory, say as the source for a copy, you'll get an error.
For example, here directories 'one' and 'three' exist, but 'two' does not:
<path id="mypath">
<pathelement path="one" />
<pathelement path="two" />
<pathelement path="three" />
</path>
<copy todir="dest">
<path refid="mypath" />
</copy>
BUILD FAILED
/.../build.xml:14: Warning: Could not find resource file ".../two" to copy.
You could use a dirset to filter out the missing items perhaps:
<pathconvert property="dirs.list" pathsep="," refid="mypath">
<map from="${basedir}/" to="" />
</pathconvert>
<dirset id="exists.dirs" dir="." includes="${dirs.list}" />
<copy todir="dest">
<dirset refid="exists.dirs" />
</copy>
[copy] Copied 2 empty directories to 2 empty directories under /.../dest

Resources