Ant 类路径和 junit.jar

发布于 2024-10-13 13:43:21 字数 1599 浏览 2 评论 0原文

我有一个 build.xml,它允许我运行 junit 测试。这是相关部分:

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

如果我将行:替换

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

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

更改会破坏类路径。我收到以下错误:

必须 如果 Ant 自己没有,则包含 junit.jar 类路径

据我了解,位置属性在两种情况下应该保持相同的值。那么原因可能是什么呢?

作为一个附带问题,此构建文件将无法在具有不同 junit 版本的环境中运行(路径将中断)。是否可以向 junit.jar 添加“常规”路径?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

江心雾 2024-10-20 13:43:21

JUnit 库位于您指定的位置。就我个人而言,我会完全忘记链接 Eclipse 附带的 jar 文件,而是直接下载 jar

如果我有一个项目,比如在路径 /project 中,那么我会尝试将依赖项放在该层次结构中的某个位置,例如 /project/test/lib/junit.jar 。如果我的 ant 构建文件是 /project/build.xml 那么就像将 ./test/lib/junit.jar 添加到 JUnit 类路径一样简单。尝试引用计算机上的任意位置是脆弱的(请记住,Eclipse 可以安装在任何地方),特别是在使用相对路径时(因为您的项目内容可以存储< em>任何地方)。

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

笑咖 2024-10-20 13:43:21

如果您使用的是 Linux,则始终可以将 jar 符号链接到另一个位置。这样您就可以将其符号链接到不包含该版本的路径。例如:

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

您可能希望在 eclipse 子目录之外有一个库文件夹。您还可以考虑使用 ivy 或 maven 来管理您的依赖项。

我不确定为什么上面的示例失败,但错误消息暗示找不到 junit.jar 。您确定 eclipse.home 已设置吗?你可以用以下方式回应它:

 <echo message="${eclipse.home}">

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}">
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文