SonarQube 시작하기

SonarQube 설치, 설정 및 사용법

SonarQube는 코드 품질을 관리하는 오픈 플랫폼으로, 코드 내에 존재할 수 있는 잠재적이거나 명백한 오류를 빠르게 식별할 수 있습니다. 본 문서에서는 이 도구의 설치, 설정 및 사용 방법을 소개합니다.

사전 준비:

  1. JDK (설명 생략)

  2. SonarQube: http://www.sonarqube.org/downloads/

  3. SonarQube Scanner: https://sonarsource.bintray.com/Distribution/sonar-scanner-cli/sonar-scanner-2.5.zip

  4. MySQL 데이터베이스 (설명 생략)

  5. 설치 단계 =====

  6. SonarQube를 다운로드하여 압축을 해제한 후 bin 디렉토리를 엽니다. 해당 OS 디렉토리의 StartSonar를 실행합니다. 본 예제에서는 Windows 64비트 시스템을 사용하므로, D:\sonar\sonarqube-5.3\sonarqube-5.3\bin\windows-x86-64\StartSonar.bat를 실행합니다.

  7. 브라우저를 열고 http://localhost:9000에 접속합니다. 아래와 유사한 화면이 나타나면 설치가 성공한 것입니다.

  8. 설정 단계 =====

  9. MySQL을 열고 새 데이터베이스를 생성합니다.

  10. SonarQube 설치 디렉토리의 D:\sonar\sonarqube-5.3\sonarqube-5.3\conf\sonar.properties 파일을 엽니다.

  11. MySQL 5.X 노드에 다음 정보를 입력합니다:

sonar.jdbc.url=jdbc:mysql://172.16.30.228:3306/qjfsonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance
sonar.jdbc.username=gmsd
sonar.jdbc.password=gmsdtrade
sonar.sourceEncoding=UTF-8
sonar.login=admin
sonar.password=admin

URL은 데이터베이스 연결 주소이며, username은 데이터베이스 사용자 이름, jdbc.password는 데이터베이스 비밀번호, login은 SonarQube 로그인 이름, sonar.password는 SonarQube 비밀번호입니다.

  1. SonarQube 서비스를 다시 시작하고 http://localhost:9000에 다시 접속합니다. 데이터베이스 정보를 초기화하므로 로딩이 다소 느릴 수 있습니다.

  2. 데이터베이스 초기화가 완료되면 로그인합니다.

  3. 아래 순서대로 클릭하여 플러그인 설치 페이지로 이동합니다.

  4. "Chinese Pack"을 검색하여 중국어 언어 팩을 설치합니다.

  5. 설치가 완료되면 SonarQube 서비스를 다시 시작하고 http://localhost:9000/에 다시 접속하면 중국어 인터페이스가 표시됩니다.

  6. 사용 단계 =====

  7. D:\sonar\sonar-scanner-2.5\conf\sonar-runner.properties 파일을 엽니다.

  8. MySQL 노드에 다음 정보를 입력합니다:

sonar.jdbc.url=jdbc:mysql://172.16.30.228:3306/qjfsonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance
sonar.jdbc.username=gmsd
sonar.jdbc.password=gmsdtrade

참고: 테스트 프로젝트가 서버와 동일한 머신에 있지 않은 경우, 서버 IP를 추가해야 합니다:

#----- Default SonarQube server
sonar.host.url=http://XXX.XXX.XXX.XXX:9000
  1. 환경 변수 설정

a. 새 변수를 생성합니다: name=SONAR_RUNNER_HOME, value=D:\sonar\sonar-scanner-2.5

b. Path를 열고 %SONAR_RUNNER_HOME%\bin;을 입력합니다.

c. sonar-runner -version을 실행하여 다음 정보가 나타나면 환경 변수 설정이 성공한 것입니다.

  1. 코드 분석을 수행할 프로젝트 루트 디렉토리를 열고 sonar-project.properties 파일을 새로 생성합니다.

  2. 다음 정보를 입력합니다:

# must be unique in a given SonarQube instance
sonar.projectKey=my:project
# this is the name displayed in the SonarQube UI
sonar.projectName=apiautocore
sonar.projectVersion=1.0
 
# Path is relative to the sonar-project.properties file. Replace "\\" by "/" on Windows.
# Since SonarQube 4.2, this property is optional if sonar.modules is set. 
# If not set, SonarQube starts looking for source code from the directory containing 
# the sonar-project.properties file.
sonar.sources=src
 
# Encoding of the source code. Default is default system encoding
#sonar.sourceEncoding=UTF-8

여기서 projectName은 프로젝트 이름이고, sources는 소스 파일이 있는 디렉토리입니다.

  1. 설정이 완료되면 SonarQube 서비스를 시작하고 cmd를 엽니다.

  2. cmd에서 프로젝트 루트 디렉토리로 이동한 후 다음 명령을 입력합니다: sonar-runner. 분석이 성공적으로 완료되면 아래와 유사한 메시지가 표시됩니다.

  3. http://localhost:9000/를 열면 메인 페이지에 분석된 프로젝트의 개요가 표시됩니다.

  4. 프로젝트를 클릭하고 "문제" 링크를 선택하면 코드 분석 결과로 발견된 버그를 볼 수 있습니다.

  5. 가장 심각한 버그 하나를 선택하여 확인합니다.

해당 위치에서 NullPointerException이 발생할 수 있는 것으로 확인되었습니다. 이는 변수를 초기화하지 않고 사용했기 때문입니다.

더 많은 사용 방법을 알고 싶다면 공식 웹사이트에 접속하여 직접 학습하시기 바랍니다.

http://www.sonarqube.org/ http://docs.sonarqube.org/display/SONAR/Analyzing+with+SonarQube+Scanner

태그: SonarQube code-quality static-analysis java continuous-integration

6월 12일 01:20에 게시됨