使用ant编译junit测试类时出现问题

发布于 2024-07-27 19:38:21 字数 842 浏览 6 评论 0原文

我在将 junit 与工作 ant build.xml 文件集成时遇到问题。 我的测试类与我的源类位于同一目录中。 当我学习如何使用 ant 时,我只想编译所有源代码和测试类。

我正在使用 eclipse,并且 junit 测试类在通过 eclipse 执行时工作正常。 这意味着使用 junit.jar 和 ant-junit-1.7.0.jar 正确设置了类路径(至少从 eclipse 的角度来看),尽管我不确定后一个 jar 是否绝对必要。

我的文件夹结构是:

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

和 ant 文件仅包含一个目标,只是为了编译 MyClass 和 MyClassTest,我目前不包含任何 junit 任务,并且不介意将构建文件也放在同一目录中:

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

Ant 工作得很好,直到我在我的文件夹中添加了 MyClassTest.java(带注释的 Junit)。 输出是:

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

我的想法是 Ant 无法找到 junit 库。 由于我没有指定类路径,我假设 Ant 会查看与源文件相同的位置来查找它需要的内容...我如何告诉 Ant junit jar 就在那里?

任何想法,真的很感激。 问候

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

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

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

发布评论

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

评论(4

落花浅忆 2024-08-03 19:38:21

是的,您必须指定一个 CLASSPATH。

自从我上次使用 Eclipse 以来已经很长时间了,但我相信您必须右键单击项目根目录,选择“属性”,然后将 JUnit JAR 添加到 CLASSPATH。

当您使用 Ant 进行构建时,它必须知道 CLASSPATH。 看看他们的<路径> 东西。

我是这样做的:

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

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>
っ左 2024-08-03 19:38:21

jar 必须按名称指定,而不是按目录指定。 使用:

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

其中“pathtojar”是包含junit jar的路径。

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.

带上头具痛哭 2024-08-03 19:38:21

实际上我没有任何运气,直到我使用每个 apache 网站的 fetch.xml 来提取 jar 依赖项。 然后它对我来说效果很好。 这就是我所做的:

  1. 在命令行上我进入了 ant 主目录。
  2. 我输入“ant -f fetch.xml -Ddest=system”(将 jar 存储在 Ant 的 lib 目录中)
  3. 它不会运行,因为它希望我在 ant/lib 文件夹中包含 maven-artifact-ant-2.0.4-dep.jar 。
  4. 我在 google 上搜索了这个 jar,并在 jarfinder.com 上找到了它。
  5. 我下载了它并将其放在 Ant Lib 文件夹中,因为它就是在其中查找该文件的。
    然后它提取了它需要的所有文件,我就准备好了。
  6. 然后在命令行中我返回到工作区文件夹并将其重置为 build.xml
    ant -buildfile build.xml
  7. 然后我做了 ant clean、ant 编译(构建成功)和 ant run 并全部工作 - 构建成功。

JUnit 是用 Eclipse 内置的,但使用 ANT 我尝试了 apache 和 ant 文档中的几种解决方案,但这对我有用。
(我使用的是Windows 7)
-埃里克

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

空心空情空意 2024-08-03 19:38:21

您需要定义一个类路径并将其与 javac 任务一起使用。

请参阅:http://ant.apache.org/manual/using.html一些基础知识。

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

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

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