かんがるーさんの日記

最近自分が興味をもったものを調べた時の手順等を書いています。今は Spring Boot をいじっています。

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 へバージョンアップします。

参照したサイト・書籍

  1. SpotBugs
    https://spotbugs.github.io/

  2. spotbugs / spotbugs
    https://github.com/spotbugs/spotbugs

  3. spotbugs / spotbugs-gradle-plugin
    https://github.com/spotbugs/spotbugs-gradle-plugin

目次

  1. build.gradle を変更する

手順

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: ... のメッセージが出力されました。

f:id:ksby:20210905170018p:plain

リンクをクリックしてレポートファイルを開いてみると「可変オブジェクトへの参照を返すことによって内部表現を暴露するかもしれないメソッド」「可変オブジェクトへの参照を取り込むことによって内部表現を暴露するかもしれないメソッド」の2種類の警告が出力されていましたが、コンストラクタインジェクションや getter メソッドを使用していると警告が出ているようで正直検知不要ですね。。。 excludeFilter を設定して検知されないようにします。

f:id:ksby:20210905170128p:plain f:id:ksby:20210905170247p:plain f:id:ksby:20210905170345p:plain f:id:ksby:20210905170439p:plain

最初に現在レポートファイルの定義を以下のように設定しているのですが、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 であることが分かります。

f:id:ksby:20210905174320p:plain f:id:ksby:20210905174405p:plain

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" のメッセージが出力されました。

f:id:ksby:20210905210013p:plain

履歴

2021/09/05
初版発行。