Philip Johnson
Philip Johnson

Reputation: 1575

Why is PMD OK from command line, but does not work from within Maven?

After many years of successful use of PMD with Ant, I am now trying without success to get PMD to work from within Maven.

To illustrate my problem, I have created a simple Maven system (based upon the Maven tutorial "my-app" hello world program). It differs only in the inclusion of a line of code which should trigger a PMD error using the basic ruleset:

Boolean bar = new Boolean("true");

When I run PMD from the command line, the problem in the code is revealed:

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.

However, when I run pmd from within Maven, the problem in the code is not revealed:

-> 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] ------------------------------------------------------------------------

Here is the 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>

I have uploaded this example system to GitHub so you can see the entire example system (and download/play with it if you would like):

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

Upvotes: 2

Views: 2663

Answers (1)

Peter Mularien
Peter Mularien

Reputation: 2638

It's because you have set the minimumPriority to 0, which will effectively prevent PMD from evaluating any rules at all (refer to this SO question for a discussion on priority).

Please refer to this section of the goal documentation for the PMD plugin for more information.

I'd suggest modifying the configuration to set the minimumPriority to 2 so that you can fail the build for BooleanInstantiation.

Hope this helps!

Upvotes: 2

Related Questions