如何在 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.

相关文章