如何在Ant Junit任务中显示类路径构建

时间:2013-07-18 21:41:17

标签: ant

一个已经工作了几个月的Ant junit任务突然失败,对于以前找到的类,NoClassDefFoundError。有没有办法显示在junit任务中构建的类路径?

<target name="basic-junit-test" description="Run a single JUnit test. ">

    <junit printsummary="yes" fork="no" haltonfailure="yes">
        <classpath>
            <pathelement location="target/WEB-INF/lib/log4j-1.2.16.jar"/> 
            .
            . many other pathelements
            .
        </classpath>
        <test name="com.mycompany.command.TestUNLOCKACCOUNTCommand" outfile="${report.dir}/junit_test_results" />
    </junit>
</target>

3 个答案:

答案 0 :(得分:5)

我非常喜欢在我的构建顶部声明我的Ant路径并在各种任务中使用类路径引用。

To pathconvert任务可用于将类路径内容打印为属性:

<path id="test.path">
    <pathelement location="target/WEB-INF/lib/log4j-1.2.16.jar"/> 
    .
    . many other pathelements
    .
</path>

<target name="echo-path" description="Echo test path">
    <pathconvert targetos="unix" property="test.path.unix" refid="test.path">
    <echo message="Test path: ${test.path.unix}"/>
</target>

<target name="basic-junit-test" depends="echo-path" description="Run a single JUnit test. ">

    <junit printsummary="yes" fork="no" haltonfailure="yes">
        <classpath>
            <path refid="test.path"/>
        </classpath>
        <test name="com.mycompany.command.TestUNLOCKACCOUNTCommand" outfile="${report.dir}/junit_test_results" />
    </junit>
</target>

更新

刚刚想到:一个更简单的解决方案可能是在调试模式下运行Ant。

答案 1 :(得分:0)

这不是我的问题的答案,Mark O'Connor和Rebse给出了很好的答案,相反,这是对我刚才提出的问题的更彻底的解释。我有一个ANT Junit任务,我用它来开发大约100个控制器类。我在几个月内第一次使用它,并且每个测试都因classNotFound异常而失败。找不到的类是我确定应该在类路径上的类,它是一个本地创建的jar文件,它会自动为构建选择。我认为类路径有问题所以我想在测试运行时显示它。

经过多次尝试弄清楚发生了什么后,我在生成classNotFound异常的代码周围放了一个try块,我发现本地类不是找不到的类。这导致我搜索lib目录并最终(大约六个小时后)我意识到问题是我用更新的版本替换了旧版本的slf4j-api。依赖于旧版本中的方法但不是新版本的方法。

答案 2 :(得分:0)

Mark的答案对我有所帮助,但只是一个简单的说明,因为我需要在复制答案中的示例时关闭pathconvert xml元素。

<pathconvert targetos="unix" property="test.path.unix" refid="test.path" />