Analyzing with SonarQube Scanner for Ant

By SonarSource – GNU LGPL 3 – Issue Tracker  Sources 

SonarQube Scanner for Ant 2.5 – Compatible with SonarQube 5.6+ (LTS) 
Download

Table of Contents

기능

SonarQube Scanner for Ant를 사용해 Apache Ant 빌드 스크립트에서 SonarQube 분석을 수행할 수 있습니다.

호환성

SoanrQube Scanner for Ant v2.5는 SonarQube 5.6+와 호환됩니다.

사전 준비

  • SonarQube가 설치되어 있어야 합니다
  • SonarQube 서버가 지원하는 최소 버전의 Java가 하나 이상 설치되어 있어야 합니다
  • 분석 대상 언어의 언어 플러그인이 설치되어 있어야 합니다
  • 소스 코드 분석하기를 숙지합니다
  • Ant v1.7.1+

설치

분석을 수행할 머신에 SonaQube Scanner for Ant를 다운로드 합니다.

사용

SonarQube Scanner for Ant는 Ant 태스크로 SonarQube Scanner의 wrapper입니다. 이 warpper는 SonarQube Scanner를 호출하고, sonar.* 의 파라미터를 SonarQube Scanner로 전달합니다. Any스러운 방법으로 동작하지는 않지만, 새로운 플러그인 혹은 SonarQube 자체가 제공하는 파라미터들을 즉시 활용할 수 있는 장점이 있습니다. 즉, SonarQube Scanner for Ant를 성공적으로 활용하려면 property 이름을 준수해야 합니다.

분석 파라미터 페이지를 참조해 Ant 기반의 분석 파라미터들을 추가로 확인할 수 있습니다.

Simple Project

Ant 빌드 스크립트에 sonar Ant 타겟을 정의합니다:

build.xml
<project name="My Project" default="all" basedir="." xmlns:sonar="antlib:org.sonar.ant">
...
  
<!-- Define the SonarQube global properties (the most usual way is to pass these properties via the command line) -->
<property name="sonar.host.url" value="http://localhost:9000" />
 
...
  
<!-- Define the SonarQube project properties -->
<property name="sonar.projectKey" value="org.sonarqube:sonarqube-scanner-ant" />
<property name="sonar.projectName" value="Example of SonarQube Scanner for Ant Usage" />
<property name="sonar.projectVersion" value="1.0" />
<property name="sonar.sources" value="src" />
<property name="sonar.java.binaries" value="build" />
<property name="sonar.java.libraries" value="lib/*.jar" />
...
 
<!-- Define SonarQube Scanner for Ant Target -->
<target name="sonar">
    <taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml">
        <!-- Update the following line, or put the "sonarqube-ant-task-*.jar" file in your "$HOME/.ant/lib" folder -->
        <classpath path="path/to/sonar/ant/task/lib/sonarqube-ant-task-*.jar" />
    </taskdef>
 
    <!-- Execute SonarQube Scanner for Ant Analysis -->
    <sonar:sonar />
</target>
...

프로젝트 베이스 디렉토리에서 다음 명령어를 실행하면 분석이 수행됩니다:

ant sonar

보안

Execute Analysis 권한을 가지고 있는 사용자는 누구나 분석을 수행할 수 있습니다.

Anyone 그룹에 Execute Analysis 권한이 부여되어 있지 않거나, SonarQube 인스턴스에 보안 설정이 된 경우에는(sonar.forceAuthentication=true) Execute Analysis 권한을 부여받은 사용자의 토큰값을 sonar.login 속성값으로 설정해야 합니다.

예: sonar-scanner -Dsonar.login=[my analysis token]

샘플 프로젝트

다음 링크에서 간단한 프로젝트 샘플을 다운로드 할 수 있습니다: https://github.com/SonarSource/sonar-scanning-examples/tree/master/sonarqube-scanner-ant

멀티-모듈 프로젝트

상위 및 하위 모듈의 환경 설정은 상위(parent) build.xml 파일에 설정해야 합니다:

Parent build.xml
... 
<!-- Set modules IDs -->
<property name="sonar.modules" value="module-one,module-two"/>
   
<!-- For modules, properties are inherited from the parent. They can be overridden as shown below: -->
<property name="module-one.sonar.projectName" value="Module One" />
<property name="module-one.sonar.sources" value="sources/java" />
<property name="module-one.sonar.binaries" value="target" />
<!-- Default module base directory is <curent_directory>/<module_ID>. It can be overridden if necessary -->
<property name="module-one.sonar.projectBaseDir" value="Module 1" /> 
...

디버그 로그 활성화

일반적인 Ant verbose 옵션을 사용해 디버그 로그를 활성화 할 수 있습니다: -v

ant sonar -v

Advanced Usage

build.xml 파일 혹은 command-line 파라미터를 사용해 분석 파라미터를 추가 설정할 수 있습니다.

© 2017-2018 Moses Kim.

별도의 언급이 없는 한, 이 스페이스의 컨텐츠는 크리에이티브 커먼즈 저작자표시-비영리-동일조건변경허락 4.0 국제 라이선스에 따라 이용할 수 있습니다.
SONARQUBE는 SonarSource SA의 트레이드 마크입니다. 모든 트레이트 마크 및 저작권은 각 소유자의 소유물입니다.

::: SonarQube 관련 문의 : 이메일 :::