sonar-pmd 插件添加 P3C 支持

Sonar 是一个用于代码质量管理的开放平台。通过插件机制,Sonar 可以集成不同的测试工具,代码分析工具,以及持续集成工具。与持续集成工具(例如 Hudson/Jenkins 等)不同,Sonar 并不是简单地把不同的代码检查工具结果(例如 FindBugs,PMD 等)直接显示在 Web 页面上,而是通过不同的插件对这些结果进行再加工处理,通过量化的方式度量代码质量的变化,从而可以方便地对不同规模和种类的工程进行代码质量管理。在对其他工具的支持方面,Sonar 不仅提供了对 IDE 的支持,可以在 Eclipse 和 IntelliJ IDEA 这些工具里联机查看结果;同时 Sonar 还对大量的持续集成工具提供了接口支持,可以很方便地在持续集成中使用 Sonar。此外,Sonar 的插件还可以对 Java 以外的其他编程语言提供支持,对国际化以及报告文档化也有良好的支持。

sonar-pmd是sonar官方的支持pmd的插件,但是还不支持p3c,实际上官方的pmd版本还一直没有更新,pmd最新版本已更新至5.8.1,但是插件中的版本依然是5.2.4。
Clone:SonarQubeCommunity/sonar-pmd
https://github.com/SonarQubeCommunity/sonar-pmd.git

准备进行修改:

  1. 添加POM依赖包:
    <!-- https://mvnrepository.com/artifact/com.alibaba.p3c/p3c-pmd -->
    <dependency>
      <groupId>com.alibaba.p3c</groupId>
      <artifactId>p3c-pmd</artifactId>
      <version>1.3.0</version>
    </dependency>
    
    下面以一条Rule为例进行修改添加;
  2. 添加 pmd.properties

    rule.pmd.CommentsMustBeJavadocFormatRule.name=CommentsMustBeJavadocFormatRule

  3. 添加 rules.xml

    <rule key="CommentsMustBeJavadocFormatRule">
    <priority>MAJOR</priority>
    <configKey><![CDATA[rulesets/java/ali-comment.xml/CommentsMustBeJavadocFormatRule]]>        </configKey>
    </rule>
    
  4. 添加pmd-model.xml文件(具体含义不详)

    <chc>
                <rule-repo>p3c</rule-repo>
                <rule-key>CommentsMustBeJavadocFormatRule</rule-key>
                <prop>
                    <key>remediationFunction</key>
                    <txt>CONSTANT_ISSUE</txt>
                </prop>
                <prop>
                    <key>offset</key>
                    <val>2</val>
                    <txt>min</txt>
                </prop>
    </chc>
    
  5. 添加描述文件 — CommentsMustBeJavadocFormatRule.html.内容来自p3c对应xml 用于错误详情页面的展示

    <p>Look for qualified this usages in the same class.</p>
    <p>Examples:</p>
    <pre>
    /**
    *
    * XXX class function description.
    *
    */
    public class XxClass implements Serializable {
       private static final long serialVersionUID = 113323427779853001L;
       /**
       * id
       */
       private Long id;
       /**
       * title
       */
       private String title;
       /**
       * find by id
       *
       * @param ruleId rule id
       * @param page start from 1
       * @return Result<Xxxx>
       */
       public Result<Xxxx> funcA(Long ruleId, Integer page) {
           return null;
       }
    }
    </pre>
    

暂时是在原有基础上添加,没有删除现有部分rule

测试分析:

  1. 安装sonarqube:
    下载sonarqube并解压,需要java8,自带web服务器:
    将上面项目mvn package 出来的jar包拷贝到 sonarqube-6.5\extensions\plugins下面
    运行sonarqube-6.5\bin\windows-x86-64\StartSonar.bat

  2. admin/admin登录,会生成一个类似token的字符串

  3. 进入Quality Profiles,新建Quality Profiles保存。进入新建的Quality Profiles,
    点击左侧的ActiveMore,进入rules 找到刚添加的rule,列表右边会有active按钮,激活即可。
    (以下为官方Usage)

    Usage
    In the quality profile, activate some rules from PMD and run an analysis on your project. Set the sonar.java.source property to tell PMD which version of Java your source code complies to. The default value is 1.5. Possible values: 1.4, 1.5 or 5, 1.6 or 6, 1.7 or 7. Since version 2.2 of the plugin, this property can also be set to 1.8 or 8.
    
  4. 分析
    进入项目文件夹开始分析:Dsonar.login 就是上面的token

    D:\GreenPro\apache-maven-3.3.9\bin\
    mvn sonar:sonar -Dsonar.host.url=http://localhost:9000 -Dsonar.login=8f59dbbe29d160a9727038333d33acf31e11caea
    
  5. 切换Profiles
    跑第一遍后projects列表会有刚刚的项目,这时候可以去项目详情,Administratoration->Quality Profiles,切换Profile

  6. 然后再跑一边分析即可。

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×