Mac環境下SonarQube的安裝與使用

SonarQube是一個用於代碼質量管理的開源平臺,可以快速的定位代碼中潛在的或者明顯的錯誤,支持包括java,C#,C/C++,PL/SQL,Cobol,JavaScrip,Groovy等等二十幾種編程語言.

準備工作

1.SonarQube
2.Sonar-Scanner

3.mysql數據庫(忽略安裝過程)

安裝

1.下載SonarQube之後,打開bin目錄下的對應OS文件夾,如:

#

     cd /Users/cw/Downloads/sonarqube-7.3/bin/macosx-universal-64
     sonar.sh start

image.png

2.打開瀏覽器,訪問SonarQube:http://localhost:9000,如出現下圖則代表SonarQube啓動成功

image.png

配置

  1. 打開mysql並新建一個數據庫,數據庫名稱可任意指定。
  2. 打開SonarQube安裝目錄下的sonar.properties文件,目錄爲:

#

    /path to your SoaneQube location directory/conf/sonar.properties

3. 在sonar.properties文件中mysql下oracle前添加如下信息

#

    sonar.jdbc.url=jdbc:mysql:/localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance
    sonar.jdbc.username=root
    sonar.jdbc.password=password
    sonar.sorceEncoding=UTF-8
    sonar.login=admin
    sonar.password=admin

sonar.jdbc.url是你的mysql數據庫連接url,sonar.jdbc.username是mysql數據庫用戶名,sonar.jdbc.password是數據庫用戶密碼,sonar.login是SonarQube的登錄用戶名,sonar.password是SonarQube的登錄密碼。
4.重啓SonarQube服務

#

    sonar.sh restart

image.png
5.打開瀏覽器,再次訪問SonarQube:http://localhost:9000,會稍微有點慢,因爲需要初始化SonarQube數據庫
6.初始化成功後登錄
image.png
點擊位置Log in進入登錄頁
image.png
輸入admin/admin登錄
7.安裝中文語言包:
image.png
如圖,先點擊位置1,再點擊位置2,在位置3中輸入chinese,即可看見Chinese Pack,最後點擊位置4以安裝中文語言包。等待安裝完成。出現如下圖界面即安裝完成:
image.png
點擊restart重啓SonarQube服務器。
image.png
重啓完成後即可看見中文界面。

使用

  1. 下載Sonar-Scanner
    2.下載完成後打開Sonar-Scanner目錄下sonar-runner.properties文件,目錄如:
    ####
    /Users/cw/Documents/devtools/sonar/sonar-scanner-2.8/conf/sonar-scanner.properties
    3.在mysql節點下添加如下信息

#

    sonar.jdbc.url=jdbc:mysql:/localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance
    sonar.jdbc.username=root
    sonar.jdbc.password=password
注意:如果待測試項目與SonarQube服務不在同一臺機器,則需要添加SonarQube服務的IP:
    #----- Default SonarQube server
    sonar.host.url=http://XXX.XXX.XXX.XXX:9000

4. 配置環境變量
打開終端進行如下操作:

#

    vim ~/.bash_profile
    在文件末尾添加如下字段:
    export SONAR_RUNNER_HOME=path to your Sonar-Scanner directory
    export PATH=$PATH:$SONAR_RUNNER_HOME/bin
    保存修改
    使配置立即生效:source ~/.bash_profile

5. 測試Sonar-Scanner
打開終端輸入如下命令

   sonar-runner -version

出現如下圖則代表配置成功
image.png
6.打開待測試的項目根目錄,新建sonar-project.properties文件並輸入如下信息
1). SonarQube版本7以下:

#

    # must be unique in a given SonarQube instance
    sonar.projectKey=my:project
    # this is the name displayed in the SonarQube UI
    sonar.projectName=i2work-operation
    sonar.projectVersion=1.0
     # Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
    # Since SonarQube 4.2, this property is optional if sonar.modules is set. 
    # If not set, SonarQube starts looking for source code from the directory containing 
    # the sonar-project.properties file.
    sonar.sources=src
    # Encoding of the source code. Default is default system encoding
    #sonar.sourceEncoding=UTF-8

2). SonarQube版本7以上:

#

    # must be unique in a given SonarQube instance
    sonar.projectKey=my:project
    # this is the name displayed in the SonarQube UI
    sonar.projectName=i2work-operation
    sonar.projectVersion=1.0
    # Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
    # Since SonarQube 4.2, this property is optional if sonar.modules is set. 
    # If not set, SonarQube starts looking for source code from the directory containing 
    # the sonar-project.properties file.
    sonar.sources=src
    # Encoding of the source code. Default is default system encoding
    #sonar.sourceEncoding=UTF-8
    #path to your project build output path
    sonar.java.binaries=target/classes

其中:sonar.projectName是項目名字,sonar.sources=是源文件所在的目錄,SonarQube新版相對於之前的版本新增要求必須指定項目class文件的目錄,如果不配置會報錯。
7. 打開終端,進入待測試項目根目錄,執行如下命令:

    sonar-runner

執行完成後會出現下圖內容
image.png
執行過程中可能會出現如下圖所示類型的錯誤:
image.png
這是因爲SonarScanner在掃描過程中發現了.svn文件,於是啓動了自己的SVN插件,去訪問SVN,但是又沒有對應SVN路徑的授權所以就報錯。
解決方式:
1. 打開sonarqube的控制檯,使用admin登錄後 按下圖所示順序進行操作
image.png
在配置->SCM菜單中將Disable the SCM Sensor設置爲TRUE,並在下面的SVN配置選項中配置自己的SVN路徑及訪問賬號密碼等信息。
8. 打開SonarQube:http://localhost:9000
按圖示順序依次點擊,即可看見正在分析中的後臺任務
image.png
分析一般會持續10分鐘左右,待分析完成後回到SonarQube主頁即項目頁,即可看見項目的分析結果,如圖:
image.png
點擊項目名即可進入查看分析報告的詳細信息。

預知更多使用,請登錄官網自行學習。
1. http://www.sonarqube.org/
2. http://docs.sonarqube.org/display/SONAR/Analyzing+with+SonarQube+Scanner

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章