eclipse 用ant编译junit测试类的问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1134939/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 13:53:14  来源:igfitidea点击:

problem compiling a junit test class with ant

javaeclipseantjunit

提问by denchr

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.

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

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.

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

My folder structure is:

我的文件夹结构是:

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

源代码/代码/MyClass.java 源代码/代码/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:

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

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

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

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

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

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

Any ideas, are really appreciated. Regards

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

回答by duffymo

Yes, you do have to specify a CLASSPATH.

是的,您必须指定一个 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.

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

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

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

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>

回答by Marco Mustapic

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.

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

回答by Erik

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:

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

  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.
  1. 在命令行上,我去了 ant 主目录。
  2. 我输入了“ant -f fetch.xml -Ddest=system”(将 jars 存储在 Ant 的 lib 目录中)
  3. 它不会运行,因为它希望我在 ant/lib 文件夹中有 maven-artifact-ant-2.0.4-dep.jar。
  4. 我在谷歌上搜索了这个 jar 并在 jarfinder.com 上找到了它。
  5. 我下载了它并将它放在 Ant Lib 文件夹中,因为它就是在那里寻找这个文件。然后它拉出它需要的所有文件,我就准备好了。
  6. 然后在命令行中我回到我的工作区文件夹并将其重置为 build.xml ant -buildfile build.xml
  7. 然后我做了 ant clean、ant compile(构建成功)和 ant run 并且一切正常 - 构建成功。

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

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

回答by Nate

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

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

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

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