Creating multiple jar files using single Ant build script - ant

I have a requirement where I need to build multiple jar files from each of the project that exists in my workspace. Can you please let me know how do I write a single build.xml to create multiple jar files ? I have tried similar code and know its possible but would like to know what are the best practices that needs to be followed. In my case, there are 5 projects in my workspace and I would have to create 5 different jars. Also there are external dependency jars, that needs to be included in some projects to build the jar file. Below is the sample code that I have written to create two jar files. The code that I have written to generate the jar file is below
<target name="compile" depends="compileVal, compileHib " description="compile the source for all">
</target>
<!-- =============================================
target: compile the source for Valueobjects
============================================= -->
<target name="compileVal" depends="clean" description="description">
<echo message="Creating directory '${target}' if not present "></echo>
<mkdir dir="${target}"/>
<mkdir dir="${Classfiles}"/>
<mkdir dir="${JarLocation}"/>
<javac srcdir="../ValueObjects/src" destdir="${Classfiles}" />
</target>
<!-- =================================
target: compile
================================= -->
<target name="compileHib" depends="clean" description="description">
<echo message="Creating directory '${target}' if not present "></echo>
<mkdir dir="${target}"/>
<mkdir dir="${Classfiles}"/>
<mkdir dir="${JarLocation}"/>
<javac srcdir="src" destdir="${Classfiles}" >
<classpath>
<pathelement location="${libloc}/hibernate3.jar"/>
<pathelement location="${libloc}/hsqldb.jar"/>
<pathelement location="${libloc}/jta.jar"/>
<pathelement location="${libloc}/ojdbc5.jar"/>
<pathelement location="${libloc}/commons-logging-1.1.1.jar"/>
<pathelement location="${libloc}/postgresql-9.1-902.jdbc4.jar"/>
</classpath>
</javac>
</target>
<!-- =================================
target: name
================================= -->
<target name="copy-non-java-files" depends="" description="description">
<copy todir="${Classfiles}" includeemptydirs="false">
<fileset dir="src" excludes="**/*.java" />
</copy>
</target>
<!-- =================================
target: compress
================================= -->
<target name="build_jar" depends="buildValobj, buildHib" description="description">
</target>
<!-- =================================
target: buildValobject jar
================================= -->
<target name="buildValobj" depends="compileVal" description="creates the valueObject jar">
<echo message="Creating the jar to Folder '${JarDest}'"> </echo>
<jar destfile="${JarDest}/${RetValObName}" basedir="${Classfiles}" />
</target>
<!-- =================================
target: buildHibjar
================================= -->
<target name="buildHib" depends="compileHib, copy-non-java-files" description="description">
<echo message="Creating the jar to Folder '${JarDest}'"> </echo>
<jar destfile="${JarDest}/${RetHibName}" basedir="${Classfiles}" />
</target>
<!-- =================================
target: clean
================================= -->
<target name="clean">
<echo message="Cleaning '${target}' folder"></echo>
<delete includeemptydirs="true">
<fileset dir="${target}" includes="**/*"/>
</delete>
</target>
In the above code, the issue im facing is while trying to compileHib though I have specified the depends="clean". The clean does not happen. A jar is created that includes, both the files that were already existing as well as the new files that were moved.Not sure why? Though I have specified clean, the clean action does not seem to happen. Can you please let me know what im missing in the above.
Please Assist. THanks in Advance

Related

ant build generating same class files while packaging the war file

Below is the Ant script i am trying in GAE project but when i look into the war file i see two class for each java file.
Just a fyi, WAR file created i open in winrar but even when i extract it winrar keeps on asking me to either replace existing file message. Not sure how even same file name & extension is present in one folder. Also i checked the "dist" & "classes" folder i does not have duplicate file.
<?xml version="1.0" ?>
<project name="AntExample1" default="war">
<path id="compile.classpath">
<fileset dir="war/WEB-INF/lib">
<include name="*.jar"/>
</fileset>
</path>
<target name="init">
<mkdir dir="build/classes"/>
<mkdir dir="dist" />
</target>
<target name="compile" depends="init" >
<javac destdir="build/classes" debug="true" srcdir="src">
<classpath refid="compile.classpath"/>
</javac>
</target>
<target name="war" depends="compile">
<war destfile="dist/AntExample.war" webxml="war/WEB-INF/web.xml">
<fileset dir="war"/>
<lib dir="war/WEB-INF/lib"/>
<classes dir="build/classes"/>
</war>
</target>
<target name="clean">
<delete dir="dist" />
<delete dir="build" />
</target>
</project>

ant delete task with wildcard not working

I would like to set up an ant task to delete all .class and .jar
files from the same directory the build.xml file is, including
nonsense.class which is in the same directory as the build.xml
file. So I've set up the following build.xml file for ant 1.9.0
as follows:
<?xml version="1.0"?>
<project name="HelloWorld" default="deploy">
<!-- ... -->
<target name="clean">
<delete file="nonsense.class" />
<delete file="*.class" />
<delete file="*.jar" />
</target>
</project>
When I execute it nonsense.class is removed but none of
the other .class or .jar files. What am I doing wrong?
You need to use a fileset to delete more than one file:
<target name="clean">
<delete file="nonsense.class" />
<delete>
<fileset dir=".">
<include name="*.class"/>
<include name="*.jar"/>
</fileset>
</delete>
</target>

ANT Generated jar: is it a namespace issue?

I have a Eclipse-Java-Project with an ANT-build-file. This build file exports a jar of the project without compiling it. So I only export the sources.
<target name="jar">
<mkdir dir="/jar"/>
<jar destfile="/jar/my_test_jarfile.jar" basedir="/src" />
</target>
I use this generated jar in another eclipse java project and set the path to the jar in the build-path-settings of the project. The problem is that eclipse says it cannot resolve the namespace of the imported classes of the jar.
If I export the jar manually by right clicking on the project and then "Export" and putting the jar to the build path of the other project, everything works fine and there are no errors. So the question is now, what am I doing wrong?
So here is my solution. It seems that you have to compile the source first and then pack it into a jar. I don't give a guarantee that this jar is exactly the same like the one you get from eclipse when you do the right click thing and export etc.
But it works for me, there are no namespace errors any longer. so here is a minimum version of my ant targets:
<project default="run" basedir=".">
<property name="src.dir" value="src" />
<property name="classes.dir" value="bin" />
<property name="build.dir" value="build" />
<path id="libs">
<fileset dir="lib">
<include name="*.jar"/>
</fileset>
<pathelement path="${basedir}\${classes.dir}"/>
</path>
<target name="run">
<antcall target="compile"/>
<antcall target="jar"/>
</target>
<target name="compile">
<javac debug="true" srcdir="${src.dir}" destdir="${classes.dir}" classpathref="libs" encoding="UTF-8" />
</target>
<target name="jar">
<jar destfile="${build.dir}/my_jar_file.jar" basedir="${classes.dir}">
</target>
</project>

Ant jar file for swing program

<?xml version="1.0" ?>
<project name="javaGui" default="execute">
<target name="init" depends="clean">
<mkdir dir="build/classes" />
<mkdir dir="dist" />
</target>
<target name="compile" depends="init">
<javac srcdir="src" destdir="build/classes" />
</target>
<target name="execute" depends="compile">
<java classname="Swing" classpath="build/classes" />
<jar destfile="dist/final.jar" basedir="build/classes" />
</target>
<target name="clean">
<delete dir="build" />
<delete dir="dist" />
</target>
This is ant script to generate jar file.problem is those code will generate jar but when i click on that jar it is not opening means it not showing any GUI.
im new to this please let me know what is going wrong.
javaGUI is project and Swing is class name
jar file will not open in GUI. You need to run jar file from console.
Go to dist directory -> run this command:
$ java -jar final.jar [optional parameters]
For more details : see this reference
UPDATE
Instead of giving ant from target execute, try this :
<target name="jar">
<mkdir dir="build/jar"/>
<jar destfile="build/jar/HelloWorld.jar" basedir="build/classes">
<manifest>
<attribute name="Main-Class" value="Swing"/>
</manifest>
</jar>
</target>
<target name="run">
<java jar="build/jar/HelloWorld.jar" fork="true"/>
</target>

hOW can i add external jars in my build.xml

hOW can i add external jars in my build.xml.
i am getting compilation error while running my build.xml.some jars are missing.how can i add them in my build.xml.
my build.xml looks like this
<project name="HUDSONSTATUSWS" default="dist" basedir=".">
<description>
Web Services build file
</description>
<!-- set global properties for this build -->
<property name="src" location="src"/>
<property name="build" location="build"/>
<property name="dist" location="dist"/>
<property name="webcontent" location="WebContent"/>
<target name="init">
<!-- Create the time stamp -->
<tstamp/>
<!-- Create the build directory structure used by compile -->
<mkdir dir="${build}"/>
</target>
<target name="compile" depends="init"
description="compile the source " >
<!-- Compile the java code from ${src} into ${build} -->
<javac srcdir="${src}" destdir="${build}"/>
</target>
<target name="dist" depends="compile"
description="generate the distribution" >
<!-- Create the distribution directory -->
<mkdir dir="${dist}/lib"/>
<!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file -->
<jar jarfile="${dist}/lib/MyProject-${DSTAMP}.jar" basedir="${build}"/>
</target>
<target name="war" depends="compile"
description="generate the distribution war" >
<!-- Create the war distribution directory -->
<mkdir dir="${dist}/war"/>
<!-- Follow standard WAR structure -->
<copydir dest="${dist}/war/build/WEB-INF/" src="${webcontent}/WEB-INF/" />
<copydir dest="${dist}/war/build/WEB-INF/classes/" src="${build}" />
<jar jarfile="${dist}/war/HelloWorld-${DSTAMP}.war" basedir="${dist}/war/build/"/>
</target>
<target name="clean"
description="clean up" >
<!-- Delete the ${build} and ${dist} directory trees -->
<delete dir="${build}"/>
<delete dir="${dist}"/>
</target>
</project>
You just need to specify the classpath for javac, using the classpath or classpathref attribute, or a nested classpath element.
See http://ant.apache.org/manual/Tasks/javac.html for details. The ant documentation is very well written, exhaustive, and full of examples. It's there to be read.

Resources