0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Java】バグパターン検出ツール「SpotBugs」を組み込んでレポート出力する方法

0
Posted at

今回は、SpotBugsを使ったバグパターンの検出の設定とレポート出力までしてみたいと思います。

ビルドツールはMavenを使います。

SpotBugs公式サイトよりプラグインをpom.xmlに入れる

下記の公式サイトに記載のある通り、プラグインをpom.xmlに追加します。

修正後のpom.xmlのキャプチャ👇
image.png

修正後の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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>4.0.1</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>Sample</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>Sample</name>
	<description>Demo project for Spring Boot</description>
	<url/>
	<licenses>
		<license/>
	</licenses>
	<developers>
		<developer/>
	</developers>
	<scm>
		<connection/>
		<developerConnection/>
		<tag/>
		<url/>
	</scm>
	<properties>
		<java.version>21</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-webmvc</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<scope>runtime</scope>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-webmvc-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<annotationProcessorPaths>
						<path>
							<groupId>org.projectlombok</groupId>
							<artifactId>lombok</artifactId>
						</path>
					</annotationProcessorPaths>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<excludes>
						<exclude>
							<groupId>org.projectlombok</groupId>
							<artifactId>lombok</artifactId>
						</exclude>
					</excludes>
				</configuration>
			</plugin>
			<plugin>
	            <groupId>com.github.spotbugs</groupId>
	            <artifactId>spotbugs-maven-plugin</artifactId>
	            <version>4.8.3.0</version>
	            <configuration>
	                <effort>Max</effort>
	                <threshold>Low</threshold>
	                <xmlOutput>true</xmlOutput>
	                <!--HTML出力-->
                	<htmlOutput>true</htmlOutput>
	            </configuration>
        	</plugin>
		</plugins>
	</build>

</project>

今回は、HTMLレポートも出力したいので下記のようにしました。

pom.xml
<plugin>
    <groupId>com.github.spotbugs</groupId>
    <artifactId>spotbugs-maven-plugin</artifactId>
    <version>4.8.3.0</version>
    <configuration>
        <effort>Max</effort>
        <threshold>Low</threshold>
        <xmlOutput>true</xmlOutput>
        <!--HTML出力-->
        <htmlOutput>true</htmlOutput>
    </configuration>
</plugin>

バグパターンのコードを書いてみる

つづいて、バグが混在しているコードを作ってみます。
👉 このコードにより、SpotBugsは
「nullチェックなしで参照している」という警告を出します。

SampleApplication.java
package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SampleApplication {

	public static void main(String[] args) {
		SpringApplication.run(SampleApplication.class, args);
		SampleApplication app = new SampleApplication();
		app.printLength(null);
	}
	// SpotBugsが検出しやすいバグ
    public void printLength(String text) {
        System.out.println(text.length()); // ← NullPointerExceptionの可能性
    }
}

SpotBugsを実行する

プロジェクトの上で右クリックして[コマンドプロンプト]を選択します。
image.png

コマンドプロンプトが起動したら、下記のコマンドを実行します。

mvn spotbugs:spotbugs

正常に完了するとコマンドプロンプトに下記が表示されます。
image.png

Eclipseのディレクトリを見てみましょう。
下記のフォルダが追加されているかと思います。
image.png

下記のようにレポートが出力出来たら完了です。
image.png

サイト

SpotBugs公式サイト

CheckStyle公式サイト

Checkstyle 使い方メモ

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?