maven-pmd-plugin - includeTests but with different ruleset?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

maven-pmd-plugin - includeTests but with different ruleset?

Ted Petersson
Hi all,
is there any way to run the maven-pmd-plugin on both the main and test
code, but with different rulesets?

It does not seem to be possible to run 2 executions with different
ruleset configurations...

What I have:
...
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-pmd-plugin</artifactId>
        <version>3.12.0</version>
        <configuration>
          <rulesets>
            <ruleset>pmd-rules.xml</ruleset>
          </rulesets>
          <printFailingErrors>true</printFailingErrors>
          <includeTests>true</includeTests>
        </configuration>

        <executions>
          <execution>
            <id>pmd-check</id>
            <phase>compile</phase>
            <goals>
              <goal>check</goal>
              <goal>cpd-check</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

I would like to have is something like this:
...
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-pmd-plugin</artifactId>
        <version>3.12.0</version>
        <configuration>
          <rulesets>
            <ruleset>pmd-rules.xml</ruleset>
          </rulesets>
          <printFailingErrors>true</printFailingErrors>
        </configuration>

        <executions>
          <execution>
            <id>pmd-main-check</id>
            <phase>compile</phase>
            <goals>
              <goal>check</goal>
              <goal>cpd-check</goal>
            </goals>
            <configuration>
              <rulesets>
                <ruleset>pmd-main-rules.xml</ruleset>  <!-- "normal" rules
-->
              </rulesets>
            </configuration>
          </execution>

          <execution>
            <id>pmd-test-check</id>
            <phase>compile</phase>
            <goals>
              <goal>check</goal>
              <goal>cpd-check</goal>
            </goals>
            <configuration>
              <rulesets>
                <ruleset>pmd-test-rules.xml</ruleset>  <!-- less strict
rules for the tests -->
              </rulesets>
              <includeTests>true</includeTests>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

But it does not seem to be possible to have ruleset-configuration
inside an execution tag. Is it possible to solve this in any way?

/Ted
Reply | Threaded
Open this post in threaded view
|

Re: maven-pmd-plugin - includeTests but with different ruleset?

Anthony Whitford
Hi Ted,

You can definitely specify configuration inside execution according to the advice here:  http://maven.apache.org/guides/mini/guide-configuring-plugins.html#Using_the_executions_Tag <http://maven.apache.org/guides/mini/guide-configuring-plugins.html#Using_the_executions_Tag>

You may want to rethink the phase — the default phase binding is for verify, not compile.  I suggest sticking with the default.  In your example, the “test set” is running before the test-compile phase — your test-compile phase should be successful before running PMD against it.

I have done something similar in the past where I had the PMD plugin declared twice — once in the <build> section with “required” rules that will fail the build when violated, and then again in the <reporting> section with “thorough” rules that will report a more comprehensive set of advice (as a report).  Is this really what you need?

To help debug, I recommend reviewing the effective-pom (mvn help:effective-pom), and running mvn with the -X debug switch.

Hope this helps,

Anthony


> On Oct 17, 2019, at 11:37 PM, Ted Petersson <[hidden email]> wrote:
>
> Hi all,
> is there any way to run the maven-pmd-plugin on both the main and test
> code, but with different rulesets?
>
> It does not seem to be possible to run 2 executions with different
> ruleset configurations...
>
> What I have:
> ...
>  <build>
>    <plugins>
>      <plugin>
>        <artifactId>maven-pmd-plugin</artifactId>
>        <version>3.12.0</version>
>        <configuration>
>          <rulesets>
>            <ruleset>pmd-rules.xml</ruleset>
>          </rulesets>
>          <printFailingErrors>true</printFailingErrors>
>          <includeTests>true</includeTests>
>        </configuration>
>
>        <executions>
>          <execution>
>            <id>pmd-check</id>
>            <phase>compile</phase>
>            <goals>
>              <goal>check</goal>
>              <goal>cpd-check</goal>
>            </goals>
>          </execution>
>        </executions>
>      </plugin>
>    </plugins>
>  </build>
>
> I would like to have is something like this:
> ...
>  <build>
>    <plugins>
>      <plugin>
>        <artifactId>maven-pmd-plugin</artifactId>
>        <version>3.12.0</version>
>        <configuration>
>          <rulesets>
>            <ruleset>pmd-rules.xml</ruleset>
>          </rulesets>
>          <printFailingErrors>true</printFailingErrors>
>        </configuration>
>
>        <executions>
>          <execution>
>            <id>pmd-main-check</id>
>            <phase>compile</phase>
>            <goals>
>              <goal>check</goal>
>              <goal>cpd-check</goal>
>            </goals>
>            <configuration>
>              <rulesets>
>                <ruleset>pmd-main-rules.xml</ruleset>  <!-- "normal" rules
> -->
>              </rulesets>
>            </configuration>
>          </execution>
>
>          <execution>
>            <id>pmd-test-check</id>
>            <phase>compile</phase>
>            <goals>
>              <goal>check</goal>
>              <goal>cpd-check</goal>
>            </goals>
>            <configuration>
>              <rulesets>
>                <ruleset>pmd-test-rules.xml</ruleset>  <!-- less strict
> rules for the tests -->
>              </rulesets>
>              <includeTests>true</includeTests>
>            </configuration>
>          </execution>
>        </executions>
>      </plugin>
>    </plugins>
>  </build>
>
> But it does not seem to be possible to have ruleset-configuration
> inside an execution tag. Is it possible to solve this in any way?
>
> /Ted