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
Related
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" />
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>
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>
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}*" />
I am trying to get ant4eclipse to work and I have used ant a bit, but not much above a simple scripting language. We have multiple source folders in our Eclipse projects so the example in the ant4eclipse documentation needs adapting:
Currently I have the following:
<target name="build">
<!-- resolve the eclipse output location -->
<getOutputpath property="classes.dir" workspace="${workspace}" projectName="${project.name}" />
<!-- init output location -->
<delete dir="${classes.dir}" />
<mkdir dir="${classes.dir}" />
<!-- resolve the eclipse source location -->
<getSourcepath pathId="source.path" project="." allowMultipleFolders='true'/>
<!-- read the eclipse classpath -->
<getEclipseClasspath pathId="build.classpath"
workspace="${workspace}" projectName="${project.name}" />
<!-- compile -->
<javac destdir="${classes.dir}" classpathref="build.classpath" verbose="false" encoding="iso-8859-1">
<src refid="source.path" />
</javac>
<!-- copy resources from src to bin -->
<copy todir="${classes.dir}" preservelastmodified="true">
<fileset refid="source.path">
<include name="**/*"/>
<!--
patternset refid="not.java.files"/>
-->
</fileset>
</copy>
</target>
The task runs successfully, but I cannot get the to work - it is supposed to copy all non-java files over too to emulate the behaviour of eclipse.
So, I have a pathId named source.path which contains multiple directories, which I somehow needs to massage into something the copy-task like. I have tried nesting which is not valid, and some other wild guesses.
How can I do this - thanks in advance.
You might consider using pathconvert to build a pattern that fileset includes can work with.
<pathconvert pathsep="/**/*," refid="source.path" property="my_fileset_pattern">
<filtermapper>
<replacestring from="${basedir}/" to="" />
</filtermapper>
</pathconvert>
That will populate ${my_fileset_pattern} with a string like:
1/**/*,2/**/*,3
if source.path consisted of the three directories 1, 2, and 3 under the basedir. We're using the pathsep to insert wildcards that will expand to the full set of files later.
The property can now be used to generate a fileset of all the files. Note that an extra trailing /**/* is needed to expand out the last directory in the set. Exclusion can be applied at this point.
<fileset dir="." id="my_fileset" includes="${my_fileset_pattern}/**/*">
<exclude name="**/*.java" />
</fileset>
The copy of all the non-java files then becomes:
<copy todir="${classes.dir}" preservelastmodified="true">
<fileset refid="my_fileset" />
</copy>
That will copy the source files over retaining the source directory structure under todir. If needed, the flatten attribute of the copy task can be set to instead make all the source files copy directly to todir.
Note that the pathconvert example here is for a unix fileseystem, rather than windows. If something portable is needed, then the file.separator property should be used to build up the pattern:
<property name="wildcard" value="${file.separator}**${file.separator}*" />
<pathconvert pathsep="${wildcard}," refid="source.path" property="my_fileset">
...
You could use the foreach task from the ant-contrib library:
<target name="build">
...
<!-- copy resources from src to bin -->
<foreach target="copy.resources" param="resource.dir">
<path refid="source.path"/>
</foreach>
</target>
<target name="copy.resources">
<copy todir="${classes.dir}" preservelastmodified="true">
<fileset dir="${resource.dir}" exclude="**/*.java">
</copy>
</target>
If your source.path contains file paths as well then you could the if task (also from ant-contrib) to prevent attempting to copy files for a file path, e.g.
<target name="copy.resources">
<if>
<available file="${classes.dir}" type="dir"/>
<then>
<copy todir="${classes.dir}" preservelastmodified="true">
<fileset dir="${resource.dir}" exclude="**/*.java">
</copy>
</then>
</if>
</target>