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

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>

Related

How do I copy files which have same name but different suffix in ant copy task?

I have written the below script which is copying files which were changed after the specified time. What i also want is to get the same files which has different suffix with these files.
<project name="copy latest files test" basedir="." xmlns:sf="antlib:com.salesforce">
<taskdef resource="net/sf/antcontrib/antlib.xml">
<classpath>
<pathelement location="C:/apache-ant-1.9.6/lib/ant-contrib-1.0b3.jar"/>
</classpath>
</taskdef>
<property name="src.dir" value="retrieveUnpackaged"/>
<property name="dest.dir" value="tempFolder"/>
<!-- Copy contents of classes folder from source folder -->
<target name="moveFolders" >
<copy toDir="${dest.dir}">
<fileset dir="${src.dir}">
<scriptselector language="javascript">
import(java.io.file);
self.setselected(true);
</scriptselector>
<include name="*/**"/>
<date datetime="04/20/2016 10:35 AM" when="after"/>
</fileset>
</copy>
</target>
I was trying to use , but couldnt figure it out.
The files that are getting copied will have another file in the same folder ending with "-meta.xml" as suffix. What I want is to get those files together as well.

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" />

Ant script: Prevent duplication of JAR in javac-classpath war-lib

I have a ANT script and I have a lot of duplicated path to same set JAR files.
But there is so many double wording in the classpath and also in the war element.
<path id="my.classpath">
<pathelement location="folderA/subFolderA/1.0/A.jar"/>
<pathelement location="folderC/subFolderB/1.0/B.jar"/>
<pathelement location="folderF/subFolderZ/2.0/Z.jar"/>
<pathelement location="compile/subFolderX/1.0/onlyForJavac.jar"/>
</path>
....
<javac ...>
<classpath refid="my.classpath" />
</javac>
....
<war ...>
<lib file="folderA/subFolderA/1.0/A.jar"/>
<lib file="folderC/subFolderB/1.0/B.jar"/>
<lib file="folderF/subFolderZ/2.0/Z.jar"/>
<lib file="moreFolderF/subFolderZ/2.0/additionFile.jar"/>
<lib file="moreFolderF/subFolderZ/2.0/additionRuntimeFile.jar"/>
</war>
I want to summary them into ONE list which is easier to keep update.
But I am blocked as I have no idea how to share a path-like-structure with a fileset-like-structure.
Since Ant 1.8.0 there is a new resource collection - mappedresources that
can be used in place of the war task lib element.
So, the task might look like this (pretty much straight from the docs):
<war ... >
<mappedresources>
<restrict>
<path refid="my.classpath"/>
<type type="file"/>
</restrict>
<chainedmapper>
<flattenmapper/>
<globmapper from="*" to="WEB-INF/lib/*"/>
</chainedmapper>
</mappedresources>
</war>
This feature was added to resolve a long-standing feature request to make
the task flatten jars when deploying to WEB-INF/lib.
previous answer:
Although you can't easily convert a path to a fileset with vanilla Ant, you can go the other way.
So one option would be to define your jars in a fileset, and derive the path from it.
Something like this perhaps:
<fileset id="my.fileset" dir="${basedir}">
<include name="folderA/subFolderA/1.0/A.jar"/>
<include name="folderC/subFolderB/1.0/B.jar"/>
<include name="folderF/subFolderZ/2.0/Z.jar"/>
<include name="moreFolderF/subFolderZ/2.0/additionFile.jar"/>
<include name="moreFolderF/subFolderZ/2.0/additionRuntimeFile.jar"/>
</fileset>
<path id="my.classpath">
<fileset refid="my.fileset" />
</path>
<!-- javac stays the same -->
<war ...>
<lib refid="my.fileset" />
</war>
Another possibility is to use the ant-contrib pathtofileset task.
Another solution, possibly 'not the best' will be to place required jar file in WEB-INF/lib, and then set the classpath from there.
<path id="compile.classpath">
<fileset dir="${lib.dir}" includes="*.jar"/>
</path>
When its time to build the war, you need not worry about the <lib> at all, as jars are already placed in WEB-INF/lib folder.
<war destfile="${dist.dir}/${project.name}.war" webxml="${web.dir}/WEB-INF/web.xml">
<fileset dir="${web.dir}"/>
<classes dir="${build.dir}/classes"/>
</war>

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

How can I exclude files from a reference path in Ant?

In a project we have several source paths, so we defined a reference path for them:
<path id="de.his.path.srcpath">
<pathelement path="${de.his.dir.src.qis.java}"/>
<pathelement path="${de.his.dir.src.h1.java}"/>
...
</path>
Using the reference works fine in the <javac> tag:
<src refid="de.his.path.srcpath" />
In the next step, we have to copy non-java files to the classpath folder:
<copy todir="${de.his.dir.bin.classes}" overwrite="true">
<fileset refid="de.his.path.srcpath">
<exclude name="**/*.java" />
</fileset>
</copy>
Unfortunately, this does not work because "refid" and nested elements may not be mixed.
Is there a way I can get a set of all non-java files in my source path without copying the list of source paths into individual filesets?
Here's an option. First, use the pathconvert task to make a pattern suitable for generating a fileset:
<pathconvert pathsep="/**/*,"
refid="de.his.path.srcpath"
property="my_fileset_pattern">
<filtermapper>
<replacestring from="${basedir}/" to="" />
</filtermapper>
</pathconvert>
Next make the fileset from all the files in the paths, except the java sources. Note the trailing wildcard /**/* needed as pathconvert only does the wildcards within the list, not the one needed at the end:
<fileset dir="." id="my_fileset" includes="${my_fileset_pattern}/**/*" >
<exclude name="**/*.java" />
</fileset>
Then your copy task would be:
<copy todir="${de.his.dir.bin.classes}" overwrite="true" >
<fileset refid="my_fileset" />
</copy>
For portability, instead of hard-coding the unix wildcard /**/* you might consider using something like:
<property name="wildcard" value="${file.separator}**${file.separator}*" />

Resources