my dog learned polymorphism*
The moose likes Ant, Maven and Other Build Tools and the fly likes <junit> problem with Ant Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Engineering » Ant, Maven and Other Build Tools
Bookmark "&lt;junit&gt; problem with Ant" Watch "&lt;junit&gt; problem with Ant" New topic
Author

&lt;junit&gt; problem with Ant

Unnsse Khan
Ranch Hand

Joined: Nov 12, 2001
Posts: 511
Hello there,

I have an Ant build script where I placed the following target:



Now, when I run this target by using "ant tests", this is what I get in response:

----------------------------------------------------

BUILD FAILED
F:\DevProjects\JavaProjects\sellcars\build.xml:51: 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.

----------------------------------------------------

Questions:

(1) What am I possibly doing wrong? Under my ANT_HOME/lib directory, a junit.jar existed...

In addition, in this particular line:



(2) How do I specify where my TestShoppingCart test case is residing? I mean, if I set a property to ${test.dir} directory, right underneath my project's root, would this work?



Many, many thanks!
Sol Mayer-Orn
Ranch Hand

Joined: Nov 13, 2002
Posts: 311
I've been struggling with the same thing myself.
Apparently, it's a ClassLoader issue, and it's made worse by the fact that they've changed the class loading policy between Ant 1.5.x and 1.6.x....

Apache have a FAQ on this (see list below).
One thing that worked for me (on Ant 1.6.5) was to call
> ant -lib SOME_DIR/junit-3.8.1.jar
When you run it this way, the junit jar should *not* appaer in the < classpath > element inside the build.xml file. It also should *not* appear in ANT_HOME/lib.


Couple of useful links:

http://ant.apache.org/faq.html#delegating-classloader

http://wiki.osuosl.org/display/howto/Running+JUnit+Tests+from+Ant+without+making+classpath+changes
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16158
    
  21

It's a royal pain is what it is!

There are two fairly simple solutions.

1. Include junit in the CLASSPATH in effect when you invoke Ant. I don't like this because it's extra meta-information that I wilol sooner or late forget to supply when I need it most.

2. Drop a copy of the junit.jar into ANT_HOME/lib, thereby ensuring that it will be part of Ant's classpath at all times (or at least as long as you use that copy of Ant).

Since you also need junit.jar in the javac classpath, you can use case 2 and specify <pathelement location="${ant.home}/lib/junit.jar"/> as part of the test-compile classpath. Or something like that, at any rate.


Customer surveys are for companies who didn't pay proper attention to begin with.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: &lt;junit&gt; problem with Ant