SonarQube를 구축하여 프로젝트를 수치화합니다

11670 단어 SonarQubeJava
오래된 가게의 코드 정적 분석 도구인SonarQube.
예전에는 Visualization이 좋지 않았고 개인적으로도 그렇게 유용하다고 느끼지 못했지만 최근 버전에서는 크게 개선되었다.
코드의 시각화는 객관적인 코드 평가뿐만 아니라 의외로 Oelai에 대한 설명도 편리하다. 환경이 하나면 해결할 수 있다면 최고다.
그래서 하나를 만들어 보자.

컨디션

  • Ubuntu 14.04.3 LTS
  • ORACLE Java 1.8.0_66
  • Apache Maven 3.0.5
  • Git 1.9.1
  • SonarQube 5.2
  • terminal
    $ cat /etc/lsb-release
    DISTRIB_ID=Ubuntu
    DISTRIB_RELEASE=14.04
    DISTRIB_CODENAME=trusty
    DISTRIB_DESCRIPTION="Ubuntu 14.04.3 LTS"
    
    $ mvn --version
    Apache Maven 3.0.5
    Maven home: /usr/share/maven
    Java version: 1.8.0_66, vendor: Oracle Corporation
    Java home: /usr/lib/jvm/java-8-oracle/jre
    Default locale: en_US, platform encoding: UTF-8
    OS name: "linux", version: "3.16.0-51-generic", arch: "amd64", family: "unix"
    
    $ git --version
    git version 1.9.1
    

    설정


    특별히 준비하지 않고 시작하면 SonarQube는 내장 DB를 사용하여 환경을 구축합니다.
    다만 이 경우 단순히 일어서는 한편으로는 화면에 DB를 박아 사용한다는 뜻으로, 환경 축소 등을 고려할 때 문제가 있는 것 같다는 표현이 나온다.
    따라서 DB부터 잘 준비해야 한다.
    대부분의 RDBMS가 지원되지만, 이번에는 포스트 greSQL에서 설정된다.

    PostgreSQL 설정


    appt-get 설치를 통해 SonarQube를 위한 사용자, 데이터베이스를 만듭니다.
    terminal
    $ sudo apt-get install postgresql
    $ sudo apt-get install postgresql-client
    
    $ sudo su postgres
    [sudo] password for :
    
    postgres@..$ createuser -P -d -l sonarqube
    Enter password for new role:
    Enter it again:
    
    postgres@..$ psql
    psql (9.3.10)
    Type "help" for help.
    
    postgres=# \du
                                 List of roles
     Role name |                   Attributes                   | Member of
    -----------+------------------------------------------------+-----------
     postgres  | Superuser, Create role, Create DB, Replication | {}
     sonarqube | Create DB                                      | {}
    
    postgres=# CREATE DATABASE sonarqube;
    CREATE DATABASE
    
    postgres=# ALTER USER sonarqube SET search_path to sonarqube;
    
    postgres=# \connect sonarqube;
    You are now connected to database "sonarqube" as user "postgres".
    
    sonarqube=# CREATE SCHEMA sonarqube AUTHORIZATION sonarqube;
    CREATE SCHEMA
    
    sonarqube=# \q
    
    postgres@..$ exit
    

    SonarQube 다운로드 및 디버깅


    wget을 통해 가져옵니다.
    기호 링크를 설정하려면 /opt에 배치합니다.
    방금 만든 데이터베이스에 대한 연결 정보 및 웹 서버 설정을 설명합니다.
    terminal
    $ wget https://sonarsource.bintray.com/Distribution/sonarqube/sonarqube-5.2.zip
    $ unzip sonarqube-5.2.zip
    $ sudo mkdir -p /opt/lib
    $ sudo mv sonarqube-5.2 /opt/lib/
    $ sudo ln -s /opt/lib/sonarqube-5.2 /opt/lib/sonarqube
    
    $ sudo vi /opt/lib/sonarqube/conf/sonar.properties
    

    SonarQube 설정, 시작


    다음과 같이 필요한 설정을 매립한다.
    sonar.properties
    #--------------------------------------------------------------------------------------------------
    # DATABASE
    #
    # IMPORTANT: the embedded H2 database is used by default. It is recommended for tests but not for
    # production use. Supported databases are MySQL, Oracle, PostgreSQL and Microsoft SQLServer.
    
    # User credentials.
    # Permissions to create tables, indices and triggers must be granted to JDBC user.
    # The schema must be created first.
    sonar.jdbc.username=sonarqube
    sonar.jdbc.password=SONARQUBE_PASSWORD
    
    #----- PostgreSQL 8.x/9.x
    # If you don't use the schema named "public", please refer to http://jira.sonarsource.com/browse/SONAR-5000
    sonar.jdbc.url=jdbc:postgresql://localhost:5432/sonarqube
    
    #--------------------------------------------------------------------------------------------------
    # WEB SERVER
    
    # Web server is executed in a dedicated Java process. By default heap size is 768Mb.
    # Use the following property to customize JVM options.
    #    Recommendations:
    #
    #    The HotSpot Server VM is recommended. The property -server should be added if server mode
    #    is not enabled by default on your environment: http://docs.oracle.com/javase/7/docs/technotes/guides/vm/server-class.html
    #
    sonar.web.javaOpts=-Xmx768m -Xms256m -XX:MaxPermSize=160m -XX:+HeapDumpOnOutOfMemoryError -Djava.net.preferIPv4Stack=true
    
    # Binding IP address. For servers with more than one IP address, this property specifies which
    # address will be used for listening on the specified ports.
    # By default, ports will be used on all IP addresses associated with the server.
    sonar.web.host=0.0.0.0
    
    # Web context. When set, it must start with forward slash (for example /sonarqube).
    # The default value is root context (empty value).
    sonar.web.context=
    
    # TCP port for incoming HTTP connections. Disabled when value is -1.
    sonar.web.port=9000
    
    여기까지의 설정이 완료되면 SonarQube 서버를 시작합니다.
    또한 설치 후 얼마 지나지 않아 Login = admin, Password = admin을 통해 로그인할 수 있습니다.
    terminal
    $ sudo /opt/lib/sonarqube/bin/linux-x86-64/sonar.sh start
    Starting SonarQube...
    Started SonarQube.
    
    http://localhost:9000/
    순조롭게 시동이 걸렸다.

    코드 분석


    환경이 좋으니 적당한 창고를 분석해 보자.
    이번에는 우리 주니트를 분석해 봅시다.
    terminal
    $ sudo mkdir -p /var/opt/sonarqube/repos
    
    $ sudo git clone https://github.com/junit-team/junit.git
    $ cd junit/
    
    $ sudo mvn clean org.jacoco:jacoco-maven-plugin:prepare-agent install -Dmaven.test.failure.ignore=true -Dmaven.compiler.source=1.5 -Dmaven.compiler.target=1.5
    
    $ sudo mvn verify sonar:sonar -Dsonar.host.url=http://localhost:9000
    
    -Dmaven.compiler.source=1.5-Dmaven.compiler.target=1.5 부분은 분석 프로젝트에 사용된 자바 버전에 따라 1.6, 1.7, 1.8 등으로 변경하십시오.-Dsonar.host.url=http://localhost:9000 섹션을 변경하면 원격으로라도 해석 결과를 SonarQube에 수집할 수 있습니다.
    CI 프로세스 종료 같은 곳에 할당되면 매번 결과가 업데이트되고 순조롭게 진행됩니다.
    결과는 이렇다.
    듣기에 괜찮다.

    지표의 독법


    처음 보면 다 알파벳이라 당황스럽네요.
    자주 사용하는 간단한 소개를 고르다.
    아래와 같다.
    표식
    일본어·의의
    용도
    Lines Of Code
    코드 행수
    프로젝트의 규모감을 파악하다.
    Duplications
    되풀이하다
    복사하여 붙여넣기를 테스트합니다.
    Complexity
    복잡도(순환 복잡도)
    분기된 다등 코드의 복잡성을 파악하다.
    Technical Debt Ratio
    기술 부채 비율
    기술 부채의 상대량을 파악하다.
    Debt
    부채(기술 부채)
    며칠 장악하면 기술 부채를 상환할 수 있다.단위는 d(일)로 표시되며 기본값은 d=8h로 계산됩니다.
    그나저나 메뉴에서 [Administration] [System] [Update Center] [Available]를 추진하면 일본어화된 플러그인'Japanese Pack'을 발견할 수 있고, 이를 적용해도 어느 정도 일본어화된다.
    [Install] 버튼을 설치한 후 다음과 같이 SonarQube를 다시 시작하면 일본어가 적용됩니다.
    terminal
    $ sudo /opt/lib/sonarqube/bin/linux-x86-64/sonar.sh stop
    Stopping SonarQube...
    Stopped SonarQube.
    
    $ sudo /opt/lib/sonarqube/bin/linux-x86-64/sonar.sh start
    Starting SonarQube...
    Started SonarQube.
    
    대체로 이렇게 사용할 수 있다.
    사업장에서 사업 수치화를 요구받으면 우선 도입하는 것이 좋다.

    참조 링크


    http://docs.sonarqube.org/display/SONAR/Requirements
    http://docs.sonarqube.org/display/SONAR/Setup+and+Upgrade
    http://docs.sonarqube.org/display/SONAR/Installing+the+Server
    http://www.sonarqube.org/unit-test-execution-in-sonarqube/
    http://stackoverflow.com/questions/33523826/maven-build-for-sonar-failed-while-installing-jdbc-driver
    http://stackoverflow.com/questions/13293062/maven-compile-error
    http://qiita.com/mkamotsu/items/98c6d721a87a74f2b36f

    좋은 웹페이지 즐겨찾기