2011-08-09 114 views
9

我有一个使用src/main/java和src/test/java结构的项目,并且我设法使用maven-jar-plugin构建了一个测试分支的jar。但是,我想打包测试jar,以便解决所有依赖关系。有没有一种方法可以告诉maven-jar-plugin包含依赖关系?如何使测试jar在Maven中包含依赖关系?

谢谢!

弗兰克

+0

你的问题不清楚。你是否想要将所有的测试依赖包括在测试jar中,或者你是否希望能够正确解析为测试代码? –

+0

我想添加依赖项到测试jar中,这样我就可以在测试jar中调用一个类,并且不需要classpath上的任何其他jar。 – Frank

+0

[maven-assembly-plugin](http://maven.apache.org/plugins/maven-assembly-plugin/)应该提供一种方法来实现你想要的。 –

回答

0

在类似的情况下,我最终将测试代码移动到一个单独的jar中,并使其依赖于原始测试代码。您可以使用聚合器项目来确保在构建主jar时运行测试。

+0

实际情况是我需要将jar传递给Hadoop,以便只能在Hadoop中运行的单元测试使用hadoop jar myMainClass运行。它目前没有在单元测试的jar类中找到... – Frank

2

你可以这样做:创建一个assembly plugin罐总成,具有解压的依赖,收拾了新的试验瓶,并将其连接到反应器中。你完成了。

包装的描述符可能看起来像this

+0

这并不完美。这个jar与之前的大小差不多,我仍然不能执行它的主类,但是NoClassDefFoundError异常。我看着焦油,实际上它不包含有问题的班级。任何想法? – Frank

+0

我很抱歉,我误解了你的要求。看我的编辑。 –

+0

我必须了解什么是“包装”和“反应堆”。我最近刚开始工作:-)感谢您的帮助。 – Frank

21

我遇到了类似的问题,需要在Hadoop上运行集成测试。我们的集成测试位于单独集成测试模块的test文件夹中,因此需要的是一个test-jar-with-dependencies以使我们的生活更轻松。

我正在使用Michael-O提到的assembly plugin。我的装配描述符位于src/main/assembly/test-jar-with-dependencies.xml并且是标准jar-with-dependencies描述是这样的插件部分的修改:

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd"> 
    <id>test-jar-with-dependencies</id> 
    <formats> 
     <format>jar</format> 
    </formats> 
    <includeBaseDirectory>false</includeBaseDirectory> 
    <dependencySets> 
     <dependencySet> 
      <outputDirectory>/</outputDirectory> 
      <useProjectArtifact>true</useProjectArtifact> 
      <!-- we're creating the test-jar as an attachement --> 
      <useProjectAttachments>true</useProjectAttachments> 
      <unpack>true</unpack> 
      <scope>test</scope> 
     </dependencySet> 
    </dependencySets> 
</assembly> 

该组件依赖于test-jar创建的模块构建的一部分。因此,我增加了以下内容模块的pom.xml:我用这个来包括测试jar.the重要线

<!-- create a complete jar for testing in other environments --> 
<plugin> 
    <groupId>org.apache.maven.plugins</groupId>      
    <artifactId>maven-jar-plugin</artifactId> 
    <executions> 
     <execution> 
      <goals> 
       <goal>test-jar</goal> 
      </goals> 
     </execution> 
    </executions> 
</plugin> 
<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-assembly-plugin</artifactId> 
    <configuration> 
     <descriptors> 
      <descriptor>src/main/assembly/test-jar-with-dependencies.xml</descriptor> 
     </descriptors> 
    </configuration> 
    <executions> 
     <execution> 
      <phase>package</phase> 
      <goals> 
       <goal>single</goal> 
      </goals> 
     </execution> 
    </executions> 
</plugin> 
+0

这应该是可接受的解决方案! – Ichthyo

0
<dependency> 
     <groupId>me.wener.xxx</groupId> 
     <artifactId>xxx-core</artifactId> 
     <version>${xxx.version}</version> 
     <type>test-jar</type> 
     <!-- <scope>test</scope> --> 
    </dependency> 

<type>test-jar</type>。我是不知道这是你所需要的。

3年前,但可以帮助他人。至少,它帮助了我。 :-)

+0

这个问题不是关于如何使用测试罐,而是关于如何创建一个 –

0

包括在您的组装测试-JAR依赖指定包括装配debendencySet的过滤波纹管:

... 
<dependencySet> 
    <outputDirectory>/</outputDirectory> 
    <includes> 
     <include>*:jar:*</include> 
     <include>*:test-jar:*</include> 
    </includes> 
</dependencySet> 
... 
0

以下对Maven 3

的pom.xml

工作
<plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-jar-plugin</artifactId> 
      <version>2.6</version> 
      <executions> 
       <execution> 
        <goals> 
         <goal>test-jar</goal> 
        </goals> 
        <phase>test-compile</phase> 
       </execution> 
      </executions> 
     </plugin> 
     <plugin> 

汇编文件

<dependencySet> 
     <outputDirectory>demo/test-lib</outputDirectory> 
     <includes> 
      <!--test only dependencies (like netty)-->  
      <include>io.netty:netty-all</include> 
      <!-- the actual test jar--> 
      <include>${project.groupId}:${project.artifactId}:test-jar</include> 
     </includes> 
     <useProjectAttachments>true</useProjectAttachments> 
     <scope>test</scope> 
    </dependencySet> 
+0

注意额外的 Ori

相关问题