LoginSignup
3
2

More than 5 years have passed since last update.

Spring Boot + Jenkinsのpom.xml設定例

Posted at

はじめに

このpom.xmlの設定例は、設定した内容の備忘録となります。

またこのpom.xmlは、『Spring徹底入門 Spring FrameworkによるJavaアプリケーション開発』と『Java本格入門』のサンプルコードを参考にIntelliJ IDEAで作成しました。

構成

システム

  • Spring Boot
  • Thymeleaf
  • PostqreSQL
  • JPA

Jenkinsで出力するレポート

  • Checkstyle
  • FindBugs
  • JUnit
  • JaCoCo

※JavaDocも出力するようにしていますがJenkinsのレポートにはまだ出力できるようにはしていません。

pom.xml例

pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>sample</groupId>
    <artifactId>sample</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>mrs</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <tomcat.version>8.5.15</tomcat.version>
        <thymeleaf.version>3.0.2.RELEASE</thymeleaf.version> <!-- ① -->
        <thymeleaf-layout-dialect.version>2.0.0</thymeleaf-layout-dialect.version> <!-- ① -->
        <assertj-core.version>3.6.2</assertj-core.version> <!-- ② -->
    </properties>

    <repositories>
        <repository> <!-- ③ -->
            <id>gemfire-repository</id>
            <name>Gemfire Repository</name>
            <url>http://repo.spring.io/plugins-release/</url> 
        </repository>
    </repositories>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency> <!-- ① -->
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-java8time</artifactId>
            <version>3.0.0.RELEASE</version>
        </dependency>
        <dependency> <!-- ① -->
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity4</artifactId>
            <version>3.0.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <pluginManagement>
            <plugins>
                <plugin> <!-- ④ -->
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.20</version>
                    <configuration>
                        <argLine>-Xmx256m ${jacocoArgs}</argLine>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-checkstyle-plugin</artifactId>
                <version>2.17</version>
                <dependencies>
                    <dependency> <!-- ⑤ -->
                        <groupId>com.puppycrawl.tools</groupId>
                        <artifactId>checkstyle</artifactId>
                        <version>7.7</version>
                    </dependency>
                </dependencies>
            </plugin>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.7.9</version>
                <executions> <!-- ⑥ -->
                    <execution>
                        <id>prepare-agent</id>
                        <phase>test-compile</phase>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                        <configuration>
                            <propertyName>jacocoArgs</propertyName>
                            <includes>
                                <include>*</include>
                            </includes>
                        </configuration>
                    </execution>
                    <execution>
                        <id>report</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <reporting>
        <plugins>
            <plugin> <!-- ⑦ -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-project-info-reports-plugin</artifactId>
                <version>2.9</version>
                <configuration>
                    <dependencyLocationsEnabled>false</dependencyLocationsEnabled>
                </configuration>
                <reportSets>
                    <reportSet>
                        <reports>
                            <report>index</report>
                            <report>license</report>
                            <report>summary</report>
                            <report>dependencies</report>
                        </reports>
                    </reportSet>
                </reportSets>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-checkstyle-plugin</artifactId>
                <version>2.17</version>
                <configuration>
                    <configLocation>config/checkstyle/google_checks.xml</configLocation>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>findbugs-maven-plugin</artifactId>
                <version>3.0.4</version>
                <configuration> <!-- ⑧ -->
                    <xmlOutput>true</xmlOutput>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.7.9</version>
                <reportSets>
                    <reportSet>
                        <reports>
                            <report>report</report>
                        </reports>
                    </reportSet>
                </reportSets>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <version>2.10.4</version>
                <configuration>
                    <source>${java.version}</source>
                    <encoding>${project.build.sourceEncoding}</encoding>
                    <docencoding>UTF-8</docencoding>
                    <charset>UTF-8</charset>
                </configuration>
                <reportSets>
                    <reportSet> <!-- ⑨ -->
                        <reports>
                            <report>javadoc</report>
                        </reports>
                    </reportSet>
                </reportSets>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jxr-plugin</artifactId>
                <version>2.5</version>
                <reportSets>
                    <reportSet> <!-- ⑩ -->
                        <reports>
                            <report>jxr</report>
                        </reports>
                    </reportSet>
                </reportSets>
            </plugin>
        </plugins>
    </reporting>
</project>

設定内容の説明

①Thymeleafのバージョン指定

Spring Bootの最新バージョンでは、Thymeleafのデフォルトが2だったので、3系を利用するためにVersionを指定しました。

②AssertJの利用

JUnit4のアサーションライブラリをAssertJに変更した。
AssertJの方がメソッドチェーンによりIDEの補完をフル活用できるのでテストコードを書くのに慣れていない我々には向いていると思ったため。
AssertJ 使い方メモ

尚、spring-boot-starter-testのDependencyにAssertJが組み込まれているようなので、propertiesで指定すれば利用できる模様。

③Gemfireリポジトリを追加

GemfireプラグインがMavenリポジトリに無く、警告が出ていたため。
Gemfire自体はSpring Bootが利用している?(あくまで警告を無くすことが目的だったので、詳細は調べていません。)

④Surefireプラグインの設定

JaCoCoのレポートを出力する際の警告に対応するために記載しました。
詳細は下記のリンク先を参照してください。
JaCoCoとMavenと何か

⑤Checkstyleのバージョン指定

MavenのCheckstyleプラグインでは、Checkstyleのバージョンが6.11.2だったので、7.7で動作するようにVersionを追加しました。
Upgrading Checkstyle at Runtime

⑥JaCoCoのレポート出力準備

Testフェーズでjacoco.exeを出力するため。
jacoco.exeからレポートを出力する。
尚、Jenkinsでレポートを表示させるだけなら、jacoco.exeが出力できればよくreport出力定義は不要だが、localで確認したい場合を考えてreportは出力できるようにする。

⑦site実行時のdependencyLocationsEnabledの指定

mvn siteを実行するとリポジトリロケーションの解析に時間が掛る。mvn site -Ddependency.locations.enabled=falseで実行すれば無効化できるが、いちいち入力したくなかったため設定を追加。
maven の依存プロダクトのリポジトリロケーションの解析が遅いので無効化する

また出力したいreportを制限するようにした。
Apache Maven Project Info Reports Plugin

⑧FindBugsの出力レポート指定

<xmlOutput>true</xmlOutput>は、findbags.xmlを出力するため。
尚、上記設定に関係なくfindbagsXml.xmlというファイルが出力される。このファイルの用途などについてはまだ調べていません。

⑨TestコードをJavaDocで生成しない設定

<report>javadoc</report>を指定することで、mainディレクトリのソースのJavadocだけを生成することができる。指定していないとtestディレクトリのソースに対してもJacadocが生成されてしまう。

⑩jxrプラグインの設定

jxrは、projectのソースを相互参照するためのplugin。site実行時にソースコードをhtmlページで表示することができる。
Maven JXR Plugin
これもtestソースはhtmlかしないように<report>jxr</report>を指定した。

※このpluginを入れた本当の目的は、site実行時に以下の警告を回避するため。
[WARNING] Unable to locate Source XRef to link to - DISABLED
(checkstyleのレポート生成時で警告が発生している行がLinkになって該当ソースに飛べるのだが、ここでjxrを利用していると思われる。)

まとめ

この設定でJenkinsでCheckstyle,JUnit,JaCoCoの結果が確認できた。
(FindBugsについてはコミットされているソースでエラーが無い状態だったのでレポートの出力確認はできていないが恐らく出力される。)

未解決

man site実行時に以下の警告が出力される。
[WARNING] Unable to find a URL to the parent project. The parent menu will NOT be added.

出力内容やタイミングから、親Pomになるspring-boot-starter-parentの設定と関係がありそうだが、未確認です。

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2