2015-08-21 25 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配置的新項目? –

相關問題