SonarQube7.4安裝和使用

聲明

本文轉自:https://www.jianshu.com/p/dd4a4bc59fc3?from=singlemessage

正文

近期比較關注代碼的檢測,以前因爲用的findbugs,所以沒有可視化的界面
因此便搜索了一些代碼檢測管理平臺,因而便搜索到了這一款平臺,接下來就開始搭建吧css

前期準備

  • jdk 1.8.0._131
  • maven 3.5.3
  • mysql 5.7

開始搭建

  1. 軟件下載
    登錄網址 https://www.sonarqube.org/downloads/
    直接下載最新的社區版便可java

     
    1

     

  2. 配置數據源
    打開配置文件:..\sonarqube-7.4\conf\sonar.properties
    增長配置:如下是mysql的配置mysql

#----- DEPRECATED 
#----- MySQL >=5.6 && <8.0
# Support of MySQL is dropped in Data Center Editions and deprecated in all other editions
# Only InnoDB storage engine is supported (not msyISAM).
# Only the bundled driver is supported. It can not be changed.
#sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false
sonar.jdbc.url=jdbc:mysql://127.0.0.1:3306/sonarqube?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false
sonar.jdbc.username=root
sonar.jdbc.password=root
sonar.sorceEncoding=UTF-8
sonar.login=admin
sonar.password=admin
  1. 啓動
    ..\sonarqube-7.4\bin\windows-x86-64\StartSonar.bat

原先配置的數據源是Oracle,啓動的時候提示鏈接oracle的jar包不存在,將鏈接oracle的jar放到路徑
..\sonarqube-7.4\extensions\jdbc-driver\oracle\ojdbc14-10.2.0.1.0.jarweb

若是啓動的時候 提示「另外一程序正在使用此文件」
因爲以前啓動的sonar進程未關閉,有衝突;打開資源管理器(ctrl+shift+esc),殺掉java進程從新啓動,問題解決sql

再次啓動的時候,啓動過會兒窗口會自動關閉,那是由於報錯了,打開log文件..\sonarqube-7.4\logs\sonar.log,發現提示「遠程主機強迫關閉了一個現有的鏈接」,以下所示:windows

 
2


而後打開web.log,發現提示以下:ruby

 
3


這是由於鏈接的oracle的jar的版本太低服務器

 

還有官方要求oracle的版本:oracle

#----- Oracle 11g/12c # The Oracle JDBC driver must be copied into the directory extensions/jdbc-driver/oracle/. # Only the thin client is supported, and only the versions 11.2.x or 12.2.x must be used. See # https://jira.sonarsource.com/browse/SONAR-9758 for more details. # If you need to set the schema, please refer to http://jira.sonarsource.com/browse/SONAR-5000 #sonar.jdbc.url=jdbc:oracle:thin:@localhost:1521/XE 

想一想算了,不想去找jar包了因而便升級mysql版本到mysql5.7,而後mysql的鏈接上面已經提供了
配置好從新啓動,因爲第一次須要建立表,因此可能有點慢maven

  1. 登錄系統
    訪問http://localhost:9000
    初始用戶名 密碼 admin admin

登錄系統後按照以下步驟下載安裝 中文漢化包

 
4

 

安裝好插件重啓sonar,登陸後以下(已經建立了一個項目,首次登陸後界面有些許差別)

 

 
5

接下來建立第一個項目

 

 
6

mvn sonar:sonar \
  -Dsonar.host.url=http://localhost:9000 \
  -Dsonar.login=0006282044b5a5098d23d999c93f9c11eef16025

複製到maven項目的根目錄下啓動檢查便可

檢查後能夠在SonarQube平臺裏看到一些代碼的問題,以下:

 

 
7

 

 
8

 

 
9
  1. Windows下重啓sonar:(這一步不清楚有沒有更好的方法,若有請指出
  • 首先關閉SonarQube.bat窗口
  • 再Ctrl+Shift+Esc調出windows資源管理器
  • 在進程中關閉全部java.exe進程
  • 而後從新進入.\sonarqube-7.4\bin\windows-x86-64\,運行StartSonar.bat文件

IDEA集成SonarLint

SonarLint 是一個插件,能夠集成到開發工具裏,有如下功能

  • 當打開java文件時可自動分析靜態文件,也能夠手動對整個項目作分析;
  • 可鏈接到SonarQube同步分析規則、質量規則與自定義設置;

因爲鵝主只使用IDEA,接下來就說明下IDEA如何集成

  1. 首先配置maven的settings.xml文件,目的是爲了將結果同步到SonarQube平臺上
    配置代碼以下:
<profile> <id>sonar</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <sonar.jdbc.url> jdbc:mysql://127.0.0.1:3306/sonarqube </sonar.jdbc.url> <sonar.jdbc.driver>com.mysql.jdbc.Driver</sonar.jdbc.driver> <sonar.jdbc.username>root</sonar.jdbc.username> <sonar.jdbc.password>root</sonar.jdbc.password> <sonar.host.url>http://127.0.0.1:9000</sonar.host.url> <!-- your_sonar_host是你的服務器地址,若是你的服務在本機則使用localhost --> </properties> </profile> 
  1. 安裝插件sonarLint


     
    10
  2. 配置SonarQube servers


     
    11

若是勾選 Automatically tigger analysis ,將會自動對打開的文件進行分析

  1. 綁定上一步驟建立的,以及選擇項目對應的SonarQube的項目
    這一步是因爲上面步驟已經在SonarQube裏分析了一次因此這裏能夠選擇SonarQube project,不清楚有沒有其餘方法,若是有的話請指出
     
    12
  2. 執行命令 ,便可將項目進行分析,以及將分析的結果同步到SonarQube平臺上,若是隻是用插件進行項目分析的話,測試發現是不會同步到平臺上的,不清楚是否是哪裏步驟有問題...
mvn clean install 
mvn sonar:sonar 

至此整個過程講解結束,若是有疑問或者指點的話歡迎留言(`・ω・´)

相關文章
相關標籤/搜索