2015-08-21 24 views
1

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

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> 
... 
+0

您应该使用''.... – khmarbaise

+0

@khmarbaise我试过了,但这并没有起作用,至少对于PMD插件来说。 – languitar

+0

你使用哪个maven版本? – khmarbaise

回答

0

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

Maven会不会重用构建/插件的配置设置或建立/ pluginManagement的报告插件。

MVN站点 [...]只使用在<reporting>元素中指定每个报告插件的<configuration>元素中定义的参数,即站点总是忽略指定的每个插件的<configuration>元件中定义的参数在<build>“。

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

所以,即使<pluginManagement>你将不得不复制XML片段为<configuration>部分。

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

我试过了,但是因为我找不到原因而没有工作。 – languitar

+0

什么不工作?您是否尝试过使用这个PMD配置的新项目? –

相关问题