정적 분석 (SpotBugs / SonarQube)
빌드 파이프라인에 SpotBugs 정적 분석과 SonarQube 코드 품질 검사를 연동하여 코드 결함을 조기에 감지한다.
SpotBugs
build.gradle 설정
plugins {
id 'com.github.spotbugs' version '6.0.26'
}
spotbugs {
toolVersion = '4.8.6'
effort = com.github.spotbugs.snom.Effort.MAX // 분석 강도 최대
reportLevel = com.github.spotbugs.snom.Confidence.LOW // 낮은 신뢰도까지 보고
ignoreFailures = true // 빌드 중단 없이 계속
}
spotbugsMain {
reports {
xml {
required = true // Jenkins 연동용
}
html {
required = false
}
}
}
주요 설정 항목
| 항목 | 값 | 설명 |
|---|---|---|
| toolVersion | 4.8.6 | SpotBugs 엔진 버전 |
| effort | MAX | 분석 강도 (MIN / DEFAULT / MAX) |
| reportLevel | LOW | 보고 수준 (LOW: 모든 버그, HIGH: 심각한 버그만) |
| ignoreFailures | true | false 설정 시 버그 발견 시 빌드 실패 처리 |
| 리포트 경로 | build/reports/spotbugs/ | XML/HTML 결과물 위치 |
제외 필터 (선택)
특정 클래스나 패턴을 분석에서 제외할 경우 필터 XML을 작성한다.
<!-- spotbugs-exclude.xml -->
<FindBugsFilter>
<Match>
<Class name="~.*GeneratedCode.*"/>
</Match>
<Match>
<Bug category="EXPERIMENTAL"/>
</Match>
</FindBugsFilter>
spotbugs {
excludeFilter = file('config/spotbugs-exclude.xml')
}
SonarQube
build.gradle 설정
plugins {
id 'org.sonarqube' version '7.0.1.6134'
}
sonar {
properties {
property 'sonar.host.url', 'https://{SONAR_HOST}:{PORT}'
property 'sonar.projectKey', '{프로젝트키}'
property 'sonar.projectName', '{프로젝트명}'
property 'sonar.java.source', '17'
property 'sonar.sourceEncoding', 'UTF-8'
property 'sonar.sources', 'src/main/java'
property 'sonar.tests', 'src/test/java'
}
}
CI/CD 실행 순서
SpotBugs 분석 완료 후 SonarQube에 결과를 함께 전송하는 방식이 일반적이다.
# Gradle 태스크 의존성 설정 (build.gradle)
tasks.named('sonar') {
dependsOn 'spotbugsMain'
}
# Jenkins 파이프라인 실행 예시
./gradlew spotbugsMain sonar -Dsonar.token=${SONAR_TOKEN}
SonarQube 토큰 관리
분석용 토큰은 코드에 포함하지 않고 Jenkins Credentials 또는 환경변수로 주입한다.
# 환경변수 주입 방식
export SONAR_TOKEN=sqa_xxxxxxxxxxxxxxxx
./gradlew sonar -Dsonar.token=${SONAR_TOKEN}
권장 워크플로
빌드(compileJava)
→ SpotBugs 정적 분석(spotbugsMain)
→ SonarQube 업로드(sonar)
→ 결과 리포트 확인
Jenkins BlueOcean 또는 SonarQube Quality Gate를 사용하면 기준치 미달 시 파이프라인을 자동으로 실패 처리할 수 있다.
체크리스트
- [ ]
spotbugs플러그인 버전 명시 - [ ]
effort = MAX,reportLevel = LOW설정 - [ ]
ignoreFailures정책 결정 (CI 초기 도입 시true권장) - [ ] XML 리포트 활성화 (Jenkins 연동용)
- [ ] SonarQube 프로젝트 키 / URL 설정
- [ ] SonarQube 토큰 Jenkins Credentials 등록
- [ ]
sonar태스크가spotbugsMain이후 실행되도록 의존성 설정