Ant,Ivy和JUnit找不到类 – build.xml中出错?

我正在尝试使用Ant,Ivy和JUnit进行简单的(?)测试项目。 基本的想法是Ivy将下载junit.jar然后Ant将使用它。

请注意, junit jar在classpath上 ,否则(在junit任务中没有classpath元素)我看到“的必须包含junit.jar,如果不在Ant自己的类路径中”。 此外,下面给出的类(junit.framework.TestListener)位于junit-4.8.2.jar中。

但是,当我在下面尝试ant test时,我看到:

 test: BUILD FAILED /home/andrew/project/guice/hg/build.xml:33: java.lang.NoClassDefFoundError: junit/framework/TestListener at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(ClassLoader.java:791) ... at org.apache.tools.ant.launch.Launcher.run(Launcher.java:280) at org.apache.tools.ant.launch.Launcher.main(Launcher.java:109) Caused by: java.lang.ClassNotFoundException: junit.framework.TestListener at java.net.URLClassLoader$1.run(URLClassLoader.java:366) at java.net.URLClassLoader$1.run(URLClassLoader.java:355) ... 

所以我猜我的build.xml出了什么问题? 什么?

这是build.xml:

   simple example build file                                        

和编译后的现有目录结构:

 . ├── build │  └── com │  └── isti │  └── example │  ├── AppendToList.class │  ├── DumpToStdout.class │  ├── LimitedCounter.class │  ├── MessageSink.class │  ├── MessageSource.class │  └── SinkToSourceTest.class ├── build.xml ├── dist │  └── lib │  └── example-20130412.jar ├── ivy.xml ├── lib │  ├── junit-4.8.2.jar │  ├── junit-4.8.2-javadoc.jar │  └── junit-4.8.2-sources.jar ├── README.md └── src ├── main │  └── java │  └── com │  └── isti │  └── example │  ├── AppendToList.java │  ├── DumpToStdout.java │  ├── LimitedCounter.java │  ├── MessageSink.java │  └── MessageSource.java └── test └── java └── com └── isti └── example └── SinkToSourceTest.java 

更新顺便说一句, ant -lib lib test (显式给出lib目录)有效。 在随机网页搜索结果中有很多关于处理这个问题的混淆描述 – 但我的印象是上面的方法与最新的文档一致(我使用的是ant 1.9) – 见第5点。所以我想这可能是是个bug ; 错误 。

Project包含以下文件:

 ├── build.xml ├── ivy.xml └── src ├── main │  ├── java │  │  └── org │  │  └── demo │  │  └── App.java │  └── resources │  └── log4j.properties └── test └── java └── org └── demo └── AppTest.java 

构建运行如下:

 $ ant Buildfile: /home/mark/Files/Dev/ivy/demo/build.xml resolve: [ivy:resolve] :: Apache Ivy 2.3.0 - 20130110142753 :: http://ant.apache.org/ivy/ :: [ivy:resolve] :: loading settings :: url = jar:file:/home/mark/.ant/lib/ivy.jar!/org/apache/ivy/core/settings/ivysettings.xml [ivy:resolve] :: resolving dependencies :: com.myspotontheweb#demo;working@mark-Lemur-Ultra [ivy:resolve] confs: [compile, runtime, test] [ivy:resolve] found org.slf4j#slf4j-api;1.7.5 in public [ivy:resolve] found org.slf4j#slf4j-log4j12;1.7.5 in public [ivy:resolve] found log4j#log4j;1.2.17 in public [ivy:resolve] found junit#junit;4.11 in public [ivy:resolve] found org.hamcrest#hamcrest-core;1.3 in public [ivy:resolve] :: resolution report :: resolve 347ms :: artifacts dl 14ms --------------------------------------------------------------------- | | modules || artifacts | | conf | number| search|dwnlded|evicted|| number|dwnlded| --------------------------------------------------------------------- | compile | 1 | 0 | 0 | 0 || 1 | 0 | | runtime | 3 | 0 | 0 | 0 || 3 | 0 | | test | 5 | 0 | 0 | 0 || 5 | 0 | --------------------------------------------------------------------- [ivy:report] Processing /home/mark/.ivy2/cache/com.myspotontheweb-demo-compile.xml to /home/mark/Files/Dev/ivy/demo/build/ivy-reports/com.myspotontheweb-demo-compile.html [ivy:report] Processing /home/mark/.ivy2/cache/com.myspotontheweb-demo-runtime.xml to /home/mark/Files/Dev/ivy/demo/build/ivy-reports/com.myspotontheweb-demo-runtime.html [ivy:report] Processing /home/mark/.ivy2/cache/com.myspotontheweb-demo-test.xml to /home/mark/Files/Dev/ivy/demo/build/ivy-reports/com.myspotontheweb-demo-test.html resources: [copy] Copying 1 file to /home/mark/Files/Dev/ivy/demo/build/classes compile: [javac] Compiling 1 source file to /home/mark/Files/Dev/ivy/demo/build/classes compile-tests: [mkdir] Created dir: /home/mark/Files/Dev/ivy/demo/build/test-classes [javac] Compiling 1 source file to /home/mark/Files/Dev/ivy/demo/build/test-classes test: [mkdir] Created dir: /home/mark/Files/Dev/ivy/demo/build/test-reports [junit] Running org.demo.AppTest [junit] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 0.085 sec build: [ivy:retrieve] :: retrieving :: com.myspotontheweb#demo [ivy:retrieve] confs: [runtime] [ivy:retrieve] 3 artifacts copied, 0 already retrieved (512kB/16ms) [jar] Building jar: /home/mark/Files/Dev/ivy/demo/build/dist/demo.jar BUILD SUCCESSFUL Total time: 4 seconds 

的ivy.xml

常春藤的一个非常强大的function是配置 。 这些允许您将依赖项组合在一起。

                 

笔记:

  • 配置使用“扩展”function来模拟Maven“编译”,“运行时”和“测试”Maven范围。
  • 请注意每个依赖项上的特殊“conf”属性。 这是从本地到远程的映射。 有关如何通过常春藤管理远程Maven模块的更多详细信息,请参阅:常春藤如何将maven范围映射到常春藤配置

build.xml文件

Ivy配置可以通过cachepath (创建ANT路径)和检索 (将文件复制到您的构建中)等任务来利用。 我还建议使用报告目标,以便您可以看到每个配置中出现哪些jar(对于管理传递依赖性很有用)

                                                                                          

注意:

  • 条件“安装常春藤”目标将自动安装常春藤。 只需重新运行构建,只需要完成一次。

App.java

Hello world日志记录示例。

 package org.demo; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * Hello world! * */ public class App { static final Logger log = LoggerFactory.getLogger(App.class); public static void main( String[] args ) { App a = new App(); a.speak("hello world"); } public void speak(String message) { log.info(message); } } 

AppTest.java

这是我档案中的一个老例子。 不使用Junit断言。

 ackage org.demo; import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; /** * Unit test for simple App. */ public class AppTest extends TestCase { /** * Create the test case * * @param testName name of the test case */ public AppTest( String testName ) { super( testName ); } /** * @return the suite of tests being tested */ public static Test suite() { return new TestSuite( AppTest.class ); } /** * Rigourous Test :-) */ public void testApp() { assertTrue( true ); } } 

log4j.properties

 # Set root logger level to DEBUG and its only appender to A1. log4j.rootLogger=DEBUG, A1 # A1 is set to be a ConsoleAppender. log4j.appender.A1=org.apache.log4j.ConsoleAppender # A1 uses PatternLayout. log4j.appender.A1.layout=org.apache.log4j.PatternLayout log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n