在构建和报告部分之间共享 Maven 插件配置

Share maven plugin configuration between build and reporting sections

我有一个 Maven 项目,我在 pom.xml 的构建和报告部分使用了几个插件(pmd、checkstyle)。前者用于执行多个约束,后者用于站点报告。插件的这些调用大多共享公共 <configuration> 元素,到目前为止我找到的唯一解决方案是复制相应的 XML 片段。有什么办法可以避免这种重复?

示例 pom.xml 片段:

<project ...>
...
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-pmd-plugin</artifactId>
                <version>${plugin.pmd.version}</version>
                <configuration>
                    <targetJdk>${target.java.version}</targetJdk>
                    <rulesets>
                        <ruleset>${project.basedir}/codecheck/pmd-rules.xml</ruleset>
                    </rulesets>
                    <excludeRoots>
                        <excludeRoot>${project.basedir}/target/generated-sources/protobuf/java</excludeRoot>
                    </excludeRoots>
                    <failOnViolation>${failOnStyleError}</failOnViolation>
                    <verbose>true</verbose>
                </configuration>
                <executions>
                    <execution>
                        <id>pmd-check</id>
                        <phase>validate</phase>
                        <goals>
                            <goal>check</goal>
                            <goal>cpd-check</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
...
    <reporting>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-pmd-plugin</artifactId>
                <version>${plugin.pmd.version}</version>
                <configuration>
                    <targetJdk>${target.java.version}</targetJdk>
                    <rulesets>
                        <ruleset>${project.basedir}/codecheck/pmd-rules.xml</ruleset>
                    </rulesets>
                    <excludeRoots>
                        <excludeRoot>${project.basedir}/target/generated-sources/protobuf/java</excludeRoot>
                    </excludeRoots>
                </configuration>
            </plugin>
        </plugins>
    </reporting>
...

它适用于我 <pluginManagement> 定义如下:

   <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-pmd-plugin</artifactId>
          <version>${plugin.pmd.version}</version>
          <configuration>
            <targetJdk>${target.java.version}</targetJdk>
            <skipEmptyReport>false</skipEmptyReport>
            <failOnViolation>>${failOnStyleError}</failOnViolation>
            <printFailingErrors>true</printFailingErrors>
            <rulesets>
              <ruleset>codecheck/pmd-rules.xml</ruleset>
            </rulesets>
            <excludeRoots>
              <excludeRoot>target/generated-sources</excludeRoot>
            </excludeRoots>
          </configuration>
        </plugin>
     </plugins>
  </pluginManagement>

那么就可以在<build>中使用了:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-pmd-plugin</artifactId>
    <executions>
      <execution>
        <phase>validate</phase>
        <goals>
          <goal>check</goal>
          <goal>cpd-check</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

<reporting>

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-pmd-plugin</artifactId>
    <version>3.5</version>
    <reportSets>
      <reportSet>
        <reports>
          <report>pmd</report>
        </reports>
      </reportSet>
    </reportSets>
  </plugin>

Is there any way to avoid this duplication?

没有

Maven 不会为报告插件重用 build/plugins 或 build/pluginManagement 的配置设置。

"mvn site [...] 仅使用 <reporting> 元素中指定的每个报告插件的 <configuration> 元素中定义的参数,即 site 总是忽略在 <build>.

中指定的每个插件的 <configuration> 元素中定义的参数

(https://maven.apache.org/guides/mini/guide-configuring-plugins.html#Configuring_Reporting_Plugins)

因此,即使使用 <pluginManagement>,您也必须复制 <configuration> 部分的 XML 片段。