我的學習系列 - SonarQube的簡單使用

最近對SonarQube進行了一點學習,從網絡上查詢了不少的文章,而後本身又進行了各類實踐,終於成功的實現了代碼掃描。

SonarQube的安裝

SonarQube下載

首先咱們須要去SonarQube的官網進行下載,我這裏下載的是最新的版本8.0,固然本身按照本身的喜愛進行版本的下載便可:
image.pngmysql

安裝嘛根本就沒有了,直接找個位置解壓縮就能夠了。sql

SonarQube運行

找到本身的系統版本運行對應的StartSonar.bat:
image.png
image.png
運行界面以下:
image.png
這時能夠看一下http://localhost:9000這個地址頁面了:
固然我以前運行過了,因此可能和大家顯示的不太同樣,不過不要緊,能顯示信息就能夠了:
image.png
而後咱們能夠登陸,用戶名和密碼就是admin,admin.數據庫

若是你對英語不是很敏感的話,那就能夠用到接下來的漢化教程了:
按照步驟進行操做:
image.png
安裝完畢後點擊重啓便可:
image.png
等一會會讓你再次登陸,而後就變成漢語啦:
image.png網絡

還有就是須要特別注意如下幾個點:oracle

  1. 不知道從那個版本開始SonarQube就不支持mysql了,這個比較坑,不過還有oracle和postgresql能夠用,我這邊用的都是postgresql,畢竟免費的。
  2. SonarQube對jdk也是有要求的,要求jdk11+,我這邊安裝的就是jdk11完美運行,別的沒有試過。

SonarQube重啓

在window沒有辦法經過命令進行SonarQube的中止或者重啓,只能粗暴的中止進程,而後再次運行StartSonar.bat:
image.pngmaven

SonarQube的配置

sonar配置

找到SonarQube裏面config文件夾下面的sonar.properties配置文件,進行數據庫信息的配置:
image.pngide

sonar.jdbc.username=用戶名
sonar.jdbc.password=密碼
sonar.jdbc.url=jdbc:postgresql://localhost:5432/sonarqube

maven配置

由於我這裏是掃描的maven工程,因此須要在maven配置文件中進行相關的設置:post

<profile>
   <id>sonar</id>
   <activation>
     <activeByDefault>true</activeByDefault>
   </activation>
   <properties>
     <sonar.jdbc.url>
            jdbc:postgresql://localhost:5432/sonarqube
     </sonar.jdbc.url>
     <sonar.jdbc.driver>org.postgresql.Driver</sonar.jdbc.driver>
     <sonar.jdbc.username>用戶名</sonar.jdbc.username>
     <sonar.jdbc.password>密碼</sonar.jdbc.password>
     <sonar.host.url>http://localhost:9000</sonar.host.url>
   </properties>
 </profile>
 
  </profiles>

配置完畢以後須要根據以前說的那樣將SonarQube重啓。學習

maven工程掃描

掃描maven工程

上面已經配置好了maven環境變量,而後在須要掃描的項目路徑下執行如下maven命令,便可掃描項目代碼:ui

mvn clean verify sonar:sonar

image.png
會執行不少東西,等到執行完畢以後就能夠去http://127.0.0.1:9000/sonar查看了:
image.png
image.png
好了就是這樣了。。。

IDEA裏面集成sonar

安裝SonarLint插件便可:
image.png
在IDEA的左下角就會出現這個了,能夠查看代碼的問題
image.png
說實話,感受沒有阿里巴巴的代碼掃描好用,我的看法。。。

關於SonarQube的升級

剛纔SonarQube發佈了8.0版本,我以前是7.9.1版本,本着能用就用最新的理念。我就下載並替換了8.0版本,但是啓動的時候出現了:

SonarQube is under maintenance. Please check back later.
Whilst waiting, you might want to check new plugins to extend the current functionality.
If you are an administrator and have no idea why this message is showing, you should read the upgrade guide

這裏表示很懵B呀,因而百度到升級方法就是:
http://127.0.0.1:9000/setup 按照提示更新下SonarQube就能夠啦,固然地址確定要是你本身的地址了。

相關文章
相關標籤/搜索