eclipse 另一个 Ant + JUnit 类路径问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2805574/
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 14:43:50  来源:igfitidea点击:

Yet another Ant + JUnit classpath problem

javaeclipseantjunitclasspath

提问by DeX3

I'm developing an Eclipse SWT application using Eclipse. There are also some JUnit 4 tests, which test some DAO's. But when I try to run the tests via an ant build, all of the tests fail, because the test classes aren't found.

我正在使用 Eclipse 开发 Eclipse SWT 应用程序。还有一些 JUnit 4 测试,用于测试一些 DAO。但是当我尝试通过 ant 构建运行测试时,所有测试都失败了,因为没有找到测试类。

Google brought up about a million of people who all have the same problem, but none of their solutions seem to work for me -.- .

谷歌提出了大约一百万人都有同样的问题,但他们的解决方案似乎都不适合我-.-。

These are the contents of my build.xml file:

这些是我的 build.xml 文件的内容:

<property name="test.reports" value="./test/reports" />
<property name="classes" value="build" />


<path id="project.classpath">
    <pathelement location="${classes}" />
</path>

<target name="testreport">
    <mkdir dir="${test.reports}" />
    <junit fork="yes" printsummary="no" haltonfailure="no">
        <batchtest fork="yes" todir="${test.reports}" >
            <fileset dir="${classes}">
                <include name="**/Test*.class" />
            </fileset>
        </batchtest>
        <formatter type="xml" />

        <classpath refid="project.classpath" />


    </junit>

    <junitreport todir="${test.reports}">
        <fileset dir="${test.reports}">
            <include name="TEST-*.xml" />
        </fileset>
        <report todir="${test.reports}" />
    </junitreport>
</target>

The test classes are in the build-directory together with the application classes, although they are in some subfolders according to their packages.

测试类与应用程序类一起位于构建目录中,尽管它们根据其包位于一些子文件夹中。

Maybe this is important too: At first Ant complained that JUnit wasn't in its classpath, but since I put it there (with the eclipse configuration editor) it complains about JUnit being in its classpath twice.

也许这也很重要:起初 Ant 抱怨 JUnit 不在它的类路径中,但是自从我把它放在那里(使用 eclipse 配置编辑器)它两次抱怨 JUnit 在它的类路径中。

WARNING: multiple versions of ant detected in path for junit 
   [junit]          jar:file:C:/Users/as df/Documents/eclipse/plugins/org.apache.ant_1.7.1.v20090120-1145/lib/ant.jar!/org/apache/tools/ant/Project.class
   [junit]      and jar:file:/C:/Users/as%20df/Documents/eclipse/plugins/org.apache.ant_1.7.1.v20090120-1145/lib/ant.jar!/org/apache/tools/ant/Project.class

I've tried specifying each and every subdirectory, each and every class file, I've tried filesets and filelists, nothing seems to work.

我试过指定每个子目录、每个类文件,我试过文件集和文件列表,但似乎没有任何效果。

Thanks for your help, I've been sitting for hours on this thing now...

谢谢你的帮助,我已经在这件事上坐了几个小时了......

采纳答案by padma

I had the same problem 'multiple versions of ant detected in path for junit'. The problem went away when I renamed by Eclipse_Home directory and removed special characters from it. The path had '[1]' in it which was causing the problem.

我有同样的问题“在junit的路径中检测到多个版本的蚂蚁”。当我通过 Eclipse_Home 目录重命名并从中删除特殊字符时,问题就消失了。路径中有“[1]”,这导致了问题。

回答by Droj

I was getting this only when using the 'fork=true' option to the junit task. It was happening because my ANT_HOME had '..' in it (e.g. '/3rdparth/jboss/jboss-5/../tools'). Once I reduced that path, the 'multiple versions of ant' warning went away.

我只有在对 junit 任务使用 'fork=true' 选项时才得到这个。这是因为我的 ANT_HOME 中有“..”(例如“/3rdparth/jboss/jboss-5/../tools”)。一旦我减少了这条路径,“蚂蚁的多个版本”警告就消失了。

回答by duffymo

This Ant build.xml works fine for me. Check out the properties to see if the directory structure matches yours; adjust as needed.

这个 Ant build.xml 对我来说很好用。检查属性以查看目录结构是否与您的匹配;根据需要进行调整。

<?xml version="1.0" encoding="UTF-8"?>
<project name="xslt-converter" basedir="." default="package">

    <property name="version" value="1.6"/>
    <property name="haltonfailure" value="no"/>

    <property name="out" value="out"/>

    <property name="production.src" value="src"/>
    <property name="production.lib" value="lib"/>
    <property name="production.resources" value="config"/>
    <property name="production.classes" value="${out}/production/${ant.project.name}"/>

    <property name="test.src" value="test"/>
    <property name="test.lib" value="lib"/>
    <property name="test.resources" value="config"/>
    <property name="test.classes" value="${out}/test/${ant.project.name}"/>

    <property name="exploded" value="out/exploded/${ant.project.name}"/>
    <property name="exploded.classes" value="${exploded}/WEB-INF/classes"/>
    <property name="exploded.lib" value="${exploded}/WEB-INF/lib"/>

    <property name="reports.out" value="${out}/reports"/>
    <property name="junit.out" value="${reports.out}/junit"/>
    <property name="testng.out" value="${reports.out}/testng"/>

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

    <path id="testng.class.path">
        <fileset dir="${test.lib}">
            <include name="**/testng*.jar"/>
        </fileset>
    </path>

    <available file="${out}" property="outputExists"/>

    <target name="clean" description="remove all generated artifacts" if="outputExists">
        <delete dir="${out}" includeEmptyDirs="true"/>
        <delete dir="${reports.out}" includeEmptyDirs="true"/>
    </target>

    <target name="create" description="create the output directories" unless="outputExists">
        <mkdir dir="${production.classes}"/>
        <mkdir dir="${test.classes}"/>
        <mkdir dir="${reports.out}"/>
        <mkdir dir="${junit.out}"/>
        <mkdir dir="${testng.out}"/>
        <mkdir dir="${exploded.classes}"/>
        <mkdir dir="${exploded.lib}"/>
    </target>

    <target name="compile" description="compile all .java source files" depends="create">
        <!-- Debug output
                <property name="production.class.path" refid="production.class.path"/>
                <echo message="${production.class.path}"/>
        -->
        <javac srcdir="src" destdir="${out}/production/${ant.project.name}" debug="on" source="${version}">
            <classpath refid="production.class.path"/>
            <include name="**/*.java"/>
            <exclude name="**/*Test.java"/>
        </javac>
        <javac srcdir="${test.src}" destdir="${out}/test/${ant.project.name}" debug="on" source="${version}">
            <classpath refid="test.class.path"/>
            <include name="**/*Test.java"/>
        </javac>
    </target>

    <target name="junit-test" description="run all junit tests" depends="compile">
        <!-- Debug output
                <property name="test.class.path" refid="test.class.path"/>
                <echo message="${test.class.path}"/>
        -->
        <junit printsummary="yes" haltonfailure="${haltonfailure}">
            <classpath refid="test.class.path"/>
            <formatter type="xml"/>
            <batchtest fork="yes" todir="${junit.out}">
                <fileset dir="${test.src}">
                    <include name="**/*Test.java"/>
                </fileset>
            </batchtest>
        </junit>
        <junitreport todir="${junit.out}">
            <fileset dir="${junit.out}">
                <include name="TEST-*.xml"/>
            </fileset>
            <report todir="${junit.out}" format="frames"/>
        </junitreport>
    </target>

    <taskdef resource="testngtasks" classpathref="testng.class.path"/>
    <target name="testng-test" description="run all testng tests" depends="compile">
        <!-- Debug output
                <property name="test.class.path" refid="test.class.path"/>
                <echo message="${test.class.path}"/>
        -->
        <testng classpathref="test.class.path" outputDir="${testng.out}" haltOnFailure="${haltonfailure}" verbose="2" parallel="methods" threadcount="50">
            <classfileset dir="${out}/test/${ant.project.name}" includes="**/*.class"/>
        </testng>
    </target>

    <target name="exploded" description="create exploded deployment" depends="testng-test">
        <copy todir="${exploded.classes}">
            <fileset dir="${production.classes}"/>
        </copy>
        <copy todir="${exploded.lib}">
            <fileset dir="${production.lib}"/>
        </copy>
    </target>

    <target name="package" description="create package file" depends="exploded">
        <jar destfile="${out}/${ant.project.name}.jar" basedir="${production.classes}" includes="**/*.class"/>
    </target>

</project>

回答by Naren Sankarlal

multiple versions of ant detected in path for junit - Because of this, JVM getting exited ab

在 junit 的路径中检测到多个版本的 ant - 因此,JVM 退出 ab