3
votes

I am having issues integrating junit with a working ant build.xml file. My test class is in the same directory as my source classes. As I am learning how to use ant, I simply want to compile all the source and the test classes.

I am using eclipse and the junit test classes work fine when execute through eclipse. Which means the classpath is set up correctly (at least from eclipse's point of view) with junit.jar and ant-junit-1.7.0.jar, although I am not sure if the latter jar is absolutely necessary.

My folder structure is:

src/code/MyClass.java src/code/MyClassTest.java

and the ant file contain only one target, just to compile MyClass and MyClassTest, I do not include any junit tasks at the moment, and do not mind to have the build files in the same directory as well:

<target name="compile" >
  <javac srcdir="src/" />
</target>

Ant worked fine until I added MyClassTest.java (Junit with annotations) in my folder. The output is:

[javac] C:\....\src\MyClassTest.java:3: package org.junit does not exist
cannot find symbol

My thinking is that somehow Ant can't find the junit libraries. Since I do not specify a classpath, I was assuming that Ant would look at the same location as the source files to find to find what it needs...How can I tell Ant that the junit jars are right there?

Any ideas, are really appreciated. Regards

4
You don't have to use the absolute path, as you did in your comment. It can be relative, but it is relative to the execution location of ant (or the basedir attribute on the project element). And don't put in a forward slash to start the path, or it will look at root and treat it as an absolute path.Yishai

4 Answers

5
votes

Jars have to be specified by name, not by directory. Use:

<javac srcdir="src/" classpath="pathtojar/junit.jar"/>

Where "pathtojar" is the path containing the junit jar.

6
votes

Yes, you do have to specify a CLASSPATH.

It's been a long time since I last used Eclipse, but I believe you have to right click on the project root, choose "Properties", and add the JUnit JAR to the CLASSPATH.

Ant has to know the CLASSPATH when you use it to build. Have a look at their <path> stuff.

Here's how I do it:

    <path id="production.class.path">
        <pathelement location="${production.classes}"/>
        <pathelement location="${production.resources}"/>
        <fileset dir="${production.lib}">
            <include name="**/*.jar"/>
            <exclude name="**/junit*.jar"/>
            <exclude name="**/*test*.jar"/>
        </fileset>
    </path>

    <path id="test.class.path">
        <path refid="production.class.path"/>
        <pathelement location="${test.classes}"/>
        <pathelement location="${test.resources}"/>
        <fileset dir="${test.lib}">
            <include name="**/junit*.jar"/>
            <include name="**/*test*.jar"/>
        </fileset>
    </path>
1
votes

Actually I didn't have any luck until I used fetch.xml per apache website to pull jar dependencies. Then it worked fine for me. This is what I did:

  1. On command line I went to ant home directory.
  2. I typed “ant -f fetch.xml -Ddest=system” (stores jars in Ant's lib directory)
  3. It would not run because it wanted me to have maven-artifact-ant-2.0.4-dep.jar in ant/lib folder.
  4. I did a google search for this jar and found it at jarfinder.com.
  5. I downloaded it and put it in the Ant Lib folder as that is where it was looking for this file. It then pulled all the files it needed and I was set.
  6. Then in command line I went back to my workspace folder and reset it to build.xml ant -buildfile build.xml
  7. I then did ant clean, ant compile (build successful) and ant run and all worked - Build Successful.

JUnit is built in with Eclipse but with ANT I tried several solutions from apache and ant documentation but this is what worked for me. (I am on Windows 7) -Erik

0
votes

You need to define a classpath and use it with the javac task.

See: http://ant.apache.org/manual/using.html for some basics.