문서의 이전 판입니다!
sonar.java.binaries, sonar.java.libraries이 올바로 설정이 안되어 Lombok 등에 대한 false alarm이 발생하게 된다.NOSONAR 보다는 Rule 자체를 변경하는 것이 좋겠다.//NOSONAR시에 항상 왜 그럴 수 밖에 없었는지 이유를 작성하자.
이럴때는 즉각 팀에 공유하고, 다른 방안을 도출하거나, NOSONAR 사유주석 혹은 @SuppressWarnings(“이슈타입”) 형태로 처리한다.
</note>
==== @SuppressWarnings(“all”) ====
* @SuppressWarnings(“all”)를 클래스에 붙이면 해당 클래스의 모든 규칙 위반을 무시한다.
==== NOSONAR ====
* 규칙 위반을 고치는게 불가능할 경우 해당 위반의 줄 맨 끝에 //NOSONAR를 붙인다.
==== @SuppressWarnings(“squid:룰번호”) ====
* @SuppressWarnings({“squid:S2078”, “squid:S2076”}) 처럼 @SuppressWarnings와 squid:S룰번호 를 사용한다.
===== 참고 자료 =====
* Analysis Parameters - SonarQube Documentation - Doc SonarQube
* SonarJava - Plugins - Doc SonarQube
* Sonar's Quality Alphabet
===== Web API =====
* 분석 및 분석 결과등을 API로 제공
* Web API
* 자신의sonarqube.주소/web_api를 통해 현재 시스템이 제공하는 API 목록 및 문서 확인 가능.
<code>
# 현재 이슈 목록
curl http://sonarqube.example.com/api/issues/search?componentKeys=project:module
# 현재 프로젝트의 메트릭(위반 갯수 등)
curl 'http://sonarqube.example.com?componentKeys=project:module&severities=BLOCKER,MAJOR,CRITICAL'
</code>
* Metric Definitions - SonarQube Documentation - Doc SonarQube : metricKeys 에 넣을 값들
* Groovy 로 API 호출
===== Quality Gates =====
* SonarQube Quality Gates
* 분석 결과 품질이 저하되거나 하면 알림을 줄 수 있다.
* 알림은 자기 계정의 My Account → Notifications에서 프로젝트 단위, 혹은 전체 프로젝트를 지정할 수 있다.
==== sonar.qualitygate.wait=true ====
* GitLab CI/CD | SonarQube Docs
* sonarqube 8.x 에서는 client 에서 sonar.qualitygate.wait=true 프라퍼리트를 지정하면 sonarqube quality gate 통과여부를 검사하고 해당 job 을 성공/실패시킬 수 있다.
===== Jenkins Pipeline에서 Quality Gate 응답 대기 =====
* sonarqube 8.x 에서는 불필요. sonarqube gradle/maven plugin 프라퍼티에 sonar.qualitygate.wait=true 사용.
* SonarQube Scanner for Jenkins 플러그인 설치 상태에서
* Jenkins 설정에서 sonarqube URL / 인증등을 설정해주고서
* sonar-quality-gates 플러그인은 사용 안하는게 나은듯.
* Quality Gate 응답을 기다리고 실패이면 UNSTABLE로 변경<code groovy>
timeout(time: 10, unit: 'MINUTES') {
def qg = waitForQualityGate()
if (qg.status != 'OK') {
println “Pipeline unstable due to SoanrQube quality gate failure: ${qg.status}”
currentBuild.result = 'UNSTABLE'
}
}
</code>
* Declarative Pipeline 에서 아래 사용시 무조건 FAILURE로 바꿔버림<code>
stage(“Quality Gate”) {
steps {
timeout(time: 2, unit: 'MINUTES') {
waitForQualityGate abortPipeline: true
}
}
}
</code>
* Sonarqube의 Administration -> Configuration -> Web Hooks 에서 http://JENKINS-SERVER/sonarqube-webhook/ 설정 필요.
===== Installing Sonar =====
* Installing Sonar
* Sonar 웹서버를 다운로드하여 압축을 풀면 설치가 끝난다.
==== 설정 ====
* conf/sonar.properties 파일에서 데이터베이스를 지정해준다. 설정 파일에서 주석만 제거하면 될 정도로 예제가 다 나와있다.<code properties>
sonar.jdbc.url: the URL of the database
sonar.jdbc.driver: the class of the driver
sonar.jdbc.user: the username
sonar.jdbc.password: the password
</code>
* bin/아키텍처/실행파일를 실행한다.
* DB 생성과 권한 설정만 해 두면 나머지는 테이블 생성은 알아서 진행된다.
* 실행시 디버그 정보를 보려면 -Dsonar.verbose=true 시스템 프라퍼티를 지정한다.
==== Annotation Magic number ====
* MagicNumber 항목을 찾아서 ignoreAnnotation, ignoreHashCode 등을 true로 변경한다.
==== Plugin ====
* sonar java ecosystem 2.0 : SonarQube 3.x 대의 가장 최종 Java 플러그인
==== exclude ====
* General Settings → Exclusions → Global Source File Exclusions에서 file:**/generated/**를 통해 generated 이하 파일들 모두 제외
===== Powermock =====
* Powermock + jacoco 사용시에는 코드 커버리지가 작동하지 않는다.
* Cobertura를 사용하면 작동한다.
===== Findbug timeout =====
* sonarqube - Jenkins findbug threshold issue<code>
Can not execute SonarQube analysis: Can not execute Findbugs with a timeout threshold value of 600000 milliseconds
</code>
* 위와 같은 오류 발생시에는 SonarQube Settings → General Settings → Java → FindBugs에 들어가 FindBug에서 timeout을 늘려줘야한다.
===== Analysis Parameter =====
* https://docs.sonarqube.org/latest/analysis/analysis-parameters/
==== duplication exclude ====
* sonar.cpd.exclusions=sonar.cpd.exclusions=/AssemblyInfo.cs,/*.g.cs,**/Mappings/*.cs
===== 참조 =====
* Java 개발자를 위한 Maven + SonarQube + Docker로 시작하는 코드 정적 분석 | Popit
* SonarQube 인지적 복잡도 Cognitive Complexity
* SonarQubeCommunity/sonar-build-breaker: Build Breaker Plugin for SonarQube
* 소스 정적 분석도구 SonarQube 리서칭 - Chan Ahn - Medium