SonarLint和marven对SonarQube的应用

myeclipse对SonarLint的使用

右键项目
《SonarLint和marven对SonarQube的应用》

生成报告
《SonarLint和marven对SonarQube的应用》

具体的
在文件中可以看到代码相应的不合理的地方被蓝色底线划出来了
《SonarLint和marven对SonarQube的应用》

对项目进行合理的重构
《SonarLint和marven对SonarQube的应用》

项目重构

1、数组
list =new ArrayList();
建议修改为
list =new ArrayList<>();

2、日志
e.printStackTrace();
建议修改
log.error(“插入错误”,e);

测试用例可以使用,因为只是测试代码,而
3、包名
应该匹配正在则
^[a-z_]+(.[a-z_][a-z0-9_])$
daoImpl
建议修改
daoimpl

4、工具类没有构造函数
C3P0DBUtils
建议添加

private C3P0DBUtils() {
        throw new IllegalStateException("Utility class");
}

5、抛出异常

/**
     * 获取连接
     *
     * @return
     */
public static Connection getConnection() {
           try {
                  return getDataSource().getConnection();
           } catch (SQLException e) {
                  throw new RuntimeException("连接数据库失败");
           }

    }

Generic exceptions should never be thrown 一般的异常不应该抛出
使用诸如error、RuntimeException、Throwable和Exception会阻碍方法正确的处理情况
系统生成的异常与应用程序生成的错误不同。

/**
     * 获取连接
     *
     * @return
     * @throws MyException 
     */

    public static Connection getConnection() throws MyException {
           try {
                  return getDataSource().getConnection();
           } catch (SQLException e) {
                  throw new MyException("连接数据库失败");
           }

    }

marven使用sonarqube

在marven安装目录下的setting.xml中添加

<profile>
        <id>sonar</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
            <properties>        
            <!-- Optional URL to server. Default value is http://localhost:9000 -->
            <sonar.host.url>http://127.0.0.1:9000</sonar.host.url>
        </properties>
    </profile>

在cmd中运行
mvn clean verify sonar:sonar
或者
mvn clean install sonar:sonar
或者myeclipse中运行
clean verify sonar:sonar
或者
clean install sonar:sonar

访问http://localhost:9000
《SonarLint和marven对SonarQube的应用》
可以看到有一个bug,立马查看一下
错误代码块如下

    public static List<Object> excuteQuery(String sql, Object[] params) {

        // 执行SQL获得结果集

        ResultSet rs = getResultSet(sql, params);

        // 创建ResultSetMetaData对象

        ResultSetMetaData rsmd = null;

        // 结果集列数

        int columnCount = 0;

        try {

            rsmd = rs.getMetaData();

            // 获得结果集列数

            columnCount = rsmd.getColumnCount();

        } catch (SQLException e1) {

            System.out.println(e1.getMessage());

        }

        // 创建List

        List<Object> list = new ArrayList<Object>();

        try {

            // 将ResultSet的结果保存到List中

            while (rs.next()) {

                Map<String, Object> map = new HashMap<String, Object>();

                for (int i = 1; i <= columnCount; i++) {

                    map.put(rsmd.getColumnLabel(i), rs.getObject(i));

                }

                list.add(map);

                // 每一个map代表一条记录,把所有记录存在list中

            }

        } catch (SQLException e) {

            System.out.println(e.getMessage());

        } finally {

            // 关闭所有资源

            close();

        }

        return list;

    }

提示错误如下:
《SonarLint和marven对SonarQube的应用》

修改代码如下

    public static List<Object> excuteQuery(String sql, Object[] params) {
        // 执行SQL获得结果集
        ResultSet rs = getResultSet(sql, params);
        // 创建ResultSetMetaData对象
        ResultSetMetaData rsmd = null;
        List<Object> list =null;
        // 结果集列数
        int columnCount = 0;
        try {
            rsmd = rs.getMetaData();
            // 获得结果集列数
            columnCount = rsmd.getColumnCount();
            // 创建List
             list = new ArrayList<Object>();

                // 将ResultSet的结果保存到List中
                while (rs.next()) {
                    Map<String, Object> map = new HashMap<String, Object>();
                    for (int i = 1; i <= columnCount; i++) {
                        map.put(rsmd.getColumnLabel(i), rs.getObject(i));
                    }
                    list.add(map);
                    // 每一个map代表一条记录,把所有记录存在list中
                }

        } catch (SQLException e1) {
            System.out.println(e1.getMessage());
        } finally {
            // 关闭所有资源
            close();
        }

        return list;
    }

再次扫描
《SonarLint和marven对SonarQube的应用》

cmd手动使用sonarqube

sonar-scanner下载地址
1、编辑sonar-scanner.properties文件

#Configure here general information about the environment, such as SonarQube DB details for example
#No information about specific project should appear here

#----- Default SonarQube server
sonar.host.url=http://localhost:9000

#----- Default source code encoding
sonar.sourceEncoding=UTF-8

2、配置系统环境变量
name=SONAR_RUNNER_HOME
value=D:\mysoftwarestudy\java\sonarqube-7.0\sonar-scanner-3.0.2

添加path变量%SONAR_RUNNER_HOME%\bin;
出现如下结果添加成功
《SonarLint和marven对SonarQube的应用》

3、打开要进行代码分析的项目根目录,新建sonar-project.properties文件
4、输入如下内容

# must be unique in a given SonarQube instance
sonar.projectKey=sonarqube
# this is the name displayed in the SonarQube UI
sonar.projectName=test
sonar.projectVersion=1.0

sonar.sources=src

# Encoding of the source code. Default is default system encoding
#sonar.sourceEncoding=UTF-8

projectName是项目名字,sources是源文件所在的目录
sonar.projectKey是安装sonarqube是设置的key
5、cmd 中输入

sonar-scanner

《SonarLint和marven对SonarQube的应用》

出现以上内容说明成功了

从浏览器查看结果
《SonarLint和marven对SonarQube的应用》

点赞

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注