I am trying to refactor an ant build.xml file to reduce duplication. Previously the file had a bunch of targets something like this:
<war ...>
<lib dir=${lib}">
<include name="foo.jar"/>
<include name="bar.jar"/>
<include name="qux.jar"/>
</lib>
...
</war>
<jar ...>
...
<manifest>
<attribute name="Class-Path" value="foo.jar bar.jar baz.jar"/>
</manifest>
</jar>
Despite the ant documentation, I got it to the point where it looks more like this:
<fileset id="commonLibs1" dir="${lib}">
<include name="foo.jar"/>
<include name="bar.jar"/>
</fileset>
<union id="clientLibs">
<fileset refid="commonLibs1"/>
<fileset dir="${lib}">
<include name="baz.jar"/>
</fileset>
</union>
<war ...>
<lib dir=${lib}">
<include name="foo.jar"/>
<include name="bar.jar"/>
<include name="qux.jar"/>
</lib>
...
</war>
<manifestclasspath property="tmpClassPath" jarfile="./placeholdername.jar">
<classpath>
<resources refid="clientLibs"/>
</classpath>
</classpath>
<jar ...>
...
<manifest>
<attribute name="Class-Path" value="${tmpClasspath}"/>
</manifest>
</jar>
Which is swell. Now I've almost reached my goal of not having all these jar names copied all over the place:
<fileset id="commonLibs1" dir="${lib}">
<include name="foo.jar"/>
<include name="bar.jar"/>
</fileset>
<union id="clientLibs">
<fileset refid="commonLibs1"/>
<fileset dir="${lib}">
<include name="baz.jar"/>
</fileset>
</union>
<union id="serverLibs">
<fileset refid="commonLibs1"/>
<fileset dir="${lib}">
<include name="qux.jar"/>
</fileset>
</union>
<war ...>
<lib refid="serverLibs"/>
...
</war>
<manifestclasspath property="tmpClassPath" jarfile="./placeholdername.jar">
<classpath>
<resources refid="clientLibs"/>
</classpath>
</classpath>
<jar ...>
...
<manifest>
<attribute name="Class-Path" value="${tmpClasspath}"/>
</manifest>
</jar>
But:
build.xml:1067: serverLibs doesn't denote a zipfileset or a fileset
Is there any way to make this work?
The lib nested element of war only accepts a fileset or a zipfileset. Other resource collections like union are not supported. You may have a few ways to convert it to a fileset. If the union needs to be kept to be used somewhere else, then you can create a path element from it and use the Ant-Contrib pathtofileset task to convert it to a fileset:
<union id="serverLibs">
<fileset refid="commonLibs1"/>
<fileset dir="${lib}">
<include name="qux.jar"/>
</fileset>
</union>
<path id="serverLibsPath">
<resources refid="serverLibs" />
</path>
<pathtofileset pathrefid="serverLibsPath" name="serverLibsFileset" dir="${lib}" />
<war ...>
<lib refid="serverLibsFileset"/>
...
</war>
Or you can skip Ant-Contrib and use mappedresources with the path created above (see this post for an example).
A simpler way (if the union is only used by the war task) is to remove the union and just specify the combination of filesets in multiple lib elements:
<war ...>
<lib refid="commonLibs1"/>
<lib dir="${lib}">
<include name="qux.jar"/>
</lib>
...
</war>
Related
I have a number of tasks in my build.xml that all utilize the same set of filesets. E.g. something like the following (I've retain the actual names of the tasks - related to the cobertura coverage tool - but the nature of the enveloping task is immaterial to this question).
<target name="coverage-report">
<cobertura:cobertura-report destdir="${coverage.xml.dir}" format="xml">
<fileset dir="${src.dir}">
<include name="**/*.java" />
</fileset>
<fileset dir="${componentFoo.dir}/src">
<include name="**/*.java" />
</fileset>
</cobertura:cobertura-report>
</target>
<target name="summary-coverage-report">
<cobertura:cobertura-report destdir="${coverage.summaryxml.dir}" format="summaryXml">
<fileset dir="${src.dir}">
<include name="**/*.java" />
</fileset>
<fileset dir="${componentFoo.dir}/src">
<include name="**/*.java" />
</fileset>
</cobertura:cobertura-report>
</target>
<target name="alternate-coverage-report">
<cobertura:cobertura-report destdir="${coverage.html.dir}">
<fileset dir="${src.dir}">
<include name="**/*.java"/>
</fileset>
<fileset dir="${componentFoo.dir}/src">
<include name="**/*.java" />
</fileset>
</cobertura:cobertura-report>
</target>
What I would like to do is to be able to define once this recurring set of filesets and reuse it. Now, there's a related SO question on how to use the same fileset in multiple places but that's not working here as I have a number of filesets, not just one. I also tried wrapping the filesets inside a macrodef and expanding the macrodef when needed but I get the message that the these tasks "don't support the nested [name of macrodef] element". So I guess macrodefs in Ant can only be used as high-level tasks and cannot be expanded in arbitrary places.
So is there a way to reuse a definition of a number of filesets in Ant?
This would be easy if the cobertura tasks accepted arbitrary resource collections instead of being hard-coded to just fileset - it may be worth submitting a feature request to this end. For example with a copy task:
<resources id="filesets">
<fileset dir="${src.dir}">
<include name="**/*.java" />
</fileset>
<fileset dir="${componentFoo.dir}/src">
<include name="**/*.java" />
</fileset>
</resources>
<copy todir="dest">
<resources refid="filesets"/>
</copy>
You can fall back to the purely XML-level and use an entity reference:
<!DOCTYPE project [
<!ENTITY cobFilesets '<fileset dir="${src.dir}">
<include name="**/*.java" />
</fileset>
<fileset dir="${componentFoo.dir}/src">
<include name="**/*.java" />
</fileset>'>
]>
<project ....>
<target name="coverage-report">
<cobertura:cobertura-report destdir="${coverage.xml.dir}" format="xml">
&cobFilesets;
</cobertura:cobertura-report>
</target>
</project>
I am new to this ant I wrote athe following build.xml file for generating war and ear. it is showing build successfull. but it not generating any war/ear file. I mentioned my script below. please help me what changes i have to do..
Thanks in advance....
<property name="src" value="src"/>
<property name="dst" value="web"/>
<property name="classes" value="WEB-INF/classes"/>
<property name="archiveName" value="medcardets"/>
<property name="archive" value="BuildArchive"/>
<fileset id="lib" dir="${dst}/WEB-INF/lib">
<include name="*.jar"/>
</fileset>
<fileset id="war.file" dir="/">
<include name="${archiveName}.war"/>
</fileset>
<fileset id="ear.file" dir="/">
<include name="${archiveName}.war"/>
</fileset>
<fileset id="lib.rules" dir="${dst}/WEB-INF/lib/rules">
<include name="*.jar"/>
</fileset>
<fileset id="lib.j2ee" dir="j2ee">
<include name="*.jar"/>
</fileset>
<target name="clear">
<delete dir="${dst}/${classes}"/>
<delete dir="${archive}"/>
</target>
<target name="build" depends="clear">
<mkdir dir="${dst}/${classes}"/>
<javac srcdir="${src}" destdir="${dst}/${classes}" debug="on" debuglevel="lines,vars,source">
<classpath>
<fileset refid="lib"/>
<fileset refid="lib.rules"/>
<fileset refid="lib.j2ee"/>
</classpath>
</javac>
<copy todir="${dst}/${classes}">
<fileset dir="${src}">
<exclude name="**/CVS"/>
<exclude name="**/*.java"/>
</fileset>
</copy>
</target>
Your question implies that you have written code that you expect to generate a WAR, but you have no instruction to do so. I see from your comment you are actually asking what the command should be.
Most important: I hope we are all new to things, after 35 years in the industry I learn something every day. Why did you ask the question rather than google? Learning to search is the most important skill. Google ant task WAR gives this task with an example:
<war destfile="myapp.war" webxml="src/metadata/myapp.xml">
<fileset dir="src/html/myapp"/>
<fileset dir="src/jsp/myapp"/>
<lib dir="thirdparty/libs">
<exclude name="jdbc1.jar"/>
</lib>
<classes dir="build/main"/>
<zipfileset dir="src/graphics/images/gifs"
prefix="images"/>
</war>
and a similar entry for EAR:
<ear destfile="${build.dir}/myapp.ear" appxml="${src.dir}/metadata/application.xml">
<fileset dir="${build.dir}" includes="*.jar,*.war"/>
</ear>
Please, find below a few targets from my ant file:
<fileset id="test-dep-jars" dir="o:/java">
<include name="junit-4.10.jar"/>
<include name="easymock-3.1\easymock-3.1.jar"/>
<include name="easymockclassextension-3.1\easymockclassextension-3.1.jar"/>
</fileset>
<target name="copy-test-deps">
<mkdir dir="${deploy.dir}"/>
<copy todir="${deploy.dir}">
<fileset refid="test-dep-jars"/>
<flattenmapper/>
</copy>
</target>
<target name="jar" depends="copy-test-deps">
<jar destfile="${deploy.dir}/test-${ant.project.name}.jar" basedir="${test.classes.dir}"
includes="**/*.class" filesetmanifest="skip">
<manifest>
<attribute name="Class-Path"
value="${ant.project.name}.jar junit-4.10.jar easymock-3.1.jar easymockclassextension-3.1.jar"/>
</manifest>
</jar>
</target>
My problem is that I have to state the test dependency jars twice - once when defining the test-dep-jars fileset and the second time when specifying the Class-Path manifest attribute of the produced jar.
If I only could get hold on the flattenmapper result, then I would be able to use it in the Class-Path as is.
How can I get hold on the flattenmapper result?
Thanks.
If you want to use flattenmapper you can use following...
<pathconvert property="mf.classpath" pathsep=" ">
<path refid="build.class.path" />
<flattenmapper />
</pathconvert>
<manifest>
<attribute name="Class-Path"
value="${ant.project.name}.jar ${mf.classpath}"/>
</manifest>
I would recommend using the manifestclasspath task instead:
<manifestclasspath property="jar.classpath" jarfile="${jar.file}">
<classpath>
<fileset dir="${deploy.dir}" includes="*.jar"/>
</classpath>
</manifestclasspath>
<jar destfile="${jar.file}" basedir="${classes.dir}">
<manifest>
<attribute name="Main-Class" value="${jar.main.class}" />
<attribute name="Class-Path" value="${jar.classpath}" />
</manifest>
</jar>
It will generate the correct classpath property definition and even works with relative paths (for example if you were to place the dependent jars in sub-directory).
I am trying to create a runnable jar file from java classes using ant. The java classes use external jars. When I execute the build.xml its showing class not found exception while running the java program. Its compiling fine.
Part of My source code:
<path id="project-libpath">
<fileset dir="${lib.dir}">
<include name="*.jar"/>
</fileset>
</path>
<path id="project-classpath">
<fileset dir="C:/xmldecode/lib">
<include name="*.jar"/>
</fileset>
</path>
<target name="compile" depends="prepare">
<javac srcdir="${src.dir}" destdir="${classes.dir}">
<classpath refid="project-classpath"/>
</javac>
</target>
<target name="jar" depends="compile">
<copy todir="${classes.dir}">
<fileset dir="C:/xmldecode/lib"/>
</copy>
<pathconvert property="mf.classpath" pathsep=";">
<path refid="project-classpath" />
<flattenmapper />
</pathconvert>
<jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
<manifest>
<attribute name="Main-Class" value="${main-class}"/>
<attribute name="Class-Path" value="${mf.classpath}"/>
</manifest>
</jar>
</target>
<target name="run" depends="jar">
<java jar="${jar.dir}/${ant.project.name}.jar" fork="true">
</java>
Your problem is that the manifest classpath entries are not separated by a ";" character.
The following will work better I think:
<pathconvert property="mf.classpath" pathsep=" ">
<path refid="project-classpath" />
<flattenmapper />
</pathconvert>
Could I suggest using the new ANT task manifestclasspath ?
<manifestclasspath property="mf.classpath" jarfile="${jar.dir}/${ant.project.name}.jar">
<classpath refid="project-classpath" />
</manifestclasspath>
This powerful method will determine paths relative to the jar's location, for example if the jar's dependencies are located in a lib directory
In the build file below, the jar target refers to the jar.class.path property for the manifest class-path. The compile target refers to project.class.path
There is redundancy here, because jar.class.path and project.class.path are very similar. They must be both updated when libraries are added, which can be a pain if the list of libraries gets very long. Is there a better way? Any solution must be cross-platform and always use relative paths.
Edit:
It should generate the JAR classpath from a fileset and not the other way around, so I can use wildcards to e.g. include all JAR files in a directory.
<?xml version="1.0"?>
<project name="Higgins" default="jar" basedir=".">
<property name="jar.class.path" value="lib/forms-1.2.0.jar lib/BrowserLauncher.jar"/>
<path id="project.class.path">
<pathelement location="build"/>
<fileset dir="lib">
<include name="forms-1.2.0.jar"/>
<include name="BrowserLauncher.jar"/>
</fileset>
</path>
<target name="prepare">
<mkdir dir="build"/>
</target>
<target name="compile" depends="prepare" description="Compile core sources">
<javac srcdir="src"
includes="**"
destdir="build"
debug="true"
source="1.5">
<classpath refid="project.class.path"/>
</javac>
</target>
<target name="jar" depends="compile" description="Generates executable jar file">
<jar jarfile="higgins.jar">
<manifest>
<attribute name="Main-Class" value="nl.helixsoft.higgins.Main"/>
<attribute name="Class-Path" value="${jar.class.path}"/>
</manifest>
<fileset dir="build" includes="**/*.class"/>
<fileset dir="src" includes="**/*.properties"/>
</jar>
</target>
</project>
<path id="build.classpath">
<fileset dir="${basedir}">
<include name="lib/*.jar"/>
</fileset>
</path>
<pathconvert property="manifest.classpath" pathsep=" ">
<path refid="build.classpath"/>
<mapper>
<chainedmapper>
<flattenmapper/>
<globmapper from="*.jar" to="lib/*.jar"/>
</chainedmapper>
</mapper>
</pathconvert>
<target depends="compile" name="buildjar">
<jar jarfile="${basedir}/${test.jar}">
<fileset dir="${build}" />
<manifest>
<attribute name="Main-Class" value="com.mycompany.TestMain"/>
<attribute name="Class-Path" value="${manifest.classpath}"/>
</manifest>
</jar>
</target>
For further information check out this article.
Assuming Ant 1.7 or above, you can use the manifestclasspath task.
<path id="dep.runtime">
<fileset dir="./lib">
<include name="**/*.jar" />
</fileset>
</path>
<property name="dep_cp" value="${toString:dep.runtime}" />
<target name="default">
<manifestclasspath property="manifest_cp" jarfile="myjar.jar">
<classpath refid="dep.runtime" />
</manifestclasspath>
<echo message="Build Classpath: ${dep_cp}" />
<echo message="Manifest Classpath: ${manifest_cp}" />
</target>
If you just want a common subpath shared between two (or more) paths, that is easy to do:
<path id="lib.path>
<fileset dir="lib">
<include name="forms-1.2.0.jar"/>
<include name="BrowserLauncher.jar"/>
</fileset>
</path>
<path id="project.class.path">
<pathelement location="build"/>
<path refid="lib.path"/>
</path>
<property name="jar.class.path" refid="lib.path"/>
EDIT Sorry, I misunderstood the question. Try this:
<property name="jar.class.path" value="lib/forms-1.2.0.jar lib/BrowserLauncher.jar"/>
<path id="project.class.path">
<pathelement location="build"/>
<fileset dir="." includes="${jar.class.path}"/>
</path>
You can use <pathconvert> to convert a path (which can contain a fileset) into a plain string. You'll likely need to <echo> that string to a file, use either <replace> or <replaceregexp> to chop the leading path bits, then finally use <loadfile> to load the manipulated string into the final property.
Implementation left as an exercise to the reader.