How to include test classes in Jar created by maven-shade-plugin?(如何在 maven-shade-plugin 创建的 Jar 中包含测试类?)
问题描述
我正在尝试使用 Maven 将我的测试类打包到一个带有依赖项的可执行 jar 中,但我很难做到这一点.
I'm trying to package my test classes in to an executable jar with dependencies using Maven, but I'm struggling to get this right.
到目前为止,这是我的 pom.xml:
This is my pom.xml so far:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.c0deattack</groupId>
<artifactId>executable-tests</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>executable-tests</name>
<dependencies>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.21.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<finalName>cucumber-tests</finalName>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>cucumber.cli.Main</mainClass>
</transformer>
</transformers>
<artifactSet>
<includes>
<include>info.cukes:*</include>
</includes>
</artifactSet>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>com.c0deattack</groupId>
<artifactId>executable-tests</artifactId>
<version>1.0</version>
<type>test-jar</type>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
当我执行 mvn clean package 时,构建会创建 3 个 jar:
When I execute mvn clean package the build creates 3 jars:
- executable-tests-1.0.jar//由mvn包阶段构建
- executable-tests-1.0-teststjar//由 jar-plugin 构建
- cucumber-tests.jar//由 shade-plugin 构建
Cucumber-tests.jar 包含 info.cuke 依赖项,但不包含 executable-tests-1.0-tests.jar.
Cucumber-tests.jar contains the info.cuke dependencies but it doesn't contain the executable-tests-1.0-tests.jar.
我做了各种尝试并尝试包含测试类,但没有任何效果,我错过了什么?
I've done all sorts of things to try and have the test classes included but nothing has worked, what am I missing?
我已经将我的示例项目推送到 GitHub,如果有人喜欢玩它的话:) https://github.com/C0deAttack/ExecutableTests
I've pushed my example project to GitHub if any one fancies playing around with it :) https://github.com/C0deAttack/ExecutableTests
推荐答案
我用不同的方式解决了我的问题;使用另外两个插件.
I've solved my problem a different way; using two other plugins.
首先我使用 maven-dependency-plugin 获取依赖并在本地解包,然后我使用 maven-jar-plugin 创建一个 test-jar 并包含解压后的依赖项中的类.
First I use the maven-dependency-plugin to get the dependencies and unpack them locally, then I use the maven-jar-plugin to create a test-jar and include the classes from the unpacked dependencies.
这篇关于如何在 maven-shade-plugin 创建的 Jar 中包含测试类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 maven-shade-plugin 创建的 Jar 中包含测试类?
基础教程推荐
- 从 python 访问 JVM 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
- 大摇大摆的枚举 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
- Java Swing计时器未清除 2022-01-01
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
- 多个组件的复杂布局 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01
- 验证是否调用了所有 getter 方法 2022-01-01
