Spring Boot 2.4.x の Web アプリを 2.5.x へバージョンアップする ( その9 )( SpotBugs を 4.2.1 → 4.4.0 へバージョンアップする )
概要
記事一覧はこちらです。
Spring Boot 2.4.x の Web アプリを 2.5.x へバージョンアップする ( その8 )( Error Prone を 2.5.1 → 2.9.0 へバージョンアップする ) の続きです。
- 今回の手順で確認できるのは以下の内容です。
- SpotBugs を 4.1.1 → 4.4.0 へバージョンアップします。
参照したサイト・書籍
SpotBugs
https://spotbugs.github.io/spotbugs / spotbugs
https://github.com/spotbugs/spotbugsspotbugs / spotbugs-gradle-plugin
https://github.com/spotbugs/spotbugs-gradle-plugin
目次
手順
build.gradle を変更する
plugins { id "java" id "eclipse" id "idea" id "org.springframework.boot" version "2.5.4" id "io.spring.dependency-management" version "1.0.11.RELEASE" id "groovy" id "checkstyle" id "com.github.spotbugs" version "4.7.3" id "pmd" id "net.ltgt.errorprone" version "2.0.2" id "com.gorylenko.gradle-git-properties" version "2.3.1" id "org.seasar.doma.codegen" version "1.4.1" }
- plugins block の以下の点を変更します。
id "com.github.spotbugs" version "4.6.1"
→id "com.github.spotbugs" version "4.7.3"
- プラグインのバージョンを 4.7.3 にすることで SpotBugs のバージョンが 4.4.0 になります。SpotBugs version mapping 参照。
Gradle Tool Window の左上にある「Refresh all Gradle projects」ボタンをクリックして更新した後、clean タスク実行 → Rebuild Project 実行 → build タスクを実行すると spotbugsMain タスクで 90 SpotBugs violations were found. See the report at: ...
のメッセージが出力されました。
リンクをクリックしてレポートファイルを開いてみると「可変オブジェクトへの参照を返すことによって内部表現を暴露するかもしれないメソッド」「可変オブジェクトへの参照を取り込むことによって内部表現を暴露するかもしれないメソッド」の2種類の警告が出力されていましたが、コンストラクタインジェクションや getter メソッドを使用していると警告が出ているようで正直検知不要ですね。。。 excludeFilter を設定して検知されないようにします。
最初に現在レポートファイルの定義を以下のように設定しているのですが、stylesheet = "color.xsl"
を設定した今のレポートファイルでは Bug の code も pattern も出力してくれないことに気づいたので stylesheet = "color.xsl"
を削除します。
spotbugsMain { reports { html { enabled = true stylesheet = "color.xsl" } } }
spotbugsMain タスクだけ実行してレポートファイルを出力し直すと、今回警告が出た Bug の code は EI, EI2、pattern が EI_EXPOSE_REP, EI_EXPOSE_REP2 であることが分かります。
config/spotbugs/exclude.xml を新規作成し、以下の内容を記述します。Bug は pattern で指定することにします。
<?xml version="1.0" encoding="UTF-8"?> <FindBugsFilter xmlns="https://github.com/spotbugs/filter/3.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://github.com/spotbugs/filter/3.0.0 https://raw.githubusercontent.com/spotbugs/spotbugs/3.1.0/spotbugs/etc/findbugsfilter.xsd"> <Match> <Bug pattern="EI_EXPOSE_REP,EI_EXPOSE_REP2"/> </Match> </FindBugsFilter>
build.gradle の spotbugs block に excludeFilter = file("${rootProject.projectDir}/config/spotbugs/exclude.xml")
を追加します。
spotbugs { toolVersion = "${spotbugs.toolVersion.get()}" ignoreFailures = true spotbugsTest.enabled = false excludeFilter = file("${rootProject.projectDir}/config/spotbugs/exclude.xml") } spotbugsMain { reports { html { enabled = true } } }
clean タスク実行 → Rebuild Project 実行 → build タスクを実行すると警告は出力されず "BUILD SUCCESSFUL" のメッセージが出力されました。
履歴
2021/09/05
初版発行。