How to include local dependencies in my ant build - java

At present I have the following build.xml:
<project name="Bccn" default="help" basedir=".">
<!-- Define the properties used by the build -->
<property name="app.name" value="bccn" />
<property name="app.version" value="0.1-dev" />
<property name="tcserver.home" value="/home/abhishek/tomcat" />
<property name="work.home" value="${basedir}/work" />
<property name="dist.home" value="${basedir}/dist" />
<property name="src.home" value="${basedir}/src" />
<property name="web.home" value="${basedir}/web" />
<property name="lib.dir" value="${basedir}/lib" />
<target name="help">
<echo>You can use the following targets:</echo>
<echo>
</echo>
<echo> help : (default) Prints this message </echo>
<echo> all : Cleans, compiles, and packages application</echo>
<echo> clean : Deletes work directories</echo>
<echo> compile : Compiles servlets into class files</echo>
<echo> dist : Packages artifacts into a deployable WAR</echo>
<echo>
</echo>
<echo>For example, to clean, compile, and package all at once, run:</echo>
<echo>prompt> ant all </echo>
</target>
<!-- Define the CLASSPATH -->
<path id="compile.classpath">
<fileset dir="${tcserver.home}/bin">
<include name="*.jar" />
</fileset>
<pathelement location="${tcserver.home}/lib" />
<fileset dir="${tcserver.home}/lib">
<include name="*.jar" />
</fileset>
</path>
<target name="all" depends="clean,compile,dist" description="Clean work dirs, then compile and create a WAR" />
<target name="clean" description="Delete old work and dist directories">
<delete dir="${work.home}" />
<delete dir="${dist.home}" />
</target>
<target name="prepare" depends="clean" description="Create working dirs and copy static files to work dir">
<mkdir dir="${dist.home}" />
<mkdir dir="${work.home}/WEB-INF/classes" />
<!-- Copy static HTML and JSP files to work dir -->
<copy todir="${work.home}">
<fileset dir="${web.home}" />
</copy>
</target>
<target name="compile" depends="prepare" description="Compile Java sources and copy to WEB-INF/classes dir">
<javac srcdir="${src.home}" destdir="${work.home}/WEB-INF/classes">
<classpath refid="compile.classpath" />
</javac>
<copy todir="${work.home}/WEB-INF/classes">
<fileset dir="${src.home}" excludes="**/*.java" />
</copy>
</target>
<target name="dist" depends="compile" description="Create WAR file for binary distribution">
<jar jarfile="${dist.home}/${app.name}-${app.version}.war" basedir="${work.home}" />
</target>
Now I included log4j as a local dependency and want to include it when I create my .war file. However, ANT is not able to find the dependency. Is there a way to get it working? Sorry for the basic question, I am a noob at it.
Update (and thanks for the help I got already):
I didn't want to add the "war" thing so I modified my build.xml as follows:
``
<target name="help">
<echo>You can use the following targets:</echo>
<echo>
</echo>
<echo> help : (default) Prints this message </echo>
<echo> all : Cleans, compiles, and packages application</echo>
<echo> clean : Deletes work directories</echo>
<echo> compile : Compiles servlets into class files</echo>
<echo> dist : Packages artifacts into a deployable WAR</echo>
<echo>
</echo>
<echo>For example, to clean, compile, and package all at once, run:</echo>
<echo>prompt> ant all </echo>
</target>
<!-- Define the CLASSPATH -->
<path id="compile.classpath">
<fileset dir="${tcserver.home}/bin">
<include name="*.jar" />
</fileset>
<pathelement location="${tcserver.home}/lib" />
<fileset dir="${tcserver.home}/lib">
<include name="*.jar" />
</fileset>
<fileset dir="${lib.dir}">
<include name="*.jar" />
</fileset>
</path>
<target name="all" depends="clean,compile,dist" description="Clean work dirs, then compile and create a WAR" />
<target name="clean" description="Delete old work and dist directories">
<delete dir="${work.home}" />
<delete dir="${dist.home}" />
</target>
<target name="prepare" depends="clean" description="Create working dirs and copy static files to work dir">
<mkdir dir="${dist.home}" />
<mkdir dir="${work.home}/WEB-INF/classes" />
<!-- Copy static HTML and JSP files to work dir -->
<copy todir="${work.home}">
<fileset dir="${web.home}" />
</copy>
</target>
<target name="compile" depends="prepare" description="Compile Java sources and copy to WEB-INF/classes dir">
<javac srcdir="${src.home}" destdir="${work.home}/WEB-INF/classes">
<classpath refid="compile.classpath" />
</javac>
<copy todir="${work.home}/WEB-INF/classes">
<fileset dir="${src.home}" excludes="**/*.java" />
<fileset dir="${lib.dir}" includes="*.jar" />
</copy>
</target>
<target name="dist" depends="compile" description="Create WAR file for binary distribution">
<jar jarfile="${dist.home}/${app.name}-${app.version}.war" basedir="${work.home}" />
</target>
Now ANT can find the dependencies and compile it. However when I deploy it to a Tomcat server, it fails to file the dependencies. Can you please provide some ideas as to how I can package the dependencies so its visible to Tomcat as well?

You should use the war task instead:
<war destfile="${warname}" webxml="war/WEB-INF/web.xml">
<fileset dir="${work.home}"/>
<lib dir="${lib.dir}/" includes="log4j.jar"/>
<classes dir = "${CLASSES}" />
</war>
You can configure it to find the dependency jars to be included in your war file and to specify the path to the web.xml folder. You shouldn't be coping the .class files to the destination in the war folder, let the war task do it for you.

Related

Ant - javac task - classpath is not taking the jars

I have my ant build.xml file like this:
<!-- Properties -->
<property name="src" value="${basedir}" />
<property name="jars" value="${src}/jars" />
<property name="dest" value="${src}/dest" />
<property name="reports" value="${src}/reports" />
<path id="claspath">
<fileset dir="${jars}">
<include name="*.jar" />
</fileset>
</path>
<target name="clean">
<echo> removing the directories "dest" and "reports" </echo>
<delete dir="${dest}" />
<delete dir="${reports}" />
</target>
<target name="makedir" depends="clean">
<echo> creating directories "dest" and "reports" </echo>
<mkdir dir="dest" />
<mkdir dir="reports" />
</target>
<target name="complie" depends="makedir">
<javac srcdir="${src}" destdir="${dest}" />
<classpath refid="classpath"/>
</target>
When I type ant command in cmd prompt the compiling happens but the jar files are not loading so I am getting compiling errors. The jar folder that i have mentioned in the above code has only one jar file which is "testng-6.8.5.jar". Please let me know what is wrong in the above code.
You're closing the <javac> tag on the same line, it should be:
<javac srcdir="${src}" destdir="${dest}">
<classpath refid="classpath"/>
</javac>

runtime classpath using manifest.mf classpath

i built an ear using ant and the its compiling fine. however when i deploy i get the noclassdeffound error in the weblogic server logs. so i added the libraries(jars) in server startup script i.e the server java classpath it works fine.
Please help me how to resolve this runtime classpath issue using ant build. i assume adding classpath in manifest.mf file will help. so far my build.xml is:
Please advice
<?xml version="1.0"?>
<project name="xxx APP Check" default="all" basedir=".">
<target name="init">
<property name="software.version" value="1.0"/>
<property name="user.name" value="usrnme"/>
<property name="dirs.base" value="${basedir}"/>
<property name="classdir" value="${dirs.base}/build/src"/>
<property name="src" value="${dirs.base}/src"/>
<property name="mf" value="${dirs.base}/src/META-INF"/>
<property name="jar" value="${dirs.base}/build/jar"/>
<property name="web" value="${dirs.base}/web"/>
<property name="deploymentdescription" value="${dirs.base}/build/deploymentdescriptors"/>
<property name="warFile" value="xxxappchk.war"/>
<property name="earFile" value="xxxxappchk.ear"/>
<property name="earDir" value="${dirs.base}/build/ear"/>
<property name="warDir" value="${dirs.base}/build/war"/>
<property name="srcDir" value="${dirs.base}/build/src"/>
<!-- Create Web-inf and classes directories -->
<mkdir dir="${warDir}/WEB-INF"/>
<mkdir dir="${warDir}/WEB-INF/classes"/>
<mkdir dir="${warDir}/WEB-INF"/>
<mkdir dir="${warDir}/WEB-INF/lib"/>
<mkdir dir="${warDir}/WEB-INF/classes"/>
<!-- Create Meta-inf and classes directories -->
<mkdir dir="${warDir}/META-INF"/>
<mkdir dir="${earDir}/META-INF"/>
</target>
<!-- Main target -->
<target name="all" depends="init,build,buildWar,buildEar,clean"/>
<!-- Compile Java Files and store in /build/src directory -->
<target name="build" >
<path id="3rdparty.jar.path">
<fileset dir="${jar}">
<include name="*.jar" />
</fileset>
</path>
<javac srcdir="${src}" includeantruntime="false" destdir="${classdir}" debug="true" includes="**/*.java" >
<classpath>
<path refid="3rdparty.jar.path"/>
</classpath>
</javac>
</target>
<!-- Create the War File -->
<target name="buildWar" depends="init">
<copy todir="${warDir}/WEB-INF/classes">
<fileset dir="${classdir}" includes="**/*.class" />
</copy>
<jar destfile="${warDir}/WEB-INF/lib/xxxx-appcheck.jar"
basedir="${warDir}/WEB-INF/classes"
/>
<echo message="Hyperion appcheck jar created."/>
<copy todir="${warDir}/WEB-INF/classes/com/ca/xxxx_appcheck">
<fileset dir="${src}" includes="**/*.java" />
</copy>
<copy todir="${warDir}/WEB-INF">
<fileset dir="${deploymentdescription}" includes="web.xml" />
</copy>
<copy todir="${warDir}/WEB-INF/lib">
<fileset dir="${jar}" includes="*.jar" />
</copy>
<copy todir="${warDir}">
<fileset dir="${web}" includes="**/*.*" />
</copy>
<!-- Create war file and place in ear directory -->
<jar jarfile="${earDir}/${warFile}" basedir="${warDir}" />
</target>
<!-- Create the War File -->
<target name="buildEar" depends="init">
<copy todir="${earDir}/META-INF">
<fileset dir="${deploymentdescription}" includes="application.xml" />
</copy>
<!-- Create ear file and place in ear directory -->
<jar jarfile="${dirs.base}/${earFile}" basedir="${earDir}" />
</target>
<target name="clean" description="Delete all generated files">
<delete dir="${srcDir}" failonerror="false"/>
<delete dir="${earDir}" failonerror="false"/>
<delete dir="${warDir}" failonerror="false"/>
<echo message="Deleted the temp directories src, war, ear"/>
<mkdir dir="${earDir}"/>
<mkdir dir="${warDir}"/>
<mkdir dir="${srcDir}"/>
<echo message="created the temp directories src, war, ear"/>
</target>
</project>
Usually you don't need to add anything to MANIFEST.MF to make your jars added to application classpath.
Your web application (WAR) files should be directly under EAR.
Overall dependencies (JAR) should be under EAR's APP-INF/lib directory.
Web application own dependencies under WAR files WEB-INF/lib directory. Looking at your build.xml, they should be there.
Before changing you Ant build script, please check these prerequisites and please provide the stacktrace from Weblogic error log.

precompile jsps into classes for Jetty8 using ant

We are using jspc ant task to pre-compile JSP files into classes/(then package into war)
Now we are switching to Jetty 8. According to the doc, there exists a maven plugin to do this. Do we have ant task to do the same?
Its best if you use the JSP libs that comes with the jetty distribution.
Here's an example, using jetty-distribution-8.1.5.v20120716
<?xml version="1.0" ?>
<project name="AntExample1" default="war">
<property name="jetty.home" value="${user.home}/code/intalio/distros/jetty-distribution-8.1.5.v20120716" />
<path id="compile.jspc">
<fileset dir="${jetty.home}">
<include name="lib/servlet-api-*.jar" />
<include name="lib/jsp/*.jar" />
</fileset>
</path>
<path id="compile.classpath">
<fileset dir="WebContent/WEB-INF/lib">
<include name="*.jar" />
</fileset>
<path refid="compile.jspc" />
</path>
<target name="jspc" depends="compile">
<taskdef classname="org.apache.jasper.JspC" name="jasper2" classpathref="compile.jspc" />
<jasper2 validateXml="false"
uriroot="WebContent"
addWebXmlMappings="true"
webXmlFragment="WebContent/WEB-INF/generated_web.xml"
compilerSourceVM="1.6"
compilerTargetVM="1.6"
outputDir="build/gen-src"
verbose="9" />
</target>
<target name="init">
<mkdir dir="build/classes" />
<mkdir dir="build/gen-src" />
<mkdir dir="dist" />
<copy todir="build/gen-src">
<fileset dir="src" includes="**/*.java" />
</copy>
</target>
<target name="compile" depends="init">
<javac destdir="build/classes" debug="true" srcdir="build/gen-src">
<classpath refid="compile.classpath" />
</javac>
</target>
<target name="war" depends="jspc">
<war destfile="dist/AntExample.war" webxml="WebContent/WEB-INF/web.xml">
<fileset dir="WebContent" />
<classes dir="build/classes" />
</war>
</target>
<target name="clean">
<delete dir="dist" />
<delete dir="build" />
</target>
</project>
Update: April 8, 2013
Pushed an example project with this build to github.
https://github.com/jetty-project/jetty-example-jspc-ant
Jasper has jspc; the linked page has ant code for calling it.
From your first sentence you are apparently already using an ant task to pre-compile the jsp files...so using jetty-8 doesn't mean you have to change that process at all, you will still just pre-compile as you have been, building your war file as you have been and then just deploy into jetty-8. You will need to add jsp to the OPTIONS in the start.ini to get the jsp engine into the server classloader.

problem running JUnit tests with Ant in Eclipse. Beginner question

I'm learning these days how to use ant to run automated test folowing this tutorial.
I have JUnit in the classpath of my project. All seem to work fine and I can include it in my classes:
import junit.framework.TestCase; //line20
public class SimpleLattice1DTest extends TestCase{
...
}
My build.xml is:
<?xml version="1.0"?>
<project name="Ant-Test" default="compile" basedir=".">
<!-- Sets variables which can later be used. -->
<!-- The value of a property is accessed via ${} -->
<property name="src.dir" location="." />
<property name="build.dir" location="build" />
<property name="dist.dir" location="dist" />
<property name="docs.dir" location="docs" />
<property name="test.dir" location="jlife/tests" />
<property name="test.report.dir" location="test/report" />
<!-- Deletes the existing build, docs and dist directory-->
<target name="clean">
<delete dir="${build.dir}" />
<delete dir="${docs.dir}" />
<delete dir="${dist.dir}" />
</target>
<!-- Creates the build, docs and dist directory-->
<target name="makedir">
<mkdir dir="${build.dir}" />
<mkdir dir="${docs.dir}" />
<mkdir dir="${dist.dir}" />
<mkdir dir="${test.report.dir}" />
</target>
<!-- Compiles the java code (including the usage of library for JUnit -->
<target name="compile" depends="clean, makedir">
<javac srcdir="${src.dir}" destdir="${build.dir}">
</javac>
</target>
<!-- Creates Javadoc -->
<target name="docs" depends="compile">
<javadoc packagenames="src" sourcepath="${src.dir}" destdir="${docs.dir}">
<!-- Define which files / directory should get included, we include all -->
<fileset dir="${src.dir}">
<include name="**" />
</fileset>
</javadoc>
</target>
<!--Creates the deployable jar file -->
<target name="jar" depends="compile">
<jar destfile="${dist.dir}\CoreTest.jar" basedir="${build.dir}">
<manifest>
<attribute name="Main-Test" value="test.CoreTest" />
</manifest>
</jar>
</target>
<!-- Run the JUnit Tests -->
<!-- Output is XML, could also be plain-->
<target name="junit" depends="compile">
<junit printsummary="on" fork="true" haltonfailure="yes">
<formatter type="xml" />
<batchtest todir="${test.report.dir}">
<fileset dir="${src.dir}">
<include name="**/*Test*.java" />
</fileset>
</batchtest>
</junit>
</target>
</project>
When i run it into eclipse I get the following error:
[javac] C:\Documents and
Settings\noname\Documenti\JLife_git\JLife_git\JLife\src\jlife\tests\SimpleLattice1DTest.java:20:
package junit.framework does not exist
[javac] import junit.framework.TestCase;
I suppose there's something wrong with it, but I have no idea. Could someone put me in the right direction?
Your javac target doesn't specify anything apart from the source and target directory - it doesn't add any classpath entries; you'll need to add an entry for the appropriate JUnit jar file. See the javac task documentation for more details. You may want to specify the path to JUnit as a classpath attribute, a nested element, or a reference to a path declared elsewhere.
The eclipse classpath is separate from your ant environment. In your build file, when you call javac you need to supply a classpath attribute.
You can define the classpath at the top of the file with the rest of your properties, like this:
<path id="classpath">
<fileset dir="[path to libraries]" includes="**/*.jar" />
</path>
and then use it in each call to javac by setting the classpathref attribute, like this:
<javac srcdir="${src.dir}" destdir="${build.dir}" classpathref="classpath" />
You need to specify the directory that contains your .class files and your external jars (like junit).
e.g.
<!-- Populates a class path containing our classes and jars -->
<path id="dist.classpath">
<fileset dir="${lib}"/>
<pathelement path="${build}"/>
</path>
<!-- Compile the java code place into ${build} -->
<target name="compile" depends="-dirty" description="Compile the source.">
<javac srcdir="${source}" destdir="${build}" includeantruntime="false">
<classpath refid="dist.classpath"/>
<exclude name="${test.relative}/**/*"/>
</javac>
</target>
Here's the complete file I took that excerpt from in case you need ideas for how to setup other common things (emma, javadoc, etc)
<project name="imp" default="dist" basedir="..">
<description>Buildscript for IMP</description>
<property name="source" location="src"/>
<property name="lib" location="lib"/>
<property name="history" location="test_history"/>
<property name="web-tests" location="/var/www/tests"/>
<property name="web-files" location="/var/www/files"/>
<property name="web-javadoc" location="/var/www/javadoc"/>
<property name="web-emma" location="/var/www/emma"/>
<property name="emma.dir" value="${lib}"/>
<property name="test" location="${source}/imp/unittest"/>
<property name="test.relative" value="imp/unittest"/>
<property name="javadoc-theme" value="tools/javadoc-theme"/>
<!-- directories for generated files -->
<property name="build" location="build"/>
<property name="build-debug" location="debug"/>
<property name="build-coverage" location="coverage"/>
<property name="dist" location="dist"/>
<property name="reports" location="reports"/>
<property name="coverage-emma" location="${reports}/coverage/emma"/>
<!-- Populates a class path containing our classes and jars -->
<path id="dist.classpath">
<fileset dir="${lib}"/>
<pathelement path="${build}"/>
</path>
<path id="debug.classpath">
<fileset dir="${lib}"/>
<pathelement path="${build-debug}"/>
</path>
<!-- import emma. This classpath limits the coverage to just our classes -->
<path id="debug.imp.classpath">
<pathelement path="${build-debug}"/>
</path>
<taskdef resource="emma_ant.properties" classpathref="debug.classpath"/>
<!--
Shouldn't ever need to use this from the command line. IRC saith that the "private"
internal use only sort of targets are prefixed with '-'.
dirty because it's the opposite of the 'clean' target.
-->
<target name="-dirty">
<tstamp/>
<mkdir dir="${build}"/>
<mkdir dir="${build-debug}"/>
<mkdir dir="${build-coverage}"/>
<mkdir dir="${dist}"/>
<mkdir dir="${reports}"/>
<mkdir dir="${coverage-emma}"/>
</target>
<!-- clean up all the generated files and direcories -->
<target name="clean" description="Deletes all files and directories created by this script.">
<delete dir="${build}"/>
<delete dir="${build-debug}"/>
<delete dir="${build-coverage}"/>
<delete dir="${dist}"/>
<delete dir="${reports}"/>
<delete dir="${coverage-emma}"/>
</target>
<!-- Compile the java code place into ${build} -->
<target name="compile" depends="-dirty" description="Compile the source.">
<javac srcdir="${source}" destdir="${build}" includeantruntime="false">
<classpath refid="dist.classpath"/>
<exclude name="${test.relative}/**/*"/>
</javac>
</target>
<!-- Compile the java code with debug info place into ${build} -->
<target name="compile-debug" depends="-dirty" description="Compile the source with debug information.">
<javac
srcdir="${source}"
destdir="${build-debug}"
includeantruntime="false"
debug="true"
debuglevel="lines,vars,source"
>
<classpath refid="debug.classpath"/>
</javac>
</target>
<!-- roll up everyting into a single jar file -->
<target name="dist" depends="clean, compile" description="Generate the distribution file for IMP.">
<!-- Copy the library .jars to the directory where the IMP distribution will be located -->
<copy todir="${dist}">
<fileset dir="${lib}"/>
</copy>
<!-- TODO: Generate the MANIFEST.MF file on the fly -->
<jar jarfile="${dist}/imp.jar" basedir="${build}" manifest="tools/MANIFEST.MF"/>
<!-- dump to web server -->
<copy todir="${web-files}">
<fileset dir="${dist}"/>
</copy>
</target>
<!-- build and run the tests then report the results in HTML -->
<target name="test" depends="compile-debug" description="Run all the JUnit tests and outputs the results as HTML.">
<!-- run the tests -->
<junit printsummary="true" haltonerror="false" haltonfailure="false">
<classpath refid="debug.classpath"/>
<formatter type="xml"/>
<batchtest fork="true" todir="${reports}">
<fileset dir="${source}">
<include name="${test.relative}/**/*Test*.java"/>
<exclude name="${test.relative}/**/AllTests.java"/>
</fileset>
</batchtest>
</junit>
<!-- report the results -->
<junitreport todir="${reports}">
<fileset dir="${reports}" includes="TEST-*.xml"/>
<report todir="${reports}"/>
</junitreport>
<!-- update the latest results file to be commited -->
<copy file="${reports}/TESTS-TestSuites.xml" tofile="${history}/test-results-latest.xml"/>
<!-- dump to webserver -->
<copy todir="${web-tests}">
<fileset dir="${reports}"/>
</copy>
</target>
<!-- run emma code coverage tool and publish results in HTML -->
<target name="emma" depends="compile-debug" description="Checks code coverage with Emma.">
<!-- put the magic emma juice into the classes -->
<emma>
<instr
instrpathref="debug.imp.classpath"
destdir="${coverage-emma}/instr"
metadatafile="${coverage-emma}/metadata.emma"
merge="true"
/>
</emma>
<!-- run the tests -->
<junit fork="true" printsummary="true" haltonerror="false" haltonfailure="false">
<classpath>
<pathelement location="${coverage-emma}/instr"/>
<path refid="debug.classpath"/>
</classpath>
<batchtest fork="true" todir="${reports}">
<fileset dir="${source}">
<include name="${test.relative}/**/*Test*.java"/>
<exclude name="${test.relative}/**/AllTests.java"/>
</fileset>
</batchtest>
<jvmarg value="-Demma.coverage.out.file=${coverage-emma}/coverage.emma"/>
<jvmarg value="-Demma.coverage.out.merge=true"/>
</junit>
<!-- publish the coverage report -->
<emma>
<report sourcepath="${source}" verbosity="verbose">
<fileset dir="${coverage-emma}">
<include name="*.emma"/>
</fileset>
<html outfile="${web-emma}/index.html"/>
</report>
</emma>
</target>
<!-- publish javadoc -->
<target name="javadoc" description="Creates javadoc for IMP.">
<delete dir="${web-javadoc}"/>
<javadoc
sourcepath="${source}"
defaultexcludes="no"
destdir="${web-javadoc}"
author="true"
version="true"
use="true"
windowtitle="IMP: Integrated Mechanisms Program"
overview="${source}/overview.html"
classpathref="debug.classpath"
stylesheetfile="${javadoc-theme}/stylesheet.css"
/>
<copy file="${javadoc-theme}/javadoc.jpg" tofile="${web-javadoc}/javadoc.jpg"/>
</target>
<target name="all" description="Runs test, emma, javadoc, and dist targets.">
<antcall target="test"/>
<antcall target="emma"/>
<antcall target="javadoc"/>
<antcall target="dist"/>
</target>
</project>
If you observe the error stack, you will find the following line, just above the error line you mentioned...
[javac] [search path for class files: C:\Program Files\Java\jre6\lib\resource...
This line shows all the jars available in the class path for this ant target execution.
You will definitely not find the desired jar over here i.e. junit-x.x.x.jar (junit-4.8.2.jar)
Now go to eclipse -> Window -> preferences -> Ant -> Runtime -> Global Entries -> Add Jars add junit-4.8.2jar (which you will find in your project lib directory)
If you play around the Ant -> Runtime -> classpath and the classpath related error line in the error stack, you will understand the issue.
Hope this solves your problem.

Ant+Junit works on first run and never again

Hey guys I've been trying all day to get an ant file to automatically build my project. The file (appended below) was on a web page I found and is pretty thorough.
My problem is that it works as long as I don't run the "clean" target. Once the I run the "clean" target the "test" target ceases to work. I get NoClassFound errors on all test classes. Even though it worked earlier.
I'm working on a Macbook (10.5.8) using the latest version of eclipse. This error occurs running the file from eclipse and from a terminal using Ant version 1.7.1
I modified the file slightly to adapt it to my file structure which is as follows:
src/
packageA.packageB/
ClassA.java
...
ClassN.java
unittests/
packageA.packageB/
AllClassTests.java
ClassATest.java
...
ClassNTest.java
lib/
junit-4.7.jar
The ant file is:
<project name="SampleJUnitTests" default="dist" basedir=".">
<description>
DataTypes Build File
</description>
<!-- set global properties for this build -->
<property name="project_name" value="DataTypes"/>
<property name="src" location="src"/>
<property name="build" location="bin"/>
<property name="dist" location="dist"/>
<property name="lib" location="lib"/>
<property name="reports" location="reports"/>
<property name="tests" location="unittests"/>
<property name="tmp" location="tmp_file"/>
<!-- the names of various distributable files -->
<property name="jar_name" value="${project_name}.jar"/>
<property name="war_name" value="${project_name}.war"/>
<!-- top level targets -->
<target name="compile" depends="init" description="compile the source code " >
<javac srcdir="${src}" destdir="${build}">
<classpath>
<fileset dir="lib">
<include name="**/*.jar"/>
</fileset>
</classpath>
</javac>
</target>
<target name="dist" depends="compile" description="generate the distributable files " >
<!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file -->
<jar jarfile="${dist}/${jar_name}" basedir="${build}"/>
</target>
<target name="clean" description="clean up" >
<!-- Delete the ${build} and ${dist} directory trees -->
<delete dir="${build}"/>
<delete dir="${dist}"/>
<delete dir="${reports}"/>
<delete dir="${tmp}"/>
</target>
<target name="run-tests" depends="compile" description="run your test suite" >
<junit printsummary="yes" haltonfailure="no" showoutput="yes" tempdir="${tmp}">
<classpath>
<pathelement path="${build}"/>
<fileset dir="lib">
<include name="**/*.jar"/>
</fileset>
</classpath>
<batchtest fork="yes" todir="${reports}/raw/">
<formatter type="xml"/>
<fileset dir="${tests}">
<include name="**/*.java"/>
<exclude name="**/All*Tests.java"/>
</fileset>
</batchtest>
</junit>
</target>
<target name ="test" depends="run-tests">
<junitreport todir="${reports}">
<fileset dir="${reports}/raw/">
<include name="TEST-*.xml"/>
</fileset>
<report format="frames" todir="${reports}\html\"/>
</junitreport>
</target>
<target name ="run" depends="" description="if this project can be run, run it" >
</target>
<!-- supporting targets -->
<target name="init" description="initialize the build environment" >
<!-- Create the time stamp -->
<tstamp/>
<!-- Create directory structures -->
<mkdir dir="${build}"/>
<mkdir dir="${lib}"/>
<mkdir dir="${dist}/lib"/>
<mkdir dir="${reports}"/>
<mkdir dir="${reports}/raw/"/>
<mkdir dir="${reports}/html/"/>
<mkdir dir="${tmp}"/>
</target>
<target name="all" depends="clean, test">
</target>
I give up and hope that someone can shine a light on my problem.
Thanks a lot!
You haven't got any Ant target which builds the test classes. You could do this as part of the compile target if you wanted:
<target name="compile" depends="init" description="compile the source code " >
<javac srcdir="${src}" destdir="${build}">
<classpath>
<fileset dir="lib">
<include name="**/*.jar"/>
</fileset>
</classpath>
</javac>
<javac srcdir="${tests}" destdir="${build}">
<classpath>
<pathelement path="${build}"/>
<fileset dir="lib">
<include name="**/*.jar"/>
</fileset>
</classpath>
</javac>
</target>

Categories