1

In Gradle, we can specify different PMD configurations (including different rulesets) for the pmdMain and pmdTest source sets. e.g.

pmdMain {
    ruleSetFiles = files("$javaBuildSystemRoot/src-pmd-rulesets.xml")
}

pmdTest {
    ruleSetFiles = files("$javaBuildSystemRoot/test-pmd-rulesets.xml")
}

We want to be less stringent on test code than main code.

There is a separate maven based project, where we cannot use gradle currently.But for now, we would like to at least apply the 2 different rulesets based on main vs test. It is a single module single project, using the maven PMD plugin.

How do we do this in the Maven pom file?

Raja Nadar
  • 9,409
  • 2
  • 32
  • 41

1 Answers1

0

It is "rather unconventional" to pmd over test sources, but that is not part of the question. :)

Using the executions tag and utilizing maven-pmd-plugin, you can do this with maven.


EDIT: In short & applied to the given input (and maybe more than you wanted/need), it enables/forces you to make both checks in every build:

<project><build><plugins>
<plugin>
    <artifactId>maven-pmd-plugin</artifactId>
    <version>3.11.0</version> <!-- latest up-to-date -->
    <executions>
       <execution>
           <id>pmd-execution</id>
           <goals>
               <goal>check</goal>
           </goals>
           <configuration>
               <rulesets>
                   <ruleset>/path/to/.../src-pmd-rulesets.xml</ruleset>
               </rulesets>
           </configuration>
      </execution>
      <execution>
           <id>pmd-test-execution</id>
           <goals>
              <goal>check</goal>
           </goals>
           <configuration>
               <includeTests>true</includeTests> <!-- this defaults to false! -->
               <rulesets>
                   <ruleset>/path/to/.../test-pmd-rulesets.xml</ruleset>
               </rulesets>
           </configuration>
        </execution>
    </executions>
</plugin>
...

See also: Can I configure multiple plugin executions in pluginManagement, and choose from them in my child POM?


EDIT 2: If you indeed don't need "both executions" (in 1 build), but only "two configurations" for "different builds", then: Maven Profiles fits your needs (with profiles ... your "possibilities converge to infinity") !

You would introduce profiles like:

 <project>
 ...
 <profiles>
   <profile>
     <id>pmdMain</id>
     <properties>
         <myPmdRuleSetLocation>/path/to/.../src-pmd-rulesets.xml</myPmdRuleSetLocation>
         <myPmdTestsInclude>false</myPmdTestsInclude>
     </properties>
   </profile> 
   <profile>
     <id>pmdTest</id>
     <properties>
         <myPmdRuleSetLocation>/path/to/.../test-pmd-rulesets.xml</myPmdRuleSetLocation>
         <myPmdTestsInclude>true</myPmdTestsInclude>
     </properties>
   </profile> 
 <profiles>
 ...
 </project>

And use it in your (single execution) pmd-plugin configuration:

...
  <ruleset>${myPmdRuleSetLocation}</ruleset>
  <includeTests>${myPmdTestsInclude}</includeTests>
...

Please read further on profiles and their activation.

(additionally <profile/> can contain & override <build/> tag!)

xerx593
  • 12,237
  • 5
  • 33
  • 64
  • xerx593, did you mean to add the 'phase' tags in the above answer OR a different goal? Without that, both the executions look to be the same. – Raja Nadar Feb 10 '19 at 12:15
  • ..."i think" `phase` is optional. of course you would have to adjust and test both configs: inside configutation - my answer is just an outline.. – xerx593 Feb 10 '19 at 14:40
  • 1
    If you use the plugin goal `pmd`, `true` should also be added to the second execution or profile. – eerriicc Aug 19 '20 at 12:26
  • 1
    Why is it "rather unconventional" to run PMD on tests? PMD has rules that are specifically for test files (like the one that ensures there is an assert in every test - https://pmd.sourceforge.io/pmd-6.53.0/pmd_rules_java_bestpractices.html#junittestsshouldincludeassert). – Ezra Jan 30 '23 at 20:26
  • because (i.m.o) doing statical *code* analysis (pmd) on "meta code" (which test code is) is "nit-picking" or "being more catholic than the pope"! (but then also interesting: why different rulesets??;) ..empiricial: this question is by far less popular than (e.g.) [this](https://stackoverflow.com/q/46728525/592355), @Ezra – xerx593 Jan 30 '23 at 20:39
  • the existence of this and test related rules implies, that pmd works also on test code (which is also "code" by its means:)... but then again (if being more catholic than the pope): why different rule sets? (the test rules don't hurt/slow much "prod code analsys") ..and if it was "rather conventional", then why would the [maven-pmd people default this setting to `false`](https://maven.apache.org/plugins/maven-pmd-plugin/pmd-mojo.html#includeTests)!? – xerx593 Jan 31 '23 at 14:56
  • @xerx593 I see your point. I think the desire for 2 rulesets is exactly what some are looking for because of the test code being "meta code" as you describe. People want the ability to have more relaxed rules for test/meta code. FWIW, you also have two rulesets; it just happens to be that the ruleset you apply to test/meta code happens to be empty. :) – Ezra Feb 02 '23 at 23:18