昔いれた、SonarQubeの設定手順。
大体は公式ドキュメントを見ながらやったはずだが、うろおぼえなので、若干まちがってるかも・・・
javadocコメントのカバレッジを見たくなったので、jenkinsともういちど連携させることにした。
jenkinsとの連携の手順を今回は省く。
インストール
/usr/localに、sonar-3.7.4.zipとsonar-runner-dist-2.4.zipを展開
sonar-server
Sonar用のDB(sonar)をあらかじめつくっておき、接続情報は/usr/local/sonar-3.7.4/conf/sonar.propertiesに記述。今回はMySQLを使用。PostgreやOracleも行ける模様。
他にも、webアプリのポート、パスなど、基本的なことはここで決めている。
以下、主要なところを抜粋
# Listen host/port and context path (for example / or /sonar). Default values are 0.0.0.0:9000/.
sonar.web.host: 0.0.0.0
sonar.web.port: 9200
sonar.web.context: /sonar
# The schema must be created first.
sonar.jdbc.username: sonar
sonar.jdbc.password: sonar_pass
# ----- MySQL 5.x
# Comment the embedded database and uncomment the following line to use MySQL
sonar.jdbc.url: jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true
bin以下に、各OSごとに起動スクリプトがあるので、該当するのを/usr/binにコピー
cp /usr/local/sonar-3.7.4/bin/linux-x86-64/sonar.sh /usr/bin/sonar
そして、次のものを/etc/init.d/sonar に作成してサービスとして登録
# !/bin/sh
#
# rc file for SonarQube
#
# chkconfig: 345 96 10
# description: SonarQube system (www.sonarsource.org)
#
### BEGIN INIT INFO
# Provides: sonar
# Required-Start: $network
# Required-Stop: $network
# Default-Start: 3 4 5
# Default-Stop: 0 1 2 6
# Short-Description: SonarQube system (www.sonarsource.org)
# Description: SonarQube system (www.sonarsource.org)
### END INIT INFO
/usr/bin/sonar $*
これで、service sonar start
でwebアプリが起動するはず。
sonar.propertiesに設定したアドレスにアクセスして確認。
http://localhost:9200/sonar
sonar-runner
runnerの方は、サーバとDBの接続情報を設定する。/usr/local/sonar-runner-2.4/conf/sonar-runner.propertiesがその設定ファイル。
# ----- Default SonarQube server
sonar.host.url=http://localhost:9200/sonar
# ----- MySQL
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8
# ----- Global database settings
sonar.jdbc.username=sonar
sonar.jdbc.password=sonar_pass
解析したいソースのルートディレクトリに、sonar-project.propertiesを作成し、ソースやバイナリのディレクトリなどを指定する。
# Required metadata
sonar.projectKey=HogeKey
sonar.projectName=Hoge
sonar.projectVersion=0.1
# Path to the parent source code directory.
# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
# Since SonarQube 4.2, this property is optional. If not set, SonarQube starts looking for source code
# from the directory containing the sonar-project.properties file.
sonar.sources=app
sonar.binaries=target
sonar.tests=test
# Encoding of the source code
sonar.sourceEncoding=UTF-8
上記がある場所でsonar-runnerを実行すれば、解析がはじまる。パスを通していない状態なら下記で実行。
/usr/local/sonar-runner-2.4/bin/sonar-runner
解析でわかること
標準でも
- LOC
- クラス数、メソッド数
- javadocコメントをつけたAPIの比率
- 重複率
- 複雑度
などがわかる。sonar-project.propertiesでバージョンを管理していれば、以前のバージョンとの比較ができる。