Ant classpath and junit.jar

2019-03-18 00:30发布

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?

2条回答
啃猪蹄的小仙女
2楼-- · 2019-03-18 01:01

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).

查看更多
一纸荒年 Trace。
3楼-- · 2019-03-18 01:09

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}">
查看更多
登录 后发表回答