Maven项目: 集成SonarQube进行代码审查

Posted 琦彦

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Maven项目: 集成SonarQube进行代码审查相关的知识,希望对你有一定的参考价值。

目录

使用 SonarQube 分析 Maven 项目

Maven 的 setting.xml文件

项目或模块的Pom文件

执行代码分析命令

Sonarqube分析结果


使用 SonarQube 分析 Maven 项目

Maven 的 setting.xml文件

我们需要配置 Maven 的 setting.xml文件,增加 sonarQube 配置。

<settings>
    <pluginGroups>
        <pluginGroup>org.sonarsource.scanner.maven</pluginGroup>
    </pluginGroups>
    <profiles>
        <profile>
            <id>sonar</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <!-- 配置 Sonar Host地址,默认:http://localhost:9000 -->
                <sonar.host.url>
                  http://192.168.172.128:9090
                </sonar.host.url>
            </properties>
        </profile>
     </profiles>
</settings>

项目或模块的Pom文件

<build>
    <pluginManagement>
        <!--使用 SonarQube 分析 Maven 项目-->
        <plugin>
            <groupId>org.sonarsource.scanner.maven</groupId>
            <artifactId>sonar-maven-plugin</artifactId>
            <version>3.9.0.2155</version>
        </plugin>
        </plugins>
    </pluginManagement>
</build>

执行代码分析命令

mvn clean verify sonar:sonar -DskipTest=true

分析输出

[INFO] ------------- Run sensors on project
[INFO] Sensor Zero Coverage Sensor
[INFO] Sensor Zero Coverage Sensor (done) | time=205ms
[INFO] Sensor Java CPD Block Indexer
[INFO] Sensor Java CPD Block Indexer (done) | time=373ms
[INFO] SCM Publisher is disabled
[INFO] CPD Executor 183 files had no CPD blocks
[INFO] CPD Executor Calculating CPD for 510 files
[INFO] CPD Executor CPD calculation finished (done) | time=456ms
[INFO] Analysis report generated in 371ms, dir size=7 MB
[INFO] Analysis report compressed in 1480ms, zip size=2 MB
[INFO] Analysis report uploaded in 160ms
[INFO] ANALYSIS SUCCESSFUL, you can browse http://192.168.172.128:9090/dashboard?id=com.bigunion%3Abigunion
[INFO] Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report
[INFO] More about the report processing at http://192.168.172.128:9090/api/ce/task?id=AXmnaSyaTReHGmpr1jBO
[INFO] Analysis total time: 1:15.595 s
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary for bigunion 2.3.0:
[INFO]
[INFO] bigunion ........................................... SUCCESS [01:17 min]
[INFO] bigunion-common .................................... SUCCESS [  0.020 s]
[INFO] bigunion-common-core ............................... SUCCESS [  3.103 s]
[INFO] bigunion-api ....................................... SUCCESS [  0.168 s]
[INFO] bigunion-api-system ................................ SUCCESS [  0.441 s]
[INFO] bigunion-common-redis .............................. SUCCESS [  0.362 s]
[INFO] bigunion-common-security ........................... SUCCESS [  0.423 s]
[INFO] bigunion-auth ...................................... SUCCESS [  3.714 s]
[INFO] bigunion-gateway ................................... SUCCESS [  1.622 s]
[INFO] bigunion-common-datascope .......................... SUCCESS [  0.351 s]
[INFO] bigunion-common-log ................................ SUCCESS [  0.340 s]
[INFO] bigunion-common-swagger ............................ SUCCESS [  0.253 s]
[INFO] bigunion-modules ................................... SUCCESS [  0.072 s]
[INFO] bigunion-system .................................... SUCCESS [  3.121 s]
[INFO] bigunion-file ...................................... SUCCESS [  2.189 s]
[INFO] bigunion-exam ...................................... SUCCESS [  4.003 s]
[INFO] bigunion-devicemag ................................. SUCCESS [  5.598 s]
[INFO] bigunion-student ................................... SUCCESS [  2.941 s]
[INFO] bigunion-project ................................... SUCCESS [  3.937 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  01:51 min
[INFO] Finished at: 2021-05-26T14:42:37+08:00
[INFO] ------------------------------------------------------------------------

Sonarqube分析结果

登录 http://192.168.172.128:9090/查看

以上是关于Maven项目: 集成SonarQube进行代码审查的主要内容,如果未能解决你的问题,请参考以下文章

gitlab+jenkins+maven+docker持续集成——sonarqube及sonarscanner代码审查

持续集成Jenkins+sonarqube部署教程

持续集成Jenkins+sonarqube部署教程

利用 SonarScanner 静态扫描 Rainbond 上的 Maven 项目

SonarQube 5.1.2和Maven 3.3集成

SonarQube静态代码分析maven集成方式