FAQ: Ant build fails because JUnit task is undefined
When an ant script uses JUnit tasks you might experience that the build fails with the message:
When the build fails Ant writes the following error message:
BUILD FAILED
...build.xml:15: The following error occurred while executing this line:
...build.xml:42: Could not create task or type of type: junit.
Read this article to learn about the cause and how to fix it in your environment.When the build fails Ant writes the following error message:
BUILD FAILED
...build.xml:15: The following error occurred while executing this line:
...build.xml:42: Could not create task or type of type: junit.
Ant could not find the task or a class this task relies upon.
This is common and has a number of causes; the usual
solutions are to read the manual pages then download and
install needed JAR files, or fix the build file:
- You have misspelt 'junit'.
Fix: check your spelling.
- The task needs an external JAR file to execute
and this is not found at the right place in the classpath.
Fix: check the documentation for dependencies.
Fix: declare the task.
- The task is an Ant optional task and the JAR file and/or libraries
implementing the functionality were not found at the time you
yourself built your installation of Ant from the Ant sources.
Fix: Look in the ANT_HOME/lib for the 'ant-' JAR corresponding to the
task and make sure it contains more than merely a META-INF/MANIFEST.MF.
If all it contains is the manifest, then rebuild Ant with the needed
libraries present in ${ant.home}/lib/optional/ , or alternatively,
download a pre-built release version from apache.org
- The build file was written for a later version of Ant
Fix: upgrade to at least the latest release version of Ant
- The task is not an Ant core or optional task
and needs to be declared using <taskdef>.
- You are attempting to use a task defined using
<presetdef> or <macrodef> but have spelt wrong or not
defined it at the point of use
Remember that for JAR files to be visible to Ant tasks implemented
in ANT_HOME/lib, the files must be in the same directory or on the
classpath
Please neither file bug reports on this problem, nor email the
Ant mailing lists, until all of these causes have been explored,
as this is not an Ant bug.
The problem and solution is already written in this error message and of course it is a classloading problem. The couse is often that if auxilliary classes for the junit task (namely ant-unit.jar) are loaded once, this jar file cannot be added dynamically to the classpath afterwards. But this jar file is always in $ANT_HOME/lib and so it is loaded when starting Ant from there.
How to fix the problem
Eclipse environment
Modify the classpath of your ant runtime configuration. You can access the dialog when you open the context menu of the build script in the Ant view and choose "Run As -> Ant build". You might see two entries named "Ant build", one of them opens the dialog. In this dialog choose the "Classpath" view and add junit.jar to the classpath.
Console / other environments
The simplest way is to copy the jar files that the junit task depends on (especially junit.jar) to $ANT_HOME/lib. For these environments the build script itself can be modified by using the right classpaths for task definition. That's the better way.
Thanks to Wolfgang Frank and Thorsten Kamann for the hints.

