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

发布于 2024-11-28 20:55:22 字数 163 浏览 2 评论 0原文

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

谢谢!

坦率

I have a project with src/main/java and src/test/java structure, and I managed to use maven-jar-plugin to build a jar of the test branch. However, I want to package the test jar so that all the dependencies are resolved. Is there a way I can tell maven-jar-plugin to include the dependencies??

Thanks!

Frank

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

羁拥 2024-12-05 20:55:22

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

我正在使用 Michael-O 提到的 程序集插件 。我的程序集描述符位于 src/main/ assembly/test-jar-with-dependency.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>

I had a similar problem with integration tests I need to run on Hadoop. Our integration tests are located in the test folder of a separate integration test module, so what is required is a test-jar-with-dependencies to make our life easier.

I'm using the assembly plugin as mentioned by Michael-O. My assembly descriptor is located in src/main/assembly/test-jar-with-dependencies.xml and is a modification of the standard jar-with-dependencies descriptor that's part of the plugin:

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

This assembly relies on the test-jar being created as part of the module build. So I added the following to the module's 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>
憧憬巴黎街头的黎明 2024-12-05 20:55:22

您可以这样做:使用 程序集插件 创建一个 jar 程序集,解压依赖项,打包一个新的测试 jar 并将其附加到反应器。你完成了。

打包的描述符可能类似于 this

You can do this: Create a jar assembly with the assembly plugin, have the dependencies unpacked, pack a new test jar and attach it to the reactor. You're done.

The descriptor for the packaging could look like this.

空‖城人不在 2024-12-05 20:55:22

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

In a similar situation I ended up moving my test code to a separate jar and made it depend on the original one. You can use an aggregator project to ensure that tests are run when you build the main jar.

时光与爱终年不遇 2024-12-05 20:55:22

要在程序集中包含测试 jar 依赖项,请指定程序集 debendencySet 的包含过滤器,如下所示:

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

to include a test-jar dependency in your assembly specify the include filter of the assembly debendencySet as bellow:

...
<dependencySet>
    <outputDirectory>/</outputDirectory>
    <includes>
        <include>*:jar:*</include>
        <include>*:test-jar:*</include>
    </includes>
</dependencySet>
...
淡淡绿茶香 2024-12-05 20:55:22

以下适用于 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>

The following worked for 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>

ASSEMBLY FILE

<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>
旧话新听 2024-12-05 20:55:22
    <dependency>
        <groupId>me.wener.xxx</groupId>
        <artifactId>xxx-core</artifactId>
        <version>${xxx.version}</version>
        <type>test-jar</type>
        <!-- <scope>test</scope> -->
    </dependency>

我用它来包含测试 jar。重要的一行是 test-jar。我不确定这是否是您所需要的。

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

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

I use this to include the test jar.the important line is <type>test-jar</type>.I am not sure this is what you need.

3 years ago, but may help others.At least, it helped me. :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文