我们如何在 Maven 中为主要和测试 java 代码集设置不同的 PMD 规则集?

How can we have different PMD rulesets in Maven for the main and test java code sets?

在Gradle中,我们可以为pmdMain和pmdTest源集指定不同的PMD配置(包括不同的规则集)。例如

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

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

我们希望对测试代码的严格程度低于主代码。

有一个单独的基于 maven 的项目,我们暂时不能在其中使用 gradle currently.But,我们希望至少应用基于 main vs test 的 2 个不同的规则集。是单模块单项目,使用maven PMD插件。

我们如何在 Maven pom 文件中执行此操作?

通过测试源 pmd 是 "rather unconventional",但这不是问题的一部分。 :)

Using the executions tag and utilizing maven-pmd-plugin,你可以用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>${javaBuildSystemRoot}/src-pmd-rulesets.xml</ruleset>
               </rulesets>
           </configuration>
      </execution>
      <execution>
           <id>pmd-test-execution</id>
           <goals>
              <goal>check</goal>
           </goals>
           <configuration>
               <rulesets>
                   <ruleset>${javaBuildSystemRoot}/test-pmd-rulesets.xml</ruleset>
               </rulesets>
           </configuration>
        </execution>
    </executions>
</plugin>
...

另请参阅: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") !

您可以介绍如下个人资料:

 <project>
 ...
 <profiles>
   <profile>
     <id>pmdMain</id>
     <properties>
         <myPmdRuleSetLocation>${javaBuildSystemRoot}/src-pmd-rulesets.xml</myPmdRuleSetLocation>
     </properties>
   </profile> 
   <profile>
     <id>pmdTest</id>
     <properties>
         <myPmdRuleSetLocation>${javaBuildSystemRoot}/test-pmd-rulesets.xml</myPmdRuleSetLocation>
     </properties>
   </profile> 
 <profiles>
 ...
 </project>

并在您的(单个)pmd-plugin 配置中使用它:

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

read further 配置文件及其激活。

(另外 <profile/> 可以包含和覆盖 <build/> 标签!)