I got some junit tests (in Tester App) that i want to run on a service (Service.jar running parallely). As soon as ant runs Service.jar, it wouldn't return to perform the junit test as expected but just sits there running the Service app. Am i doing something wrong? My build.xml looks like this below..
<target name="compile" depends="clean">
<javac srcdir="${srcdir}" destdir="${testdir}"
verbose="${full-compile}">
<classpath refid="classpath.test"/>
</javac>
</target>
<target name="test" depends="compile,run">
<junit fork="no" haltonfailure="yes">
<classpath refid="classpath.test" />
<formatter type="brief" usefile="false" />
<test name="xyz.Tester.Test1" outfile="result" >
<formatter type="plain"/>
<formatter type="xml"/>
</test>
</junit>
</target>
<target name="run">
<java jar="Service.jar" fork="yes">
</java>
</target>
Moreover, i would like to know if there's a way i can run the Service.jar just within Ant (fork="no") if it's possible as ant wouldn't let me (Cannot execute a jar in non-forked mode. Please set fork='true'.)
Any help would be appreciated. Thanks
By default, ant tasks are executed in sequence. When you need to run something in parallel, you need to explicitly use task parallel - see the samples in its docs - something like:
...
<parallel>
<java jar="Service.jar" fork="yes"/>
<sequential>
<sleep seconds="30"/>
<junit fork="true" forkmode="once" ... >
<java jar="ServiceStopper.jar" fork="yes"/>
</sequential>
</parallel>
...
Alternatively, you can use the spawn attribute on the java task. It causes that your service is spawned in a separate process, and processing continues with next ant command.
Concerning the non-forked jar execution - you can avoid forking if you specify classpath and classname attributes.
Related
I'm solving task related with code coverage analyse using JaCoCo, and I need to describe Ant target to start scanning. Inspite of examples "java" and "junit" tasks highlighted with red color in the following XML:
<target name="jacoco" description="Code coverage reporting" xmlns:jacoco="antlib:org.jacoco.ant">
<jacoco:coverage destfile="jacoco.exe">
<java classname="org.jacoco.examples.HelloJaCoCo" fork="true">
<classpath>
<pathelement location="./bin"/>
</classpath>
</java>
</jacoco:coverage>
<jacoco:coverage>
<junit fork="true" forkmode="once">
<test name="org.jacoco.examples.HelloJaCoCoTest"/>
<classpath>
<pathelement location="./bin"/>
</classpath>
</junit>
</jacoco:coverage>
But at the same time "java" and "junit" are recognized in any other task. I'm lack of experience with Ant, so what should I adjust to make these tasks work?
You are probably not properly defining the tasks prior to trying to running them.
<project name="Example" xmlns:jacoco="antlib:org.jacoco.ant">
<taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml">
<classpath path="path_to_jacoco/lib/jacocoant.jar"/>
</taskdef>
...
</project>
I found this on javacoco's Ant task directory. make sure you are performing this prior to attempting to run those tasks
Below is the code for setting class path in ant.
<path id="build.classpath">
<fileset dir="${lib.dir}">
<include name="**/*.jar" />
</fileset>
</path>
<target name="compile" depends="clean">
<javac srcdir="${src.dir}" destdir="${build.dir}" classpathref="build.classpath" />
</target>
Why do we need to set classpath in ant ?
Think of ant as a framework to run your build tasks - compiling the code, running the (unit) tests, etc. In order to properly compile and execute your (test) code, java will need access to the third party libraries you may be using (e.g., JUnit for running unit tests). The classpath tells java where these JARs are located, so it can use them.
I would like to start Server and the ProxyServer class simultaneously, using Ant tag, is it possible to run the wo classes?
Here is the code I tried but Ant only starts the Server class and does not do anything there after, not sure if there is away in ant to achieve this.
Appreciate your help.
<target name="pxyServer" depends="server">
<echo>Executing Target - Run ProxyServer</echo>
<java classname="pxy.ProxyServer">
<classpath path="staging" />
</java>
</target>
<target name="server">
<echo>Executing Target - RunServer</echo>
<java classname="pxy.Server">
<classpath path="staging" />
</java>
</target>
Your targets are executed sequentially, and since the first one keeps running, the second one never gets the chance to start.
For parallel execution, you can use ant's "parallel" task:
http://ant.apache.org/manual/Tasks/parallel.html
Your modified script should probably look something like this:
<target name="startServerAndProxy">
<echo>Running server and proxy...</echo>
<parallel>
<java classname="pxy.Server">
<classpath path="staging" />
</java>
<java classname="pxy.ProxyServer">
<classpath path="staging" />
</java>
</parallel>
</target>
(Of course, if you're trying to start some third application in parallel, a client for example, then you should also include that one in the "parallel".)
UPDATE:
To start the server and the proxy each in its own console, I don't know if it can be done with the "java" Ant task, but I just tested that it can be done with "exec":
<target name="doit">
<parallel>
<exec executable="cmd" dir="staging">
<arg line="/k start java.exe pxy.Server"/>
</exec>
<exec executable="cmd" dir="staging">
<arg line="/k start java.exe pxy.ProxyServer"/>
</exec>
</parallel>
</target>
I am attempting to write unit tests against a series of RMI interfaces / implementations and am relying on Ant + Junit to facilitate these tests. The main problem is that I need to have the rmiregistry start up at the beginning of my Junit task and close down at the end of execution whether that be failure,error, or success. Ant script:
<project>
<target name="test">
<javac srcdir="Test/src" destdir="Test/bin" />
<junit fork="true" printsummary="true">
<batchtest>
<fileset dir="Test/test">
<include name="**/*Test.*"/>
</fileset>
</batchtest>
</junit>
</target>
</project>
Basically, just set haltonerror="false" and failonerror="false" for the junit task.
This way junit won't fail due to tests. Starting and stopping is done via the exec task. I used taskkill here to kill the rmiregistry.exe.
<exec executable="start"> <--your start command here -->
<arg value="rmiregistry"/>
</exec>
<junit fork="true" printsummary="true" haltonerror="false" failonerror="false" errorproperty="juniterrors" failureproperty="junitfailures">
...
</junit>
<exec executable="taskkill"> <--your shutdown command here -->
<arg value="/IM"/>
<arg value="rmiregistry.exe"/>
</exec>
<fail if="juniterrors"/> <!-- ant can fail now, if desired -->
<fail if="junitfailures"/>
Another option is to use try/catch from ant-contrib, but I don't think this is needed here.
I have an Ant script with a junit target where I want it to start up the VM with a different working directory than the basedir. How would I do this?
Here's a pseudo version of my target.
<target name="buildWithClassFiles">
<mkdir dir="${basedir}/UnitTest/junit-reports"/>
<junit fork="true" printsummary="yes">
<classpath>
<pathelement location="${basedir}/UnitTest/bin"/>
<path refid="classpath.compile.tests.nojars"/>
</classpath>
<jvmarg value="-javaagent:${lib}/jmockit/jmockit.jar=coverage=:html"/>
<formatter type="xml" />
<test name="GlobalTests" todir="${basedir}/UnitTest/junit-reports" />
</junit>
</target>
Have you tried:
<junit fork="true" printsummary="yes" dir="workingdir">
I think the other answers might be overlooking the fact that you want the working directory to be specified, not just that you want to run junit on a particular directory. In other words, you want to make sure that if a test creates a file with no path information, it is from the base directory you are specifying.
Try to pass in the directory you want as a JVM arg to junit, overriding user.dir:
<junit fork="true" ...>
<jvmarg value="-Duser.dir=${desired.current.dir}"/>
....