简体   繁体   English

使用Ivy下载JUnit以供Ant使用

[英]Using Ivy to download JUnit for use by Ant

I have a currently private Java project which I'm trying to get into shape for use by other developers. 我有一个目前私有的Java项目,我正在努力使其成形以供其他开发人员使用。 One of the things I'm trying to do is to have Ant itself download the JARs needed for things like JUnit, PMD and FindBugs, so developers don't have to install it themselves. 我要尝试做的一件事是让Ant自己下载JUnit,PMD和FindBugs之类的东西所需的JAR,因此开发人员不必自己安装它。 So I've deinstalled the RPM packages for all those things (to make sure I'm using the Ivy downloaded versions) and I've got Ivy working just fine to download the JUnit related JAR files. 因此,我已经为所有这些操作卸载了RPM软件包(以确保使用的是Ivy下载的版本),并且Ivy可以很好地下载与JUnit相关的JAR文件。 However, I'm having two (possibly related) problems: 但是,我遇到两个(可能是相关的)问题:

1) If I put in a taskdef for the junit task, I get the warning Trying to override old definition of datatype junit . 1)如果我为junit任务添加了taskdefjunit收到警告, Trying to override old definition of datatype junit However, if I don't do that, I get the error: 但是,如果我不这样做,则会收到错误消息:

failed to create task or type junit
Cause: the class org.apache.tools.ant.taskdefs.optional.junit.JUnitTask was not found.

2) When I do override the junit taskdef, I get this error when trying to run JUnit tests: 2)当我确实重写junit taskdef时,尝试运行JUnit测试时出现此错误:

junit.framework.AssertionFailedError: No tests found

Doing a search for that error says that it happens for tests that were written for JUnit 3.x when being run under 4.x. 搜索该错误表明,在4.x下运行时,为JUnit 3.x编写的测试会发生这种情况。 However, my JUnit tests all use the 4.x @Test way of doing tests, the tests ran just fine when I was using the RPM package provided JUnit 4.11 JAR files, and the JUnit JAR files downloaded by Ivy are version 4.11 for JUnit and 1.9.4 for ant-junit. 但是,我的JUnit测试全部使用4.x @Test进行测试的方式,当我使用提供JUnit 4.11 JAR文件的RPM软件包时,测试运行得很好,而Ivy下载的JUnit JAR文件的版本为JUnit和4.11。 1.9.4 for ant-junit。 There is a mismatch between the RPM package installed Ant I'm using (1.9.2) and the Ant JAR files downloaded by Ivy (1.9.4), but I don't think that's the problem. 我正在使用的Ant(1.9.2)中安装的RPM软件包与Ivy(1.9.4)下载的Ant JAR文件之间不匹配,但是我不认为这是问题所在。

My junit taskdef is: 我的junit taskdef是:

<taskdef name="junit"
    classname="org.apache.tools.ant.taskdefs.optional.junit.JUnitTask">
        <classpath location="lib/ant-junit.jar"/>
        <classpath location="lib/junit.jar"/>
        <classpath location="lib/hamcrest-core.jar"/>
</taskdef>

Running Ant with debugging turned on produces this output relevvant to the problem: 在调试打开的情况下运行Ant会产生与问题相关的输出:

Finding class org.apache.tools.ant.taskdefs.optional.junit.JUnitTask
Loaded from /BlahBlah/lib/ant-junit.jar org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.class
Class org.apache.tools.ant.Task loaded from parent loader (parentFirst)
Class org.apache.tools.ant.taskdefs.optional.junit.JUnitTask loaded from ant loader (parentFirst)
[...]
Class org.apache.tools.ant.util.FileUtils loaded from parent loader (parentFirst)
Could not load class (org.apache.tools.ant.taskdefs.optional.junit.JUnitTask) for type junit
Could not load class (org.apache.tools.ant.taskdefs.optional.junit.JUnitTask) for type junit
Trying to override old definition of datatype junit
 +Datatype junit org.apache.tools.ant.taskdefs.optional.junit.JUnitTask

So, is there some way to fix this? 那么,有什么办法可以解决这个问题? Or maybe I should take an entirely different approach? 还是我应该采取完全不同的方法?

I suggest using ivy configurations to manage your test classpath. 我建议使用常春藤配置来管理您的测试类路径。 The following example contains a working example: 以下示例包含一个工作示例:

Your ivy file contains a dependency with a configuration mapping to "test": 您的常春藤文件包含一个依赖项,其配置映射到“测试”:

<configurations>
    <conf name="compile" description="Required to compile application"/>
    <conf name="runtime" description="Additional run-time dependencies" extends="compile"/>
    <conf name="test"    description="Required for test only" extends="runtime"/>
</configurations>

<dependencies>
    ..
    ..

    <!-- test dependencies -->
    <dependency org="junit" name="junit" rev="4.11" conf="test->default"/>
</dependencies>

The build file creates a classpath populated using the ivy configuration: 构建文件创建一个使用常春藤配置填充的类路径:

<target name="resolve" depends="...." description="Use ivy to resolve classpaths">
    ..
    ..

    <ivy:cachepath pathid="test.path"    conf="test"/>
</target>

And finally the junit task uses this classpath 最后,junit任务使用此类路径

<target name="test" depends="compile-tests" description="Run unit tests">
    ..
    ..
    <junit printsummary="yes" haltonfailure="yes">
        <classpath>
            <path refid="test.path"/>
            <pathelement path="${classes.dir}"/>
            <pathelement path="${test.classes.dir}"/>
        </classpath>
        ..
        ..
    </junit>
</target>

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

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