Unrecognized tasks in Ant - java

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

Related

java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing despite having hamcrest on classpath

When attempting to run tests with JUnit 5 via the Ant junitlauncher task, I receive the following message:
[junitlauncher] WARNING: TestEngine with ID 'junit-jupiter' failed to execute tests
[junitlauncher] java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing
How do I fix this so that my tests can run?
The answers to similar questions (e.g. this one) mainly say to make sure that hamcrest is on the classpath. I've verified using the print-test-classpath target (see build file below for details) that the classpath I'm using to run my tests includes hamcrest-2.1.jar. Furthermore, the tests compile without any issue.
Here are the sections of my build.xml I think are relevant:
<path id="lib-only-class.path">
<fileset dir="${lib.dir}">
<include name="**/*.jar" />
</fileset>
</path>
<path id="class.path">
<path refid="lib-only-class.path"/>
<pathelement location="${classes.dir}"/>
</path>
<path id="test-class.path">
<path refid="class.path"/>
<pathelement location="${test-classes.dir}"/>
</path>
<target name="compile-tests" depends="compile">
<mkdir dir="${test-classes.dir}"/>
<javac srcdir="${test.dir}" destdir="${test-classes.dir}"
includeantruntime="no" classpathref="class.path"/>
</target>
<target name="test" depends="compile-tests">
<junitlauncher printsummary="yes">
<classpath refid="test-class.path"/>
<testclasses>
<fileset dir="${test-classes.dir}"/>
</testclasses>
</junitlauncher>
</target>
<target name="print-test-classpath">
<pathconvert property="classpathInName" refid="test-class.path"/>
<echo>Classpath is ${classpathInName}</echo>
</target>

Test coverage with JaCoCo and Ant for Sonar

I have adjusted Sonar part to show project fake test coverage - giving mock "jacoco.exec" file, so I need to have real file as a result of project analyse with JaCoCo.
I have Ant "build.xml" and try to implement two approaches for getting goal (both are described here http://eclemma.org/jacoco/trunk/doc/ant.html):
Using Ant's Java agent I have adjusted JVM option
-agentvmparam:${somepath}\\lib\\jacocoagent.jar=destfile=D:\\jacoco.exec
and try to execute in build.xml
<project name="project" default="all" basedir="." xmlns:jacoco="antlib:org.jacoco.ant">
<jacoco:agent property="agentvmparam"/>
Unfortunately, nothing happens.
Using JaCoCo coverage I try to run the following - but java tag is not specified and build failed:
<project name="project" default="all" basedir="." xmlns:jacoco="antlib:org.jacoco.ant">
<target name="jacoco" description="Code coverage reporting" xmlns:jacoco="antlib:org.jacoco.ant">
<jacoco:coverage>
<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>
</target>

Java Ant wont complete task after running a Jar

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.

Getting ant to run java file

I am having trouble just creating something simple, and it does not seem that any website is clear on how to do this, and honestly I think it should be simple.
I have a bunch of java files for a project. I want to compile all of them, and then run each file with specific arguments.
Basically I want the order of operations to be something like this
javac prob1.java
javac prob2.java
java prob1 parameter
java prob2 parameter
But I want that in ant (build.xml).
I can do the compile part just fine with
<project default="compile">
<target name="compile">
<javac srcdir="." />
</target>
</project>
I just can not get it to run say prob1 with an argument. I imagine this is extremely easy, but every solution I have found, does not seem to work. Also note prob1.class and prob2.class are in the same directory.
This should work:
<target name="run">
<java classname="prob1">
<classpath>
<pathelement location="."/>
</classpath>
<arg value="parameter" />
</java>
<java classname="prob2">
<classpath>
<pathelement location="."/>
</classpath>
<arg value="parameter" />
</java>
</target>

How to set the working directory for a <junit> task to something other than the basedir?

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}"/>
....

Categories

Resources