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

时间:2011-08-09 18:15:09

标签: maven jar dependency-management

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

谢谢!

6 个答案:

答案 0 :(得分:30)

我在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

<!-- 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>

答案 1 :(得分:2)

您可以这样做:使用assembly plugin创建一个jar程序集,解压缩依赖项,打包一个新的测试jar并将其附加到reactor。你已经完成了。

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

答案 2 :(得分:0)

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

我用它来包含测试jar。重要的一行是<type>test-jar</type>。我不确定这是你需要的。

3年前,但可能会帮助别人。至少,它对我有所帮助。 : - )

答案 3 :(得分:0)

在程序集中包含一个test-jar依赖项,指定程序集debendencySet的include过滤器如下:

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

答案 4 :(得分: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>

汇编文件

<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>

答案 5 :(得分:-1)

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

相关问题