2011-08-09 87 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

相關問題