Ant classpath and junit.jar

32,093

Solution 1

The JUnit library resides wherever you tell it to reside. Personally, I would forget entirely about linking against the jar files shipped with Eclipse and instead download the jars directly.

If I have a project, say at path /project then I would try to put the dependency somewhere in that hierarchy, like at /project/test/lib/junit.jar. If my ant build file is then /project/build.xml then it's as simple as adding ./test/lib/junit.jar to the JUnit classpath. Trying to reference an arbitrary location on your machine is fragile (remember, Eclipse could be installed anywhere), particularly when using relative paths (since your project contents could also be stored anywhere).

Solution 2

You can always symlink your jar to another location if you are on linux. This way you could symlink it to a path that does not include the version. For example:

 ln -s plugins/org.junit4_4.5.0.v20090824/junit.jar /wherever/junit.jar

You may want to have a libraries folder outside of the eclipse subdirectories. You could also consider using ivy or maven to manage your dependencies.

I'm not sure why your above example fails, but the error message implies that junit.jar is not found. Are you sure that eclipse.home is set? You could echo it with:

 <echo message="${eclipse.home}">
Share:
32,093
Artium
Author by

Artium

עתודאי

Updated on July 14, 2022

Comments

  • Artium
    Artium almost 2 years

    I have the an build.xml that allows me to run junit tests. Here is the relevant part:

    <path id="JUnit 4.libraryclasspath">
        <pathelement location="../../../../../eclipse/plugins/org.junit4_4.5.0.v20090824/junit.jar"/>
        <pathelement location="../../../../../eclipse/plugins/org.hamcrest.core_1.1.0.v20090501071000.jar"/>
    </path>
    <path id="MyProject.classpath">
        <pathelement location="bin"/>
        <path refid="JUnit 4.libraryclasspath"/>
    </path>
    
    <target name="run_unit_tests" depends="build">
            <mkdir dir="${junit.output.dir}"/>
            <junit printsummary="yes" haltonfailure="no">   
                <classpath refid="MyProject.classpath" />
    
                <formatter type="xml"/>
                <batchtest todir="${junit.output.dir}">
                        <fileset dir="${src}">
                                <include name="**/*Test*.java"/>
                        </fileset>
                </batchtest>
            </junit>
    </target>
    

    If I replace the line:

    <pathelement location="../../../../../eclipse/plugins/org.junit4_4.5.0.v20090824/junit.jar"/>
    

    with

    <pathelement location="${eclipse.home}/plugins/org.junit4_4.5.0.v20090824/junit.jar"/>
    

    The change breaks the classpath. I get the following error:

    The <classpath> for <junit> must include junit.jar if not in Ant's own classpath

    As far as I understand, the location attribute should hold the same value in both cases. So what can be the reason?

    As a side question, this build file will not work on an environment with different junit version (the path will break). Is it possible to add a "general" path to junit.jar?

  • jamesmortensen
    jamesmortensen over 8 years
    +1 for the ${eclipse.home}. Now I can write build.xml scripts that relatively link to Eclipse plugins instead of the hack of hard-coding or copying the libs to the project.