Ant Build file with junit test for java project - ant

I am trying to write an android build file that compiles, creates a jar executes it and also runs a bunch of test files. This is what I have so far, but not sure how to proceed with writing the test block. I have looked around but an example of a build file with junit testing but haven't found any..an example of a ant file with junit would be helpful please
<property name="src.dir" value="src"/>
<property name="build.dir" value="build"/>
<property name="classes.dir" value="${build.dir}/classes"/>
<property name="jar.dir" value="${build.dir}/jar"/>
<property name="test.dir" value="${build.dir}/test"/>
<property name="main-class" value="com.arkangelx.classes.ATMLauncher"/>
<property name="TALK" value="true" />
<target name="clean">
<delete dir="${build.dir}"/>
</target>
<target name="compile">
<mkdir dir="${classes.dir}"/>
<javac srcdir="${src.dir}" destdir="${classes.dir}" verbose="${TALK}"/>
</target>
<target name="jar" depends="compile">
<mkdir dir="${jar.dir}"/>
<jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
<manifest>
<attribute name="Main-Class" value="${main-class}"/>
</manifest>
</jar>
</target>
<target name="test" depends="run">
<mkdir dir="${test.dir}"/>
<test destfile="${test.dir}/${ant.project.name}.test" basedir="${build.dir}">
<junit>
<classpath refid="classpath.test" />
<formatter type="brief" usefile="false" />
<test name="TestExample" />
</junit>
</test>
</target>
<target name="run" depends="jar">
<java jar="${jar.dir}/${ant.project.name}.jar" fork="true"/>
</target>
<target name="clean-build" depends="clean,jar"/>
<target name="main" depends="clean,run"/>

The test element must be a sub-element of the junit task. There are several exemples available in (surprinsingly) the documentation of the junit task.

Related

how to test symlinks with ant 1.9

I need to perform a command if the jar file is a file instead of a symlink. I have found a solution that works only with ant 1.10.
Does anyone know how to do it with ant 1.9 ?
Here is my build.xml.
<?xml version="1.0"?>
<project name="AsterixDecoder" default="bm" basedir=".">
<!-- set global properties for this build -->
<property name="src" location="src"/>
<property name="build" location="build"/>
<property name="dist" location="dist"/>
<property environment="env"/>
<condition property="exists.CCM_ADDR">
<isset property="env.CCM_ADDR"/>
</condition>
<target name="compile" description="compile the source " >
<mkdir dir="${build}"/>
<javac srcdir="${src}" destdir="${build}" includeantruntime="false"/>
<mkdir dir="${build}/resources"/>
<copy todir="${build}/resources">
<fileset dir="resources"/>
</copy>
</target>
<target name="checkout" if="exists.CCM_ADDR">
<ccmcheckout file="${dist}/AsterixDecoder.jar"/>
</target>
<target name="dist" depends="compile, checkout"
description="generate the distribution" >
<jar jarfile="${dist}/AsterixDecoder.jar" filesetmanifest="mergewithoutmain">
<manifest>
<attribute name="Main-Class" value="fr.eurocontrol.escape.ground.asterixdecoder.AsterixDataTree"/>
<attribute name="Class-Path" value="."/>
</manifest>
<fileset dir="${build}"/>
</jar>
</target>
<target name="check.symlink">
<fileset dir="${dist}" id="fileset" includes="AsterixDecoder.jar">
<symlink/>
</fileset>
<pathconvert refid="fileset" property="is.symlink" setonempty="false"/>
</target>
<target name="reconcile" depends="check.symlink" if="exists.CCM_ADDR" unless="is.symlink">
<exec executable="ccm">
<arg value="reconcile"/>
<arg value="-udb"/>
<arg value="${dist}/AsterixDecoder.jar"/>
</exec>
</target>
<target name="bm" description="build management" depends="dist, reconcile">
</target>
</project>
Do not hesitate to make any suggestion of improvements. I am still a beginner in writing ant files.
The most straightforward way to do this would be to use the record function of Ant's symlink task. This creates a property file that lists all of the symlinks found within a given resource collection. Here's an example target:
<target name="default">
<symlink link="testdir" resource="build" />
<symlink action="record" linkfilename="links.record">
<fileset dir="." includes="*" />
</symlink>
<property file="links.record" />
<condition property="testdir.is.symlink">
<isset property="testdir" />
</condition>
<echo message="${testdir.is.symlink}" />
</target>

Ant creates jar files in wrong destination

Below is my build.xml file for ant.
<?xml version="1.0" encoding="UTF-8"?>
<project name="Struts2ProductsDemo" default="jar">
<property name="src.dir" location="src" />
<property name="build.dir" location="L:\build" />
<property name="project.name" value="Struts2ProductsDemo" />
<target name="clean">
<delete dir="${build.dir}" />
</target>
<target name="makedir">
<mkdir dir="${build.dir}" />
<mkdir dir="${build.dir}/classes" />
</target>
<target name="compile" depends="clean,makedir">
<javac srcdir="${src.dir}" destdir="${build.dir}/classes" />
</target>
<target name="jar" depends="compile">
<jar destfile="${build.jar}/jars/${project.name}.jar" basedir="${build.dir}/classes" />
</target>
The jar files are created in: C:\Users\San\EclipseWS\Struts2ProductsDemo\${build.jar}\jars\Struts2ProductsDemo.jar
I am expecting the jar to be here: ${build.jar}/jars/${project.name}.jar
Property build.jar is never set. You have to define it. For example, you may add this to your build:
<property name="build.jar" location="target"/>

Ant Build issue : Additonal folder getting created

need your help at the earliest and I have been breaking my head over this for a while.
Below is my Ant Script.
<property name="build.dir" value="${basedir}/build"/>
<property name="lib.dir" value="${basedir}/lib"/>
<property name="src.dir" value="${basedir}/allocator"/>
<property name="classes.dir" value="${basedir}/classes"/>
<property name="jar.dir" value="${basedir}/jar"/>
<property name="main-class" value="allocator.Allocator"/>
<path id="classpath_ref">
<pathelement path="${basedir}/"/>
<fileset dir="${lib.dir}" includes="*.jar"/>
</path>
<target name="clean">
<delete dir="${classes.dir}" />
<delete dir="${build.dir}" />
<delete dir="${jar.dir}" />
</target>
<target name="compile">
<mkdir dir="${classes.dir}"/>
<javac srcdir="${src.dir}" destdir="${classes.dir}" classpathref="classpath_ref" includeantruntime="false">
<src path="${basedir}/supportlibraries"/>
<src path="${basedir}/com/xyz/framework"/>
</javac>
<copy todir="${build.dir}"><fileset dir="${src.dir}" excludes="**/*.java"/>
</copy>
</target>
<target name="jar" depends="compile">
<mkdir dir="${jar.dir}"/>
<jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${build.dir}">
<manifest>
<attribute name="Main-Class" value="${main-class}"/>
</manifest>
</jar>
</target>
<target name="run" depends="jar">
<java classname="${main-class}" fork="true" >
<arg line="username"/>
<arg line="password"/>
<classpath>
<path refid="classpath_ref"/>
<pathelement location="${jar.dir}/${ant.project.name}.jar"/>
</classpath>
</java>
</target>
<target name="main" depends="clean,run"/>
</project>
when i try to run the ant build from eclipse it is creating an additional folder allocator, supportlibraries and com/xyz/framework inside the class folder. why is it creating additional folder? because of the additional folders when the run target executes its not able to locate the allocator class. Please let me know if i am missing anything
Remove the <copy> from the "compile" <target>.
The <jar> in the "jar" target should be:
<jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
<manifest>
<attribute name="Main-Class" value="${main-class}"/>
</manifest>
</jar>
In the above snippet, basedir is ${classes.dir} instead of ${build.dir}.
I don't see a need for build.dir in your script. All references to it can likely be removed.

Could not find or load main class ant file

<target name="init">
<property name="src.dir" value="src"/>
<property name="build.dir" value="build"/>
<property name="apidoc.dir" value="apidoc"/>
<property name="lib.dir" value="lib"/>
<property name="lib.res" value="resources.jar"/>
<property name="jar.path" value="${build.dir}/AntLabRun.jar"/>
<property name="main.class" value="edu.gatech.oad.antlab.pkg1.AntLabMain"/>
<path id="classpath">
<fileset dir="${lib.dir}" includes="lib.res"/>
</path>
</target>
<target name="jar" depends="compile">
<manifestclasspath property="jar.classpath" jarfile="${jar.path}">
<classpath refid="classpath"/>
</manifestclasspath>
<jar destfile="${jar.path}" basedir="${src.dir}">
<manifest>
<attribute name="Main-Class" value="${main.class}"/>
<attribute name="Class-Path" value="${jar.classpath}" />
</manifest>
</jar>
</target>
<target name="clean" depends="init">
<delete dir="${build.dir}"/>
<delete dir="${apidoc.dir}"/>
</target>
<target name="run" depends="jar">
<java jar="${jar.path}" fork="true"/>
</target>
<target name="all">
<antcall target="init"/>
<antcall target="prepare"/>
<antcall target="compile"/>
<antcall target="javadoc"/>
<antcall target="jar"/>
<antcall target="run"/>
</target>
Every time I run my jar it gives me this error, I don't know what is causing it. Everything else works fine and I've googled around and have found several posts with this same issue but nothing that worked for them seems to be working for me.
Try to add a classname="${main.class}" inside the java tag of the run target.
Also, I think that even when running your program, you need to add the libraries to the classpath.

Unable to find images when running selenium/sikuli scripts using Ant

I've written a couple selenium scripts (Java) and use sikuli to verify the images exist. It worked fine when I ran the tests through Eclipse/TestNG but with Ant I'm getting the following error:
[testng] [error] resources/x.png looks like a file, but can't be found on the disk. Assume it's text.
The following is my build.xml, please let me know if there's more information I can provide.
<project name="test" default="test">
<property name="src.dir" location="src" />
<property name="build.dir" location="build" />
<property name="dist.dir" location="dist" />
<property name="lib.dir" location="lib" />
<path id="build.classpath">
<fileset dir="${lib.dir}">
<include name="**/*.jar" />
</fileset>
</path>
<target name="clean">
<delete dir="${build.dir}" />
</target>
<target name="makedir" depends="clean">
<mkdir dir="${build.dir}" />
</target>
<!-- Compiles the java code -->
<target name="compile" depends="clean, makedir">
<javac srcdir="${src.dir}" destdir="${build.dir}" classpathref="build.classpath"/>
</target>
<!--Creates the deployable jar file -->
<target name="jar" depends="compile">
<jar destfile="${dist.dir}\build.test.ant.jar" basedir="${build.dir}">
<manifest>
<attribute name="Main-Class" value="test.Main" />
</manifest>
</jar>
</target>
<taskdef resource="testngtasks" classpath="${lib.dir}/testng-6.5.2.jar"/>
<target name="test" depends="jar">
<testng
classpath="${build.dir}:${lib.dir}/selenium-java-2.24.1.jar:${lib.dir}/selenium-server-standalone-2.24.1.jar:${lib.dir}/sikuli-script.jar"
outputDir="${testng.report.dir}"
testname="test1">
<xmlfileset dir="." includes="testng.xml" />
</testng>
<fail message="BUILD FAILURE" if="failed" />
</target>
</project>

Resources