テストパッケージをjarに固めてみた

フレームワークを自作するときに、共通的なテストユーティリティを提供する場合があった。
その場合、pomの設定でmvn installするとテストパッケージ(src/test配下)をjarに固めることが出来る。

pomの設定(共通的なテストユーティリティ)

クラス、ソースとJavaDocをJarに固める設定。

...

<build>
<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <executions>
            <execution>
                <phase>package</phase>
                <goals>
                    <goal>test-jar</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-javadoc-plugin</artifactId>
        <executions>
            <execution>
                <phase>package</phase>
                <goals>
                    <goal>jar</goal>
                    <goal>test-jar</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-source-plugin</artifactId>
        <executions>
            <execution>
                <phase>package</phase>
                <goals>
                    <goal>jar</goal>
                    <goal>test-jar</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
</plugins>
</build>

...

pomの設定(使用する側)

実際に提供されたテストユーティリティを使う側の設定。
dependencyに先ほど作成したJarの依存設定を追加する。

...

<dependencies>
    <dependency>
        <groupId>com.myco.app</groupId>
        <artifactId>hoge</artifactId>
        <classifier>tests</classifier>
        <scope>test</scope>
    </dependency>
</dependencies>

...

tips

mvn installでパッケージングするが、ビルド時にテストが走ってしまうので、テストをパスしたい場合のコマンド。

テスト対象オプションの「test」に存在しないテストを指定。
テストの失敗を無視する設定「maven.test.failure.ignore」をtrueにする。
※無理やり感は満載です。

$ mvn install -Dtest=none -Dmaven.test.failure.ignore=true

※ちなみに下記でもテストをパスできるが、test用のjarが出来ません。

$ mvn install -Dmaven.test.skip=true