阿里巴巴Java规约p3c-pmd与maven集成

阿里巴巴规约使用的是pmd代码静态分析工具,且是通过maven-pmd-plugin这个maven插件实现的。所以主要配置maven-pmd-plugin这个插件就能实现使用p3c对代码检查。

配置pom.xml

1 属性配置:配置properties节点

<properties>
    <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    <java.version>1.8java.version> 
properties>

2 PMD插件配置
在build>plugins节点下添加如下配置
PMD插件配置

<plugin>
  <groupId>org.apache.maven.pluginsgroupId>
  <artifactId>maven-pmd-pluginartifactId>
  <version>3.8version>
  <configuration>
    <sourceEncoding>${project.build.sourceEncoding}sourceEncoding>
    <targetJdk>${java.version}targetJdk>
    <printFailingErrors>trueprintFailingErrors>
    <rulesets>
      <ruleset>rulesets/java/ali-comment.xmlruleset>
      <ruleset>rulesets/java/ali-concurrent.xmlruleset>
      <ruleset>rulesets/java/ali-constant.xmlruleset>
      <ruleset>rulesets/java/ali-exception.xmlruleset>
      <ruleset>rulesets/java/ali-flowcontrol.xmlruleset>
      <ruleset>rulesets/java/ali-naming.xmlruleset>
      <ruleset>rulesets/java/ali-oop.xmlruleset>
      <ruleset>rulesets/java/ali-orm.xmlruleset>
      <ruleset>rulesets/java/ali-other.xmlruleset>
      <ruleset>rulesets/java/ali-set.xmlruleset>
    rulesets>
  configuration>
  <executions>
    <execution>
      <phase>verifyphase>
      <goals>
        <goal>checkgoal>
      goals>
    execution>
  executions>
  <dependencies>
    <dependency>
      <groupId>com.alibaba.p3cgroupId>
      <artifactId>p3c-pmdartifactId>
      <version>1.3.5version>
    dependency>
  dependencies>
plugin>

3 编译插件配置:主要配置编译时使用的jdk版本(Spring-Boot 项目不需要设置,因为parent中已设置)
在build>plugins节点下添加如下配置
编译插件配置


<plugin>
  <groupId>org.apache.maven.pluginsgroupId>
  <artifactId>maven-compiler-pluginartifactId>
  <version>3.7.0version>
  <configuration>
    <source>${java.version}source>
    <target>${java.version}target>
  configuration>
plugin>

4 整体配置

<project xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0modelVersion>
  <groupId>com.weibogroupId>
  <artifactId>helloartifactId>
  <version>1.0-SNAPSHOTversion>
  <packaging>jarpackaging>
  <name>helloname>
  <url>http://maven.apache.orgurl>


  
  <properties>
    <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    <java.version>1.8java.version>
  properties>
  <build>
    <plugins>

      
      <plugin>
        <groupId>org.apache.maven.pluginsgroupId>
        <artifactId>maven-pmd-pluginartifactId>
        <version>3.8version>
        <configuration>
          <sourceEncoding>${project.build.sourceEncoding}sourceEncoding>
          <targetJdk>${java.version}targetJdk>
          <printFailingErrors>trueprintFailingErrors>
          
          <rulesets>
            <ruleset>rulesets/java/ali-comment.xmlruleset>
            <ruleset>rulesets/java/ali-concurrent.xmlruleset>
            <ruleset>rulesets/java/ali-constant.xmlruleset>
            <ruleset>rulesets/java/ali-exception.xmlruleset>
            <ruleset>rulesets/java/ali-flowcontrol.xmlruleset>
            <ruleset>rulesets/java/ali-naming.xmlruleset>
            <ruleset>rulesets/java/ali-oop.xmlruleset>
            <ruleset>rulesets/java/ali-orm.xmlruleset>
            <ruleset>rulesets/java/ali-other.xmlruleset>
            <ruleset>rulesets/java/ali-set.xmlruleset>
          rulesets>
        configuration>
        <executions>
          
          <execution>
            <id>pmd-check-verifyid>
            <phase>verifyphase>
            <goals>
              <goal>checkgoal>
            goals>
          execution>
          
          <execution>
            <id>pmd-pmd-siteid>
            <phase>sitephase>
            <goals>
              <goal>pmdgoal>
            goals>
          execution>
        executions>
        
        <dependencies>
          <dependency>
            <groupId>com.alibaba.p3cgroupId>
            <artifactId>p3c-pmdartifactId>
            <version>1.3.5version>
          dependency>
        dependencies>
      plugin>
      
      <plugin>
        <groupId>org.apache.maven.pluginsgroupId>
        <artifactId>maven-compiler-pluginartifactId>
        <version>3.7.0version>
        <configuration>
          <source>${java.version}source>
          <target>${java.version}target>
        configuration>
      plugin>
    plugins>
  build>

  
  <reporting>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.pluginsgroupId>
        <artifactId>maven-jxr-pluginartifactId>
        <version>2.3version>
      plugin>
    plugins>
  reporting>
project>

5 多模块项目配置请考虑简化配置,不要同样的配置所有项目都配置一遍。可以考虑使用parent标签或使用依赖作用域为import
Spring-Boot项目默认已经继承了parent,maven的单继承不允许再使用parent了,所以可以使用import来简化配置。
参见:maven中import scope依赖方式解决单继承问题的理解

构建命令

pmd插件有两个goal:pmd:pmd, pmd:check

  1. pmd:pmd
    该goal无论代码是否有误都会构建成功。会生成分析报告在target目录
  2. pmd:check
    该goal如果代码检查不通过,构建失败。会生成分析报告在target目录
    文件已经绑定pmd:pmd到site生命周期,pmd:check到verify生命周期

maven生命周期介绍请见:Maven入门指南⑦:Maven的生命周期和插件

构建结果示例

  1. 命令行输出

  2. 分析报告有两种形式,一种是xml,一种是html
    target/pmd.xml
    “`xml

    **target/site/pmd.html**

    使用浏览器打开
![这里写图片描述](http://docs.trustchain.com/download/attachments/983869/%E6%B7%B1%E5%BA%A6%E6%88%AA%E5%9B%BE_%E9%80%89%E6%8B%A9%E5%8C%BA%E5%9F%9F_20180627164425.png?version=1&modificationDate=1530090276000&api=v2)


# maven-jxr-plugin插件的使用
配置maven-jxr-plugin
```xml


  
    
      org.apache.maven.plugins
      maven-jxr-plugin
      2.3
    
  

执行 mvn site ,打开target/site/pmd.html,发现行号位置生成了超链接

点击行号,可以浏览代码了

你可能感兴趣的:(java)