agentleFS
hellyguohellyguo/self-ai-spec/skills/java-check-spotbugs/SKILL.md

java-check-spotbugs

Skill8 starsChanged 16 days agoApache-2.0View on GitHub

使用 SpotBugs(含 Find Security Bugs、fb-contrib)对 Java 字节码做缺陷与安全分析。适用于 Java 项目缺陷排查、安全审计、CI 集成。

The file

Files
SKILL.md

SpotBugs Java静态分析技能

本文件中的 templates/... 均指本技能目录下的 templates/ 子目录。

版本兼容性

  • SpotBugs 4.7.3:最高支持Java 17(class文件major version 61)
  • Java 25不兼容,需使用Java 17或更早版本运行分析
java -version
javap -verbose target/classes/com/example/Test.class | grep major

工具位置

SPOTBUGS_HOME=~/app/spotbugs-4.7.3
SPOTBUGS_JAR=$SPOTBUGS_HOME/lib/spotbugs.jar
FIND_SEC_BUGS_SRC=${HOME}/open_source/github/find-sec-bugs/
FB_CONTRIB_SRC=${HOME}/open_source/github/fb-contrib/

执行流程

步骤1:准备字节码

mvn compile          # Maven
gradle classes       # Gradle
javac -d target/classes src/*.java  # 手动

步骤2:执行分析

# 基础分析
java -jar $SPOTBUGS_JAR -textui -output spotbugs-basic.txt target/classes/

# 含安全检测
java -jar $SPOTBUGS_JAR -textui -output spotbugs-security.txt \
  -pluginList $FIND_SEC_BUGS_JAR target/classes/

# 完整分析(所有插件)
java -jar $SPOTBUGS_JAR -textui -output spotbugs-full.txt \
  -pluginList $FIND_SEC_BUGS_JAR:$FB_CONTRIB_JAR target/classes/

步骤3:特定检测器

# 仅安全漏洞
java -jar $SPOTBUGS_JAR -textui -output security-only.txt \
  -pluginList $FIND_SEC_BUGS_JAR -onlyAnalyze ".*Security.*" target/classes/

# 性能和资源问题
java -jar $SPOTBUGS_JAR -textui -output performance.txt \
  -effort:max -visitors "FindDeadLocalStores,FindUnconditionalWait,FindReturnRef" target/classes/

步骤4:生成报告

使用模板 templates/report.md 输出分析报告。

配置与过滤

使用模板 templates/filter.xml 自定义过滤器:

java -jar $SPOTBUGS_JAR -textui -output filtered.txt \
  -exclude filter.xml target/classes/

输出格式

格式命令参数用途
文本-textui默认,终端查看
XML-xmlCI集成
HTML-html可视化报告

检测器分类

插件主要检测典型Pattern
SpotBugs核心空指针、资源泄漏、并发、性能IC_SUPERCLASS_USES_SUBCLASS
Find Security BugsSQL注入、XSS、命令注入SQL_INJECTION, COMMAND_INJECTION
fb-contrib代码坏味道、命名规范LOOP_CAN_BE_WHILE

常用命令

场景命令
查看所有检测器java -jar $SPOTBUGS_JAR -textui -list
分析特定类java -jar $SPOTBUGS_JAR -textui -onlyAnalyze "com.example.security.*" target/classes/
低阈值(更多问题)java -jar $SPOTBUGS_JAR -textui -low target/classes/
高阈值(仅严重)java -jar $SPOTBUGS_JAR -textui -high target/classes/

CI/CD集成

Maven

<plugin>
  <groupId>com.github.spotbugs</groupId>
  <artifactId>spotbugs-maven-plugin</artifactId>
  <version>4.7.3.0</version>
  <configuration>
    <effort>Max</effort>
    <threshold>Low</threshold>
  </configuration>
</plugin>

Gradle

plugins {
  id "com.github.spotbugs" version "5.0.14"
}
spotbugs {
  toolVersion = '4.7.3'
  effort = 'max'
  reportLevel = 'low'
}

故障排除

问题解决方案
Unsupported class file major version使用Java 17或更早版本
ClassNotFoundException检查类路径
No classes specified确认编译后字节码存在
插件加载失败检查插件JAR路径和版本

版本兼容性解决:

export JAVA_HOME=/path/to/java17
java -jar $SPOTBUGS_JAR -textui target/classes/

技能版本: 1.0.0 工具版本: SpotBugs 4.7.3 + Find Security Bugs + fb-contrib 适用项目: Java 17及以下版本项目

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.