34

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が作成されます。

  • 実行可能ファイル-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

4

2 に答える 2

23

回答は遅くなりましたが、この質問の将来の訪問者に役立つ可能性のある実用的な解決策が得られました。

私は、Mavenプラグインを1つだけ使用して、次のものを含むファットjarを作成することに成功しました。

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

これは基本的に、テストクラス(およびそれらの依存関係)が追加されたファットジャーを意味します。Maven Jarプラグインとその目標test-jarは、このニーズに適合しません。MavenシェードプラグインとそのオプションshadeTestJarも役に立ちません。

では、Mavenでテストクラスと外部依存関係を持つファットjarを作成するにはどうすればよいですか?

この場合、 Mavenアセンブリプラグインは最適な候補です。

最小限の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\assemblyassembly.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パッケージ化されたfatjarの一部としてコンパイルされたテストクラスを含めるように設定する
  • 分類子を使用して最終jarを設定しfat-testsます(したがって、最終ファイルは次のようになります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メインからテストスイートを起動します

テストスイートの例:

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

上記のメインは、構成されたすべてのテストをチェーンで実行するテストスイートを実行します。

于 2016-03-17T10:50:35.883 に答える
5

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

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

于 2012-05-02T21:11:15.893 に答える