I\'m trying to run ant script from Gradle, but I have this exception:
Cause: the class org.apache.tools.ant.taskdefs.optional.junit.JUnitTask was not found
This is just repost from comment, which solved my problem: Thanks to Opal
Running gradle without net connection may not be a very good idea. Nevertheless if You have the jar You need in the local directory, just add id to script dependencies using file dependencies. See: http://www.gradle.org/docs/current/userguide/dependency_management.html#file-dependencies from Opal
I have fixed the following Gradle error:
Execution failed for task ':test'.
> Problem: failed to create task or type junit
Cause: the class org.apache.tools.ant.taskdefs.optional.junit.JUnitTask was not found.
This looks like one of Ant's optional components.
Action: Check that the appropriate optional JAR exists in
-ANT_HOME/lib
-the IDE Ant configuration dialogs
by adding the following lines in my Ant script build.xml
just before my Ant task <junit>
<taskdef name="junit"
classname="org.apache.tools.ant.taskdefs.optional.junit.JUnitTask"
classpath="/usr/share/ant/lib/ant-junit.jar:/usr/share/ant/lib/ant-junit4.jar"
/>
This is the full Ant target test
:
<target name="test" depends="test_compile">
<mkdir dir="${test.report.dir}" />
<!-- Required for Gradle -->
<taskdef name="junit"
classname="org.apache.tools.ant.taskdefs.optional.junit.JUnitTask"
classpath="/usr/share/ant/lib/ant-junit.jar:/usr/share/ant/lib/ant-junit4.jar"
/>
<junit printsummary="yes" fork="true" haltonfailure="on">
<classpath refid="classpath_test"/>
<batchtest>
<fileset dir="${test.src.dir}">
<include name="**/*Tester.java" />
</fileset>
</batchtest>
</junit>
</target>
The answer about "JUnitTask was not found" from oers has been a useful help to find my above fix.
To understand the issue, I have tweaked the scripts build.xml
and build.gradle
in order to compare the Ant and Gradle outputs. See also the answer of How to get “gradle” have similar output details as “ant”? for some details.
The interesting difference is in the -classpath
parameter:
/some/path/myApplication.jar
/usr/share/java/ant-launcher-1.9.7.jar
/usr/share/ant/lib/ant.jar
/usr/share/ant/lib/junit.jar
/usr/share/ant/lib/ant-junit.jar
/usr/share/ant/lib/ant-junit4.jar
/some/path/myApplication.jar
/usr/lib/gradle/3.2.1/lib/ant-launcher-1.9.6.jar
/usr/lib/gradle/3.2.1/lib/ant-1.9.6.jar
We can observe that Gradle uses its own ant-launcher-1.9.6.jar
and ant-1.9.6.jar
. But Gradle does not use the JARs from Ant installation (/usr/share/ant/lib/*jar
are symbolic links to /usr/share/java/*jar
).
In this discussion about integration of Ant JUnit taskdef, Luke Daley confirms in Novembre 2013;
Gradle uses its own 'instance' of ant and does not bother using what's installed at
$ANT_HOME
In this discussion about JUnit 5 Eric Wendelin expressed the priority to integrate more JUnit within Gradle core.
Threfore, solution for long term is to stop using JUnit from Ant scripts and convert them into Gradle syntax.