2

在多年成功使用 PMD 和 Ant 之后,我现在尝试让 PMD 在 Maven 中工作,但没有成功。

为了说明我的问题,我创建了一个简单的 Maven 系统(基于 Maven 教程“my-app”hello world 程序)。它的不同之处仅在于包含一行代码,该代码行应使用基本规则集触发 PMD 错误:

Boolean bar = new Boolean("true");

当我从命令行运行 PMD 时,代码中的问题就暴露出来了:

run.sh pmd -d src/main/java -f text -R rulesets/java/basic.xml -language java
maven-pmd-example/src/main/java/com/mycompany/app/App.java:11   Avoid instantiating Boolean objects; reference Boolean.TRUE or Boolean.FALSE or call Boolean.valueOf() instead.

但是,当我在 Maven 中运行 pmd 时,代码中的问题并没有显示出来:

-> mvn pmd:check
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building my-app 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] >>> maven-pmd-plugin:2.7.1:check (default-cli) @ my-app >>>
[INFO] 
[INFO] --- maven-pmd-plugin:2.7.1:pmd (pmd) @ my-app ---
[WARNING] Unable to locate Source XRef to link to - DISABLED
[INFO] 
[INFO] <<< maven-pmd-plugin:2.7.1:check (default-cli) @ my-app <<<
[INFO] 
[INFO] --- maven-pmd-plugin:2.7.1:check (default-cli) @ my-app ---
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.375s
[INFO] Finished at: Sun Feb 03 15:38:02 HST 2013
[INFO] Final Memory: 12M/309M
[INFO] ------------------------------------------------------------------------

这是 pom.xml:

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany.app</groupId>
  <artifactId>my-app</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>my-app</name>
  <url>http://maven.apache.org</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-pmd-plugin</artifactId>
        <version>2.7.1</version>
        <executions>
          <execution>
            <phase>verify</phase> 
            <goals>
              <goal>check</goal> 
            </goals>
          </execution>
        </executions>
        <configuration>
          <verbose>true</verbose>
          <minimumPriority>0</minimumPriority>
          <rulesets>
            <ruleset>rulesets/basic.xml</ruleset>
          </rulesets>
          <targetJdk>1.6</targetJdk>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jxr-plugin</artifactId>
        <version>2.3</version>
      </plugin>
    </plugins>
  </build>
</project>

我已将此示例系统上传到 GitHub,因此您可以查看整个示例系统(如果您愿意,可以下载/使用它):

https://github.com/philipmjohnson/maven-pmd-example

4

1 回答 1

2

这是因为您已将 设置minimumPriority为 0,这将有效地阻止 PMD 评估任何规则(有关优先级的讨论,请参阅此 SO 问题)。

有关更多信息,请参阅PMD 插件的目标文档的这一部分。

我建议修改配置以将 设置minimumPriority为 2,这样您就可以在BooleanInstantiation.

希望这可以帮助!

于 2013-02-04T02:31:32.083 回答