我有一個使用src/main/java和src/test/java結構的項目,並且我設法使用maven-jar-plugin構建了一個測試分支的jar。但是,我想打包測試jar,以便解決所有依賴關係。有沒有一種方法可以告訴maven-jar-plugin包含依賴關係?如何使測試jar在Maven中包含依賴關係?
謝謝!
弗蘭克
我有一個使用src/main/java和src/test/java結構的項目,並且我設法使用maven-jar-plugin構建了一個測試分支的jar。但是,我想打包測試jar,以便解決所有依賴關係。有沒有一種方法可以告訴maven-jar-plugin包含依賴關係?如何使測試jar在Maven中包含依賴關係?
謝謝!
弗蘭克
在類似的情況下,我最終將測試代碼移動到一個單獨的jar中,並使其依賴於原始測試代碼。您可以使用聚合器項目來確保在構建主jar時運行測試。
實際情況是我需要將jar傳遞給Hadoop,以便只能在Hadoop中運行的單元測試使用hadoop jar
你可以這樣做:創建一個assembly plugin罐總成,具有解壓的依賴,收拾了新的試驗瓶,並將其連接到反應器中。你完成了。
包裝的描述符可能看起來像this。
我遇到了類似的問題,需要在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>
這應該是可接受的解決方案! – Ichthyo
<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年前,但可以幫助他人。至少,它幫助了我。 :-)
這個問題不是關於如何使用測試罐,而是關於如何創建一個 –
包括在您的組裝測試-JAR依賴指定包括裝配debendencySet的過濾波紋管:
...
<dependencySet>
<outputDirectory>/</outputDirectory>
<includes>
<include>*:jar:*</include>
<include>*:test-jar:*</include>
</includes>
</dependencySet>
...
以下對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>
注意額外的
你的問題不清楚。你是否想要將所有的測試依賴包括在測試jar中,或者你是否希望能夠正確解析爲測試代碼? –
我想添加依賴項到測試jar中,這樣我就可以在測試jar中調用一個類,並且不需要classpath上的任何其他jar。 – Frank
[maven-assembly-plugin](http://maven.apache.org/plugins/maven-assembly-plugin/)應該提供一種方法來實現你想要的。 –