基礎環境
SonarQube 8.2
maven3.3.6
jdk:1.8
配置項目
創建項目
登錄後右上角+號創建新項目
填寫項目名稱及標識
創建token
下一步
選擇項目類型
以上就已提供maven插件執行命令(注意’'是換行的意思,執行前需要刪掉並去掉換行)
配置setting.xml
注意:此項非必須
sonar插件在不配置的情況也是可以用的,如果每次不想帶url、token等參數,而想簡單的執行[mvn sonar:sonar]則需要在setting.xml將sonar信息配置進去
<settings>
<!-- pluginGroups也可以不配置 -->
<pluginGroups>
<pluginGroup>org.sonarsource.scanner.maven</pluginGroup>
</pluginGroups>
<profiles>
<profile>
<id>sonar</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<!-- 平臺登錄的賬號的用戶名
<sonar.login>admin</sonar.login> -->
<!-- SonarQube平臺登錄的賬號的密碼
<sonar.password>admin</sonar.password>-->
<!-- 全局配置的情況建議使用用戶名密碼的形式 -->
<sonar.projectKey>
e71f9c0248f4e299abea623f1fa81ff99eb62477
</sonar.projectKey>
<!-- Optional URL to server. Default value is http://localhost:9000 -->
<sonar.host.url>
http://myserver:9000
</sonar.host.url>
</properties>
</profile>
</profiles>
<!-- 官方未配置activeProfiles 但是個人建議配置上 -->
<activeProfiles>
<!-- 這步配置,sonar的profile配置才能生效 -->
<activeProfile>sonar</activeProfile>
</activeProfiles>
</settings>
配置完成後執行如下命令即可掃描
mvn sonar:sonar
執行掃描任務
以下命令無需配置setting.xml
mvn sonar:sonar -Dsonar.projectKey=n-demo -Dsonar.host.url=http://192.168.1.17:9000 -Dsonar.login=e71f9c0248f4e299abea623f1fa81ff99eb62477
查看結果
登錄sonar服務,即可看到項目分析結果