2011-05-03 80 views
4

我的一个项目需要为生成的JAR文件设置相当复杂的设置,因此我想在package阶段之后运行测试以确保JAR包含它应该包含的内容。包装后运行测试

我该如何做到这一点与Maven 2?

+0

也许这是值得看看的[Maven的故障安全插件](http://maven.apache.org/plugins/maven-failsafe-plugin/) - 不幸的是我从来没有用它自己并能没有提供更多的信息。 – FrVaBe 2011-05-03 07:47:59

回答

3

将您的项目转换为multi-module build。在第一个模块中,构建您的原始项目。在第二个模块中,向第一个模块添加一个依赖项。

这会将第一个JAR添加到类路径中。


更新由OP:这工作,但我不得不把它添加到我的POM:

<plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-surefire-plugin</artifactId> 
      <version>${version.maven-surefire-plugin}</version> 
      <configuration> 
       <useSystemClassLoader>false</useSystemClassLoader> 
      </configuration> 
     </plugin> 

的重要组成部分,是<useSystemClassLoader>false</useSystemClassLoader>。如果没有这个,我的类路径只包含几个VM JAR以及surefire引导程序JAR(其中包含MANIFEST.MF中的测试类路径)。我不知道为什么这个测试类路径不能从它加载的类中看到。

+0

测试工艺?你的意思是另一个模块? – 2011-05-03 07:32:16

+0

是的。一个额外的人工制品将要求您最近从存储库中构建您的lib并对其进行检查。 – martin 2011-05-03 13:27:16

5

您可以使用surefire插件。你需要做的是将一个阶段与一个执行相关联(见下文)。你需要在软件包阶段之后将阶段改为任何你想要的。

 <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-surefire-plugin</artifactId> 
      <configuration> 
       <skip>true</skip> 
      </configuration> 
      <executions> 
       <execution> 
        <id>unittests</id> 
        <phase>test</phase> 
        <goals> 
         <goal>test</goal> 
        </goals> 
        <configuration> 
         <skip>false</skip> 
         <includes> 
          <include>**/**/**/*Test.java</include> 
         </includes> 
        </configuration> 
       </execution> 
      </executions> 
     </plugin> 
+0

你确定打包的jar文件将在测试类路径中而不是在编译类中吗? – FrVaBe 2011-05-03 09:03:45

+1

@K。 Clazen:是的,会的。用'-X'参数运行maven来检查类路径。 – 2011-05-03 09:41:35

+0

@Robert Munteanu在构建jar之后:c:\ me \ workspaces \ default \ do-nothing-project-target \ do-nothing-project-0.0.1-SNAPSHOT.jar'我得到了'[DEBUG] Test Classpath: [调试] c:\ me \ workspaces \ default \ do-nothing-project \ target \ test-classes [DEBUG] C:\ me \ workspaces \ default \ do-nothing-project \ target \ classes [DEBUG] C :\ Users \ me \ .m2 \ repository \ junit \ junit \ 4.8.2 \ junit-4.8.2.jar' - 看起来像编译的类在类路径而不是jar。 – FrVaBe 2011-05-03 10:19:19