简体   繁体   English

用ant编译junit测试类的问题

[英]problem compiling a junit test class with ant

I am having issues integrating junit with a working ant build.xml file. 我在将junit与工作的ant build.xml文件集成时遇到问题。 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. 在我学习如何使用ant时,我只想编译所有源代码和测试类。

I am using eclipse and the junit test classes work fine when execute through eclipse. 我正在使用eclipse,junit测试类在通过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. 这意味着使用junit.jar和ant-junit-1.7.0.jar正确设置了类路径(至少从eclipse的角度来看),尽管我不确定后一个jar是否绝对必要。

My folder structure is: 我的文件夹结构是:

src/code/MyClass.java src/code/MyClassTest.java 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: 并且ant文件只包含一个目标,只是为了编译MyClass和MyClassTest,我目前不包含任何junit任务,并且不介意将构建文件放在同一目录中:

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

Ant worked fine until I added MyClassTest.java (Junit with annotations) in my folder. Ant工作正常,直到我在我的文件夹中添加了MyClassTest.java(带注释的Junit)。 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. 我的想法是,Ant无法找到junit库。 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会查找与源文件相同的位置以查找它需要的内容...我怎么能告诉Ant junit jar就在那里?

Any ideas, are really appreciated. 任何想法,真的很感激。 Regards 问候

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. 当你使用它来构建时,Ant必须知道CLASSPATH。 Have a look at their <path> stuff. 看看他们的<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>

Jars have to be specified by name, not by directory. 必须通过名称而不是目录来指定Jars。 Use: 采用:

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

Where "pathtojar" is the path containing the junit jar. 其中“pathtojar”是包含junit jar的路径。

Actually I didn't have any luck until I used fetch.xml per apache website to pull jar dependencies. 实际上我没有任何运气,直到我使用每个apache网站的fetch.xml来拉取jar依赖。 Then it worked fine for me. 然后它对我来说很好。 This is what I did: 这就是我做的:

  1. On command line I went to ant home directory. 在命令行上我去了ant主目录。
  2. I typed “ant -f fetch.xml -Ddest=system” (stores jars in Ant's lib directory) 我键入“ant -f fetch.xml -Ddest = system”(将jar存放在Ant的lib目录中)
  3. It would not run because it wanted me to have maven-artifact-ant-2.0.4-dep.jar in ant/lib folder. 它不会运行,因为它要我在ant / lib文件夹中有maven-artifact-ant-2.0.4-dep.jar。
  4. I did a google search for this jar and found it at jarfinder.com. 我做了谷歌搜索这个罐子,并在jarfinder.com找到它。
  5. I downloaded it and put it in the Ant Lib folder as that is where it was looking for this file. 我下载并将其放在Ant Lib文件夹中,因为它正在寻找此文件。 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 然后在命令行中我回到我的工作区文件夹并将其重置为build.xml ant -buildfile build.xml
  7. I then did ant clean, ant compile (build successful) and ant run and all worked - Build Successful. 然后我做了ant clean,ant compile(构建成功)和ant run并且全部工作 - 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. JUnit是用Eclipse构建的,但是使用ANT我尝试了几种来自apache和ant文档的解决方案,但这对我有用。 (I am on Windows 7) -Erik (我在Windows 7上)-Erik

You need to define a classpath and use it with the javac task. 您需要定义类路径并将其与javac任务一起使用。

See: http://ant.apache.org/manual/using.html for some basics. 有关基础知识,请参阅: http//ant.apache.org/manual/using.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM