0

自上一天以来,我一直在努力解决这个问题。我们使用 Weblogic Workshop 81(WLW) 作为 IDE。我尝试在 eclipse 中运行 junit,它没有问题(没有taskdef)。

我在 WLW 上尝试了以下方法

第一种方法:我使用了几乎相同的 ant 文件(如 eclipse)在 WLW 中构建,但是在使用如下 taskdef 时无法找到JunitTask类,它抱怨如下类

Cannot parse Ant build file: ..... taskdef class org.apache.tools.ant.taskdefs.optional.junit.JUnitTask cannot be found

构建.xml

<project>
 ....
    <path id="MyProject.classpath">
        <pathelement location="${output.directory}/ant-optional.jar" />
        <pathelement location="${output.directory}/junit.jar" />
        <fileset dir="${output.directory}">
            <include name="**/*.jar" />
        </fileset>
        <fileset dir="${platformhome.local.directory}\server\lib\">
            <include name="**/*.jar" />
        </fileset>

        <pathelement path="${class.path}"/>
        <dirset dir="${dest.path}">         
        </dirset>
    </path>

    <path id="classpath.test">
        <fileset dir="C:\tools\Build\ant\apache-ant-1.8.2\lib">
            <include name="**/*.jar" />
        </fileset>
    </path>

    <taskdef name="junit"
      classname="org.apache.tools.ant.taskdefs.optional.junit.JUnitTask" classpathref="MyProject.classpath">      
    </taskdef>

    <target name="test">      

        <echo>In Test</echo>  
        <mkdir dir="${junit.output.dir}" />
        <junit>
            <classpath refid="MyProject.classpath">
            </classpath>           
            <batchtest todir="${junit.output.dir}">
                <formatter type="plain" usefile="false"/>
                <formatter type="plain" />
                <fileset dir="${src.path}">
                    <include name="**/*Test*.java" />
                </fileset>
            </batchtest>    
        </junit>
    </target>
</project>

我通过在命令提示符下的构建中使用 -diagnostic 标志验证了 ant home(使用 web-logic 使用的 ant 版本,所以我正在测试正确的 ant 版本)我得到以下响应

------- Ant diagnostics report -------
Apache Ant version 1.5.3 compiled on August 13 2003

-------------------------------------------
 Implementation Version (JDK1.2+ only)
-------------------------------------------
core tasks     : null
optional tasks : 1.5.3

-------------------------------------------
 ANT_HOME/lib jar listing
-------------------------------------------
ant.home: C:\tools\@#$@##$\bea\weblogic81\server\bin\\..
ant-optional.jar (671546 bytes)
avalon-framework.jar (62694 bytes)
batik.jar (2111580 bytes)
debugging.jar (274343 bytes)
EccpressoAsn1.jar (61543 bytes)
EccpressoCore.jar (133746 bytes)
EccpressoJcae.jar (107821 bytes)
ejbgen.jar (766896 bytes)
fop.jar (1479760 bytes)
jconn2.jar (909569 bytes)
jConnect.jar (764285 bytes)
JDIProxy.jar (86647 bytes)
jms450.jar (24470 bytes)
jms451.jar (25749 bytes)
jms500.jar (26572 bytes)
jms51-interop.jar (4720 bytes)
jms510.jar (26572 bytes)
jsafeFIPS.jar (404439 bytes)
junit.jar (121070 bytes)

这意味着 ANT_HOME 具有ant-optional.jar&junit.jar具有 JunitTask.java 负责从 ant 运行单元测试。

我什至在项目的类路径中添加了上面提到的 jar,所以它会与项目一起编译。

第二种方法这次我从 ant 文件中删除了taskdef,以查看 ant 是否能够从类路径中自行找到junit任务。它给出了以下异常。

ERROR: Could not create task or type of type: junit.
ERROR: Ant could not find the task or a class this task relies upon.
ERROR: This is common and has a number of causes; the usual 
ERROR: solutions are to read the manual pages then download and
ERROR: install needed JAR files, or fix the build file: 

有人可以为我提供这方面的指示。谢谢。

4

1 回答 1

1

我想你需要阅读一下classpath, bootstrap class loader and ant's class loader。请参阅此处并浏览三个答案<junit>-and-<classpath>

你似乎也失踪了ant-junit.jar

从文档。在这里,执行以下操作之一:

  • 将 junit.jar 和 ant-junit.jar 放在 ANT_HOME/lib 中。
  • 不要将它们放在 ANT_HOME/lib 中,而是将它们的位置包含在您的 CLASSPATH 环境变量中。
  • 使用 -lib 将两个 JAR 添加到您的类路径中。
  • 使用构建文件中的元素指定两个 JAR 的位置。
  • 将 ant-junit.jar 保留在 ANT_HOME/lib 中的默认位置,但将 junit.jar 包含在传递给 . (从 Ant 1.7 开始)

另外,作为参考,这里有一个工作build.xml如果需要,您可以参考。

于 2014-07-21T22:46:00.340 回答