如何让测试jar包含Maven中的依赖项?

Fra*_*ank 12 jar dependency-management maven

我有一个带有src/main/java和src/test/java结构的项目,我设法使用maven-jar-plugin构建了一个测试分支的jar.但是,我想打包测试jar,以便解决所有依赖项.有没有办法告诉maven-jar-plugin包含依赖项?

谢谢!

坦率

eri*_*opf 32

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

我正在使用Michael-O提到的汇编插件.我的汇编描述符位于插件中,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>
Run Code Online (Sandbox Code Playgroud)

此程序集依赖于test-jar作为模块构建的一部分创建的程序集.所以我在模块中添加了以下内容pom.xml:

<!-- 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>
Run Code Online (Sandbox Code Playgroud)


Nic*_*tti 0

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