之前使用了,Sonarqube來做代碼質量管理,現在需要升級SonarQube最新版本,現將使用過程記錄。
1.官網下載
https://www.sonarqube.org/downloads/
2.安裝服務
配置:wrapper.conf 中指定jdk 這裏必須使用jdk11
wrapper.java.command=C:\Program Files\Java\jdk-11.0.7\bin\java
數據庫配置:參考這篇博客,按照上面步驟完成即可,但需要注意url設置指定信息,必須準確,否則啓動失敗。
https://www.cnblogs.com/Simple-Small/p/12882948.html
#----- PostgreSQL 9.3 or greater
# By default the schema named "public" is used. It can be overridden with the parameter "currentSchema".
sonar.jdbc.url=jdbc:postgresql://localhost/sonar?currentSchema=public
啓動sonarqube。
外部訪問不了時,可能是外部訪問端口號被關了,windows系統開放外部訪問端口參看下面博客
https://blog.csdn.net/a12345555555/article/details/72722161
3.配置gradle
爲了便於維護,sonarqube的配置單獨配了一個sonarqube.gradle文件,在build.gradle中引入這個文件
這裏需要注意,由於sonarqube8.X 對jacoco只支持.xml導入,因此我們增加了jacocoTestReport 任務,該任務會在.\build\reports\jacoco內生成.xml格式報表。沒有這一步,你會看到sonarqube上coverage是0%
//sonar.skipSslTrustStore 環境變量 可以忽略這個if
if ("true" == System.getProperty('sonar.skipSslTrustStore')) {
apply from: rootProject.file('sonarqube.gradle')
}
apply plugin: 'org.sonarqube'
apply plugin: 'jacoco'
sonarqube{
properties {
properties["sonar.scm.enabled"] = "false"
properties["sonar.scm.disabled"] = "true"
properties["sonar.projectName"] = "xxxxx:" + project.name
properties["sonar.host.url"] = "http://192.168.101.71:9000"
properties["sonar.login"] = "admin"
properties["sonar.password"] = "admin"
}
}
jacocoTestReport {
reports {
xml.enabled true
csv.enabled false
html.enabled false
}
}
執行命令
gradle clean jacocoTestReport sonarqube -Dsonar.skipSslTrustStore=true
稍等片刻便可以在sonarqube上看到結果了