【问题标题】:Share maven plugin configuration between build and reporting sections在构建和报告部分之间共享 Maven 插件配置
【发布时间】:2015-11-15 04:37:11
【问题描述】:

我有一个 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>
...

【问题讨论】:

标签: java maven maven-3 pom.xml pmd


【解决方案1】:

有什么办法可以避免这种重复吗?

没有。

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

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

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

因此,即使使用&lt;pluginManagement&gt;,您也必须复制&lt;configuration&gt; 部分的XML 片段。

【讨论】:

    【解决方案2】:

    它适用于我,&lt;pluginManagement&gt; 定义如下:

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

    那么就可以在&lt;build&gt;中使用了:

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

    &lt;reporting&gt;:

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

    【讨论】:

    • 我试过了,但由于我无法找到的原因,这不起作用。
    • 究竟是什么不起作用?您是否尝试过仅使用此 PMD 配置在新项目中使用它?
    • 正如@ufkub 所说,这不起作用,如果您记得pluginManagement 在&lt;build&gt; 中,这也是有道理的,因此它对pom 的其他部分不“可见”是有道理的不在&lt;build&gt; 下,&lt;reporting&gt; 不在。
    • 这应该只在 2017 年春季发布的 Maven 3.5 之后才有效;见the related issue
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-19
    • 1970-01-01
    • 2018-08-23
    • 2017-09-28
    • 2013-07-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多