使用ant编译junit测试类时出现问题
我在将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的,您必须指定一个 CLASSPATH。
自从我上次使用 Eclipse 以来已经很长时间了,但我相信您必须右键单击项目根目录,选择“属性”,然后将 JUnit JAR 添加到 CLASSPATH。
当您使用 Ant 进行构建时,它必须知道 CLASSPATH。 看看他们的<路径> 东西。
我是这样做的:
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:
jar 必须按名称指定,而不是按目录指定。 使用:
其中“pathtojar”是包含junit jar的路径。
Jars have to be specified by name, not by directory. Use:
Where "pathtojar" is the path containing the junit jar.
实际上我没有任何运气,直到我使用每个 apache 网站的 fetch.xml 来提取 jar 依赖项。 然后它对我来说效果很好。 这就是我所做的:
然后它提取了它需要的所有文件,我就准备好了。
ant -buildfile build.xml
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:
It then pulled all the files it needed and I was set.
ant -buildfile build.xml
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
您需要定义一个类路径并将其与 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.