全网最全SonarQube使用教程,搭建本地环境以及调试代码全流程,附带git地址

这几天公司要使用SonarQube检测代码规范,然而不满足于SonarQube自带的阿里规范(这不有病吗,阿里的代码规范已经很好用了好吗,干嘛这么折腾),于是让我去编写公司独特的规范,踩了很多坑,主要是SonarQube太矫情,不同版本对于jdk要求不同,对于mysql支持与否不同,对于pgsql版本也有限制,服了。。。

首先选定Sonarqube的版本,我选择了7.0
下载地址
然后选定的数据库是mysql5.6.49版本
在这里插入图片描述
jdk版本的选定是11,不过一般公司没有用8以上的,如果想用8,则需要Sonarqube降级,6.5可以兼容8(我自己没尝试,阿里那边的客服说他们的产品用的6.5,这玩意儿下载的贼慢)。

环境都配置好了之后,修改配置文件
找到对应的安装目录
F:\软件开发工具\sonarqube-7.0\conf\sonar.properties
配置如下代码,配置jdbc

sonar.jdbc.url=jdbc:mysql://127.0.0.1:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false 
sonar.jdbc.driverClassName=com.mysql.cj.jdbc.Driver
sonar.sorceEncoding=UTF-8
sonar.jdbc.username=root
sonar.jdbc.password=root
sonar.login=admin
sonar.password=admin

#开启端口
sonar.web.port=9000

然后修改第二个配置文件
F:\软件开发工具\sonarqube-7.0\conf\wrapper.conf
配置如下代码

wrapper.java.command=E:\java\jdk11.0.7\bin\java

以上就是所有的配置项。
然后就可以启动Sonarqube
找到你操作系统对用的文件夹下的启动程序,双击
F:\软件开发工具\sonarqube-7.0\bin\windows-x86-64\StartSonar.bat
成功启动如下图
在这里插入图片描述
如果没启动,或者闪退,那就是报错了。在如下目录查看log日志
F:\软件开发工具\sonarqube-7.0\logs
原因多半是jdbc那里配置的不对,要么就是jdk版本不行,mysql版本不行。具体的要看报错信息,我之前卡在了mysql的版本上,最后换成了5.6.49版本成功运行了。

本地登录Sonarqube
访问http://localhost:9000
在这里插入图片描述
有汉化包,自行百度装配即可。
时间不够了,如何部署项目到sonarqube上和如何把自定义规则也部署上去有空了再详细说。
我自定义了一个规则,有需要的可以参考源代码

package org.sonar.samples.java.checks;

import org.sonar.check.Priority;
import org.sonar.check.Rule;
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
import org.sonar.plugins.java.api.semantic.Symbol;
import org.sonar.plugins.java.api.semantic.Type;
import org.sonar.plugins.java.api.tree.AnnotationTree;
import org.sonar.plugins.java.api.tree.MethodTree;
import org.sonar.plugins.java.api.tree.Tree;
import org.sonar.plugins.java.api.tree.Tree.Kind;
import java.util.Collections;
import java.util.List;

@Rule(
    key = "MyFirstCustomCheck",
    name = "This rule detects whether the parameter is the usage of \"POJO\"",
    description = "在标有@RequestMapping注解的所有rest服务方法中,出/入参数必须使用pojo类型参数,不允许使用Object、map、jsonobject、String等。",
    priority = Priority.CRITICAL,
    tags = {"bug"})
public class MyFirstCustomCheck extends IssuableSubscriptionVisitor {
    @Override
    public List<Kind> nodesToVisit() {
        return Collections.singletonList(Kind.METHOD);
    }
    @Override
    public void visitNode(Tree tree) {
        MethodTree method = (MethodTree) tree;
        String methodName = method.simpleName().toString();
        System.out.println("方法名==="+methodName);
        List<AnnotationTree> annotations = method.modifiers().annotations();
        for(AnnotationTree annotationTree : annotations){
            // 注解名称
            String annotate = annotationTree.annotationType().toString();
            // 该方法的被RequestMapping注解
            if("RequestMapping".equals(annotate)){
                // 现在,通过检查方法是否具有单个参数来缩小规则的范围,并在这种情况下提出问题。
                if (method.parameters().size() >= 1) {
                    Symbol.MethodSymbol symbol = method.symbol();
                    Type firstParameterType = symbol.parameterTypes().get(0);
                    System.out.println("第一个参数的类型"+firstParameterType);
                    Type returnType = symbol.returnType().type();
                    System.out.println("返回参数类型"+returnType);
                    String[] paramsType = {"Object","Map","JSONObject","String"};
                    for(String param : paramsType){
                        if(param.equals(firstParameterType.toString()) || param.equals(returnType.toString())){
                            System.out.println("方法"+methodName+"不符合“出/入参数必须使用pojo类型参数”规范");
                            if (returnType.is(firstParameterType.fullyQualifiedName())) {
                                // 我们选择在精确的位置报告问题,这将是方法的名称。
                                reportIssue(method.simpleName(), "出/入参数必须使用pojo类型!");
                            }
                            return;
                        }
                    }
                }
            }
        }

    }
}

这个规则是:在标有@RequestMapping注解的所有rest服务方法中,出/入参数必须使用pojo类型参数,不允许使用Object、map、jsonobject、String等。
扫描到了我自己添加的规则
扫描到了我自己添加的规则
激活规则即可
项目地址

  • 1
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值