1. 程式人生 > >阿里巴巴Java規約p3c-pmd與maven整合

阿里巴巴Java規約p3c-pmd與maven整合

阿里巴巴規約使用的是pmd程式碼靜態分析工具,且是通過maven-pmd-plugin這個maven外掛實現的。所以主要配置maven-pmd-plugin這個外掛就能實現使用p3c對程式碼檢查。

配置pom.xml

1 屬性配置:配置properties節點

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version> <!-- Spring-Boot 專案不需要設定該項(因為parent中已設定) -->
</properties>

2 PMD外掛配置
在build>plugins節點下新增如下配置
PMD外掛配置

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-pmd-plugin</artifactId>
  <version>3.8</version>
  <configuration>
    <sourceEncoding>${project.build.sourceEncoding}</sourceEncoding
>
<targetJdk>${java.version}</targetJdk> <printFailingErrors>true</printFailingErrors> <rulesets> <ruleset>rulesets/java/ali-comment.xml</ruleset> <ruleset>rulesets/java/ali-concurrent.xml</ruleset> <ruleset>rulesets/java/ali-constant.xml</ruleset
>
<ruleset>rulesets/java/ali-exception.xml</ruleset> <ruleset>rulesets/java/ali-flowcontrol.xml</ruleset> <ruleset>rulesets/java/ali-naming.xml</ruleset> <ruleset>rulesets/java/ali-oop.xml</ruleset> <ruleset>rulesets/java/ali-orm.xml</ruleset> <ruleset>rulesets/java/ali-other.xml</ruleset> <ruleset>rulesets/java/ali-set.xml</ruleset> </rulesets> </configuration> <executions> <execution> <phase>verify</phase> <goals> <goal>check</goal> </goals> </execution> </executions> <dependencies> <dependency> <groupId>com.alibaba.p3c</groupId> <artifactId>p3c-pmd</artifactId> <version>1.3.5</version> </dependency> </dependencies> </plugin>

3 編譯外掛配置:主要配置編譯時使用的jdk版本(Spring-Boot 專案不需要設定,因為parent中已設定)
在build>plugins節點下新增如下配置
編譯外掛配置

<!-- 編譯外掛 Spring-Boot 專案不需要設定,因為parent中已設定 -->
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin</artifactId>
  <version>3.7.0</version>
  <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.0</modelVersion>
  <groupId>com.weibo</groupId>
  <artifactId>hello</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>
  <name>hello</name>
  <url>http://maven.apache.org</url>


  <!-- 屬性配置 -->
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
  </properties>
  <build>
    <plugins>

      <!-- PMD外掛 -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-pmd-plugin</artifactId>
        <version>3.8</version>
        <configuration>
          <sourceEncoding>${project.build.sourceEncoding}</sourceEncoding>
          <targetJdk>${java.version}</targetJdk>
          <printFailingErrors>true</printFailingErrors>
          <!-- 程式碼檢查規則 -->
          <rulesets>
            <ruleset>rulesets/java/ali-comment.xml</ruleset>
            <ruleset>rulesets/java/ali-concurrent.xml</ruleset>
            <ruleset>rulesets/java/ali-constant.xml</ruleset>
            <ruleset>rulesets/java/ali-exception.xml</ruleset>
            <ruleset>rulesets/java/ali-flowcontrol.xml</ruleset>
            <ruleset>rulesets/java/ali-naming.xml</ruleset>
            <ruleset>rulesets/java/ali-oop.xml</ruleset>
            <ruleset>rulesets/java/ali-orm.xml</ruleset>
            <ruleset>rulesets/java/ali-other.xml</ruleset>
            <ruleset>rulesets/java/ali-set.xml</ruleset>
          </rulesets>
        </configuration>
        <executions>
          <!-- 繫結pmd:check到verify生命週期 -->
          <execution>
            <id>pmd-check-verify</id>
            <phase>verify</phase>
            <goals>
              <goal>check</goal>
            </goals>
          </execution>
          <!-- 繫結pmd:pmd到site生命週期 -->
          <execution>
            <id>pmd-pmd-site</id>
            <phase>site</phase>
            <goals>
              <goal>pmd</goal>
            </goals>
          </execution>
        </executions>
        <!-- p3c依賴 -->
        <dependencies>
          <dependency>
            <groupId>com.alibaba.p3c</groupId>
            <artifactId>p3c-pmd</artifactId>
            <version>1.3.5</version>
          </dependency>
        </dependencies>
      </plugin>
      <!-- 編譯外掛 Spring-Boot 專案不需要設定,因為parent中已設定 -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.7.0</version>
        <configuration>
          <source>${java.version}</source>
          <target>${java.version}</target>
        </configuration>
      </plugin>
    </plugins>
  </build>

  <!-- 用於生成錯誤到程式碼內容的連結 -->
  <reporting>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jxr-plugin</artifactId>
        <version>2.3</version>
      </plugin>
    </plugins>
  </reporting>
</project>

5 多模組專案配置請考慮簡化配置,不要同樣的配置所有專案都配置一遍。可以考慮使用parent標籤或使用依賴作用域為<scope>import</scope>
Spring-Boot專案預設已經繼承了parent,maven的單繼承不允許再使用parent了,所以可以使用<scope>import</scope>來簡化配置。
參見: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生命週期

構建結果示例

  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
<!-- 用於生成錯誤到程式碼內容的連結 -->
<reporting>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-jxr-plugin</artifactId>
      <version>2.3</version>
    </plugin>
  </plugins>
</reporting>

執行 mvn site ,開啟target/site/pmd.html,發現行號位置生成了超連結
這裡寫圖片描述
點選行號,可以瀏覽程式碼了
這裡寫圖片描述