簡體   English   中英

如何在maven-shade-plugin創建的Jar中包含測試類?

[英]How to include test classes in Jar created by maven-shade-plugin?

我正在嘗試使用Maven將我的測試類打包到一個帶有依賴項的可執行jar中,但我很難做到這一點。

到目前為止這是我的pom.xml:

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

  • 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

我已經做了各種各樣的嘗試,包括測試課程,但沒有任何工作,我錯過了什么?

編輯:我已經將我的示例項目推送到GitHub,如果有任何人想玩它:) https://github.com/C0deAttack/ExecutableTests

回答很晚,但得到了一個可行的解決方案,可以幫助未來的訪問者這個問題。

我成功使用了一個只使用一個Maven插件的胖罐,包括:

  • 測試類
  • 應用程序代碼類
  • 應用程序代碼所需的外部依賴項(在compile范圍內)
  • 測試代碼所需的外部依賴關系(在test范圍內)

這基本上意味着增加了測試類(及其依賴項)的胖罐。 Maven Jar插件及其test-jar目標不適合這種需求。 Maven Shade插件及其shadeTestJar選項也無濟於事。

那么,如何在Maven中創建一個帶有測試類和外部依賴項的胖罐?

在這種情況下, Maven Assembly Plugin是一個完美的候選者。

這是一個最小的POM樣本:

<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.sample</groupId>
    <artifactId>sample-project</artifactId>
    <version>1.0-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <descriptor>src/main/assembly/assembly.xml</descriptor>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <archive>
                                <manifest>
                                    <mainClass>com.sample.TestMain</mainClass>
                                </manifest>
                            </archive>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

上面的配置還設置了在測試類中定義的主類(用於快速檢查它是否有效,在答案上)。 但這還不夠。

您還需要在src\\main\\assembly文件夾中創建一個描述符文件 ,其中包含以下內容的assembly.xml文件:

<assembly
    xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
    <id>fat-tests</id>
    <formats>
        <format>jar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <dependencySets>
        <dependencySet>
            <outputDirectory>/</outputDirectory>
            <useProjectArtifact>true</useProjectArtifact>
            <unpack>true</unpack>
            <scope>test</scope>
        </dependencySet>
    </dependencySets>
    <fileSets>
        <fileSet>
            <directory>${project.build.directory}/test-classes</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>**/*.class</include>
            </includes>
            <useDefaultExcludes>true</useDefaultExcludes>
        </fileSet>
    </fileSets>
</assembly>

上面的配置是:

  • 設置從test范圍中獲取的外部依賴項(也將采用compile范圍)
  • 設置一個fileset以包含已編譯的測試類作為打包的胖jar的一部分
  • 使用fat-tests分類器設置最終jar(因此你的最終文件將類似於sampleproject-1.0-SNAPSHOT-fat-tests.jar )。

我們怎么測試呢?

建立jar:

mvn clean compile test-compile assembly:single

target文件夾運行:

java -jar sampleproject-1.0-SNAPSHOT-fat-tests.jar

我們將執行main(來自測試類)。 main可以調用其他測試或應用程序代碼(因此在compiletest范圍內都需要外部依賴)並且一切都可以正常工作。


從這樣的main,您還可以調用所有測試用例,如下所示:

  • 創建一個JUni測試套件
  • 將相關測試添加到測試套件中
  • 從普通的Java main調用測試套件

測試套件示例:

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

@RunWith(Suite.class)
@SuiteClasses({ AppTest.class })
public class AllTests {

}

注意:在這種情況下,測試套件僅涉及AppTest樣本測試。

然后你可以有一個主類如下:

import org.junit.internal.TextListener;
import org.junit.runner.JUnitCore;

public class MainAppTest {

    public static void main(String[] args) {
        System.out.println("Running tests!");

        JUnitCore engine = new JUnitCore();
        engine.addListener(new TextListener(System.out)); // required to print reports
        engine.run(AllTests.class);
    }
}

然后,上面的主要部分將執行測試套件,該套件將鏈執行所有已配置的測試。

我以不同的方式解決了我的問題; 使用另外兩個插件。

首先,我使用maven-dependency-plugin來獲取依賴項並在本地解壓縮,然后我使用maven-jar-plugin創建一個test-jar並包含來自解壓縮依賴項的類。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM