天天看點

sonarqube+maven+jenkins

http://www.cnblogs.com/az19870227/archive/2013/04/03/2998351.html

http://blog.csdn.net/hunterno4/article/details/11687269

SonarQube安裝

預置條件

1.已安裝JAVA環境

2.已安裝有MySQL資料庫

軟體下載下傳位址:http://www.sonarqube.org/downloads/

下載下傳SonarQube與SonarQube Runner

中文更新檔包下載下傳:http://docs.codehaus.org/display/SONAR/Chinese+Pack

1.資料庫配置

進入資料庫指令

#mysql -u root -p

mysql> CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;

mysql> CREATE USER 'sonar' IDENTIFIED BY 'sonar';

mysql> GRANT ALL ON sonar.* TO 'sonar'@'%' IDENTIFIED BY 'sonar';

mysql> GRANT ALL ON sonar.* TO 'sonar'@'localhost' IDENTIFIED BY 'sonar';

mysql> FLUSH PRIVILEGES;

2.安裝sonar與sonar-runner

将下載下傳的sonar-3.7.zip包解壓至Linux某路徑如/usr/local

将下載下傳的sonar-runner-dist-2.3.zip包解壓某路徑/usr/local

添加SONAR_HOME、SONAR_RUNNER_HOME環境變量,并将SONAR_RUNNER_HOME加入PATH

修改sonar配置檔案

編輯<install_directory>/conf/sonar.properties檔案,配置資料庫設定,預設已經提供了各類資料庫的支援

這裡使用mysql,是以取消mysql子產品的注釋

#vi sonar.properties

[java] view plaincopy

sonar.jdbc.username: sonar

sonar.jdbc.password: sonar

sonar.jdbc.url: jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true

# Optional properties

sonar.jdbc.driverClassName: com.mysql.jdbc.Driver

修改sonar-runner的配置檔案

切換至sonar-runner的安裝目錄下,修改sonar-runner.properties

根據實際使用資料庫情況取消相應注釋

[java] view plaincopy

#Configure here general information about the environment, such as SonarQube DB details for example

#No information about specific project should appear here

#----- Default SonarQube server

sonar.host.url=http://localhost:9000

#----- PostgreSQL

#sonar.jdbc.url=jdbc:postgresql://localhost/sonar

#----- MySQL

sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8

#----- Oracle

#sonar.jdbc.url=jdbc:oracle:thin:@localhost/XE

#----- Microsoft SQLServer

#sonar.jdbc.url=jdbc:jtds:sqlserver://localhost/sonar;SelectMethod=Cursor

#----- Global database settings

sonar.jdbc.username=sonar

sonar.jdbc.password=sonar

#----- Default source code encoding

sonar.sourceEncoding=UTF-8

#----- Security (when 'sonar.forceAuthentication' is set to 'true')

sonar.login=admin

sonar.password=admin

3.添加資料庫驅動

除了Oracle資料庫外,其它資料庫驅動都預設已經提供了,且這些已添加的驅動是sonar唯一支援的,是以不需要修改

如果是Oracle資料庫,需要複制JDBC驅動至<install_directory>/extensions/jdbc-driver/oracle目錄

4.啟動服務

目錄切換至sonar的<install_directory>/bin/linux-x86-64/目錄,啟動服務

#./sonar.sh start 啟動服務

#./sonar.sh stop 停止服務

#./sonar.sh restart 重新開機服務

至此,sonar就安裝好了

通路http:\\localhost:9000即可

5.sonar中文更新檔包安裝

中文包安裝

安裝中文更新檔包可以通過通路http:\\localhost:9000,打開sonar後,進入更新中心安裝

或者下載下傳中文更新檔包後,放到SONARQUBE_HOME/extensions/plugins目錄,然後重新開機SonarQube服務

sonar作為Linux服務并開機自啟動

建立檔案/etc/init.d/sonar,輸入如下内容:

[java] view plaincopy

#!/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 $*

SonarQube開機自啟動(Ubuntu, 32位):

sudo ln -s $SONAR_HOME/bin/linux-x86-32/sonar.sh /usr/bin/sonar

sudo chmod 755 /etc/init.d/sonar

sudo update-rc.d sonar defaults

SonarQube開機自啟動(RedHat, CentOS, 64位):

sudo ln -s $SONAR_HOME/bin/linux-x86-64/sonar.sh /usr/bin/sonar

sudo chmod 755 /etc/init.d/sonar

sudo chkconfig --add sonar

使用SonarQube Runner分析源碼

預置條件

已安裝SonarQube Runner且環境變量已配置,即sonar-runner指令可在任意目錄下執行

1.在項目源碼的根目錄下建立sonar-project.properties配置檔案

以android項目為例:

[java] view plaincopy

sonar.projectKey=android-sonarqube-runner

sonar.projectName=Simple Android project analyzed with the SonarQube Runner

sonar.projectVersion=1.0

sonar.sources=src

sonar.binaries=bin/classes

sonar.language=java

sonar.sourceEncoding=UTF-8

sonar.profile=Android Lint

注:要使用Android Lint

規則分析需要先通路http:\\localhost:9000更新中心添加Android Lint插件,使其可以分析Android Lint規則

2.執行分析

切換到項目源碼根目錄,執行指令

# sonar-runner

分析成功後通路http:\\localhost:9000即可檢視分析結果

不同參數的意思:

http://docs.codehaus.org/display/SONAR/Analysis+Parameters

不同項目的源碼分析示例下載下傳:

https://github.com/SonarSource/sonar-examples/zipball/master

與IDE關聯

最後,當然了,得與IDE相關聯,才能更友善地實時檢視

以Eclipse為例,請見:http://docs.sonarqube.org/display/SONAR/SonarQube+in+Eclipse

附:

sonarQube官網位址:http://www.sonarqube.org/

sonarQube官方文檔位址:http://docs.codehaus.org/display/SONAR/Documentation

sonarQube示例位址:http://nemo.sonarqube.org/

網上另兩篇相關的文章:http://www.cnblogs.com/gao241/p/3190701.html

http://www.myexception.cn/open-source/1307345.html

在jenkins中安裝sonar jenkins plugin

1.在jenkins的插件管理中選擇安裝,sonar jenkins plugin. 然後到jenkins的系統配置頁面,

2.在sonar 配置部分點選add sonar

3.配置sonar的安裝資訊

4.在project中添加sonar觸發

普通的project。進入project,點選configue,在build部分中點選 'Add build step' 選擇 'Invoke Standalone Sonar Analysis':

配置sonar

2對于maven project

在post-build action 部分點選 Add post-build action

配置

選擇sonar installtion

選擇advanced