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

时间:2015-08-21 09:21:18

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

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

2 个答案:

答案 0 :(得分:1)

  

有没有办法避免这种重复?

没有

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

mvn site [...]仅使用<configuration>元素中指定的每个报告插件的<reporting>元素中定义的参数,即网站始终忽略<configuration>中指定的每个插件的<build>元素中定义的参数。“

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

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

答案 1 :(得分:0)

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