Jenkins + Maven + SonarQube 构建代码质量检测平台

转自: https://segmentfault.com/a/1190000011118541

使用SonarQube扫描仪分析Maven

安装jenkins

安装Maven

安装SonarQube

配置Jenkins + Maven + SonarQube

  • SonarQube 登录

access-sonar

用户密码为安装sonar设置的用户名和密码

  • 登录到sonar平台,设置 administration -security -user -administrator (右键,重新获取一个tokens,名字自定义)

get-tokens

右键复制 获取的tokens,然后去jenkins里面配置 sonar

  • jenkins登录 -> configure system -> SonarQube servers

config-sonar

注:笔者安装的服务统一使用域名的方式访问。如果有需要,记得本地设置hosts

  • 配置maven

编辑位于$ MAVEN_HOME / conf或〜/ .m2中的settings.xml文件,设置插件前缀和可选的SonarQube服务器URL

<settings>
    <pluginGroups>
        <pluginGroup>org.sonarsource.scanner.maven</pluginGroup>
    </pluginGroups>
    <profiles>
        <profile>
            <id>sonar</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <!-- Optional URL to server. Default value is http://localhost:9000 -->
                <sonar.host.url>
                  http://sonar.aniu.so  # 填写自己的sonar服务器地址
                </sonar.host.url>
            </properties>
        </profile>
     </profiles>
</settings>
  • 分析一个Maven项目

移动到一个maven项目目录内,执行下面命令

mvn clean verify sonar:sonar 
# 此段命令也可以在jenkins配置,如下:

这里写图片描述

mvn 分析完成之后,登录sonar平台查看分析结果

这里写图片描述

从图中可以很明显的看出此项目存在347个BUG,然后给开发创建sonar账号,让他们帮忙修复。。。

相关报错解决

  • Sonargraph Integration: Skipping project aniu-api-product [tv.aniu:aniu-api-product], since no Sonargraph rules are activated in current SonarQube quality profile [SonarQube]

此报错暂时不影响maven 集成到 sonar上

  • 413 Request Entity Too Large

原因是nginx默认上传文件的大小是1M,可nginx的设置中修改

# 解决方法如下:

1.打开nginx配置文件 nginx.conf, 路径一般是:/etc/nginx/nginx.conf。
2.在http{}段中加入 client_max_body_size 20m; 20m为允许最大上传的大小(大小可自定义)。
3.保存后重启nginx,问题解决。
  • sonar Failed to upload report - 500: An error has occurred

Caused by: com.mysql.jdbc.PacketTooBigException: Packet for query is too large (22790518 > 16777216). You can change this value on the server by setting the max_allowed_packet' variable.

show variables like '%max_allowed_packet%';
# 更改mysql 的max_allowed_packet参数,设置 max_allowed_packet = 64M ,然后重启mysql
[mysqld]
max_allowed_packet=32M
https://dev.mysql.com/doc/refman/5.7/en/packet-too-large.html

注:这里报错可以通过查看sonar的web.log得出原因。


猜你喜欢

转载自blog.csdn.net/qq_36688928/article/details/80687282