針對非maven模式的JAVA專案進行sonar-runner配置

吳峻申發表於2014-02-17

過年後,北京分公司的同事移交了一個專案給我們上海分公司。目前此專案由我負責。因為原來上海分公司這邊的java專案都是maven配置模式,因此用sonar基本上很輕鬆方便。但是這個專案沒有用maven,也沒有用ant,因此想在sonar中檢視此專案就有點困難,我在sonar官網上找了sonar-runner,又結合網上一些資源對此專案進行了配置。現特地將整個配置過程分享給大家。謝謝。

注:沒有使用過sonar的朋友,可參考以下兩個網頁對maven配置的java專案安裝sonar http://my.oschina.net/codingforme/blog/185106

http://my.oschina.net/xiaokaceng/blog/177862

前置條件

1.已安裝JAVA環境

2.已安裝MySQL資料庫

3.已安裝DbVisualizer

4.已安裝sonar

5.已下載sonar-runner

下載地址:

http://repo1.maven.org/maven2/org/codehaus/sonar/runner/sonar-runner-dist/2.3/sonar-runner-dist-2.3.zip

6.作業系統:win8.1

Step 1:

在DbVisualizer裡執行下列語句,建立database和user

CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;
CREATE USER 'sonar' IDENTIFIED BY 'sonar';
GRANT ALL ON sonar.* TO 'sonar'@'%' IDENTIFIED BY 'sonar';
GRANT ALL ON sonar.* TO 'sonar'@'localhost' IDENTIFIED BY 'sonar';
FLUSH PRIVILEGES;

連線建立的sonar資料庫

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

詳細配置如下圖: enter image description here

Step 2:

安裝sonar-runner

將下載包解壓至win 8某目錄,新增SONAR_RUNNER_HOME環境變數,並將SONAR_RUNNER_HOME加入PATH

修改sonar配置檔案(在前置條件裡已經預設安裝了sonar,所以直接去sonar安裝目錄修改)

編輯/conf/sonar.properties檔案,配置資料庫設定,預設已經提供了各類資料庫的支援,這裡使用mysql,因此如下:

# Permissions to create tables, indices and triggers must be granted to JDBC user.
# The schema must be created first.
sonar.jdbc.username=sonar
sonar.jdbc.password=sonar
........
#----- 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
........
#----- Connection pool settings
sonar.jdbc.maxActive=20
sonar.jdbc.maxIdle=5
sonar.jdbc.minIdle=2
sonar.jdbc.maxWait=5000
sonar.jdbc.minEvictableIdleTimeMillis=600000
sonar.jdbc.timeBetweenEvictionRunsMillis=30000

修改sonar-runner的配置檔案

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

#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
sonar.jdbc.driver=com.mysql.jdbc.Driver

#----- 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

Step 3:

配置sonar要分析的目標專案

在要分析的目標專案原始碼根目錄下新建sonar-project.properties檔案

檔案內容如下:

# Required metadata
sonar.projectKey=my:project
sonar.projectName=multiMedia
sonar.projectVersion=1.0

# Paths to source directories.
# Paths are relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
# Do not put the "sonar-project.properties" file in the same directory with the source code.
# (i.e. never set the "sonar.sources" property to ".")
sonar.sources=C:/wjs/workspace/multiMedia/src

# The value of the property must be the key of the language.
sonar.language=java

# Encoding of the source code
sonar.sourceEncoding=UTF-8

# Additional parameters
sonar.my.property=value

說明

sonar.projectKey和sonar.projectName內容無硬性規定,最好不要有中文

根據sonar.sources上方的註釋可知在windows系統中,應該寫"/"而不是"\",且不能和原始碼檔案放在同一目錄下,而且經過測試我發現這裡要寫絕對路徑。multiMedia就是我的目標專案名。

啟動sonar

目錄切換至sonar的/bin/windows-x86-64目錄,雙擊StartSonar.bat檔案,啟動sonar(因為我的作業系統是win8,64位,可根據自身作業系統情況選擇相應目錄)

啟動sonar-runner

在目標專案原始碼根目錄下開啟cmd視窗,執行sonar-runner命令(如下)

C:\wjs\workspace\multiMedia\src>sonar-runner

也可以加引數檢視分析過程(如下)

C:\wjs\workspace\multiMedia\src>sonar-runner -X

C:\wjs\workspace\multiMedia\src>sonar-runner -e

執行成功介面如下:
enter image description here

enter image description here

訪問http:\localhost:9000即可檢視分析結果,如下圖:
enter image description here

相關文章