web-dev-qa-db-ja.com

maven-shade-pluginによって作成されたJarにテストクラスを含める方法は?

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パッケージフェーズによってビルドされました
  • 実行可能ファイル-tests-1.0-teststjar // jar-pluginによってビルドされます
  • cucumber-tests.jar //シェードプラグインでビルド

Cucumber-tests.jarにはinfo.cuke依存関係が含まれていますが、executable-tests-1.0-tests.jarは含まれていません。

私はあらゆる種類のことを試し、テストクラスを含めましたが、何も機能しません。何が欠けていますか?

編集:誰かがそれで遊んでいるのが好きなら、私は私のサンプルプロジェクトをGitHubにプッシュしました:) https://github.com/C0deAttack/ExecutableTests

30
C0deAttack

別の方法で問題を解決しました。他の2つのプラグインを使用します。

まず、maven-dependency-pluginを使用して依存関係を取得し、ローカルで解凍します。次に、maven-jar-pluginを使用してtest-jarを作成し、解凍した依存関係のクラスを含めます。

4
C0deAttack

遅く答えますが、この質問の将来の訪問者を助けるかもしれない実用的な解決策を得ました。

私は、1つのMavenプラグインのみを使用してファットジャーを作成することに成功しました。

  • テストクラス
  • アプリケーションコードクラス
  • アプリケーションコードに必要な外部依存関係(compileスコープ内)
  • テストコードで必要な外部依存関係(testスコープ内)

これは基本的に、テストクラス(およびそれらの依存関係)が追加されたファットジャーを意味します。 Maven Jarプラグイン およびその test-jar 目標は、このニーズに適合しません。 Maven Shade Plugin とその 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スコープも取得します)
  • パッケージ化されたfatjarの一部としてコンパイルされたテストクラスを含めるようにfilesetを設定する
  • 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は、他のテストまたはアプリケーションコードを呼び出す可能性があり(したがって、compileまたはtestスコープの両方で外部依存関係が必要になります)、すべてが正常に機能します。


このようなメインから、次のようにすべてのテストケースを呼び出すこともできます。

  • 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);
    }
}

上記のメインは、構成されたすべてのテストを連鎖的に実行するテストスイートを実行します。

18
A_Di-Matteo