为什么PMD可以从命令行运行,但是在Maven中不起作用?

时间:2013-02-04 01:59:31

标签: maven pmd

在使用Ant成功使用PMD多年后,我现在正试图让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

1 个答案:

答案 0 :(得分:1)

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

请提供PMD插件目标文档的refer to this section以获取更多信息。

我建议修改配置以将minimumPriority设置为2,这样就可以使BooleanInstantiation的构建失败。

希望这有帮助!