Problem compiling junit test class with ant

I'm having problems integrating junit with the ant build.xml working file. My test class is in the same directory as my source classes. Since I am learning how to use ant, I just want to compile all the source and test classes.

I use eclipse and the junit test classes work fine when executed through eclipse. This means that the class path is configured correctly (at least in terms of eclipse) using junit.jar and ant -junit-1.7.0.jar, although I'm not sure that the last jar is absolutely necessary.

My folder structure:

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

and the ant file contains only one goal, just to compile MyClass and MyClassTest, at the moment I do not include junit tasks and do not want to have assembly files in the same directory:

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

Ant worked fine until I added MyClassTest.java (Junit with annotations) to my folder. Exit:

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

My thinking is that somehow ant cannot find junit libraries. Since I do not specify the class path, I assumed that ant would search the same place as the source files to find what it needed ... How can I tell ant that junit jars is right there?

Any ideas really appreciated. Relations

+3
source share
4 answers

Boxes should be listed by name, not catalog. Using:

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

"pathtojar" - , junit jar.

+5

, 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>
+6

, fetch.xml apache, jar. . , :

  • ant.
  • "ant -f fetch.xml -Ddest = system" ( ant lib)
  • , , maven-artifact- ant -2.0.4-dep.jar ant/lib.
  • Google jarfinder.com.
  • ant Lib, . , .
  • reset build.xml ant -buildfile build.xml
  • ant clean, ant ( ) ant - Build Successful.

JUnit Eclipse, ant apache ant , , . ( Windows 7) -Erik

+1

Source: https://habr.com/ru/post/1712732/


All Articles