1. 程式人生 > >Mac環境下SonarQube的安裝與使用

Mac環境下SonarQube的安裝與使用

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

準備工作

安裝

1.下載SonarQube之後,開啟bin目錄下的對應OS資料夾,如:

#

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

image.png

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
點選專案名即可進入檢視分析報告的詳細資訊。