2011-12-12 69 views
0

我將Maven項目與現有的自定義Checkstyle規則集合在一起,編譯爲jar並在Nexus存儲庫中提供。規則集對Checkstyle 5.0有依賴性,並且與新版本不兼容。如何在Maven構建中使用較舊的Checkstyle版本?

我在配置Maven時使用這個較舊的Checkstyle版本和我的規則時遇到了問題。

這裏是我的項目佈局:

root/ 
    pom.xml 
    buildsupport/ 
    pom.xml 
    src/main/resources/checkstyle 
     mycompany-coding-rules.xml 
    src/main/java/com.mycompany.checkstyletest/ 
     CheckstyleViolator.java 
    [other projects with pom.xml and java source...] 

這是我的父母pom.xml

<project> 
... 
<build> 
    <pluginManagement> 
     <plugins> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-checkstyle-plugin</artifactId> 
       <version>2.5</version> 
      </plugin> 
     </plugins> 
    </pluginManagement> 
<plugins> 
    ... 
    <plugin> 
     <groupId>org.apache.maven.plugins</groupId> 
     <artifactId>maven-site-plugin</artifactId> 
     <version>3.0</version> 
     <dependencies> 
      <dependency> 
       <groupId>com.mycompany</groupId> 
       <artifactId>buildsupport</artifactId> 
       <version>X.Y.Z</version> 
      </dependency> 
      <dependency> 
       <groupId>checkstyle</groupId> 
       <artifactId>checkstyle</artifactId> 
       <version>5.0</version> 
      </dependency> 
      <dependency> 
       <groupId>com.mycompany.checkstyle</groupId> 
       <artifactId>mycompany-checkstyle</artifactId> 
       <version>1.1</version> 
      </dependency> 
     </dependencies> 
    </plugin> 
</plugins> 
</build> 

<reporting> 
    <plugins> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-checkstyle-plugin</artifactId> 
      <version>2.5</version> 
      <configuration> 
       <configLocation>checkstyle/mycompany-coding-rules.xml</configLocation> 
       <packageNamesLocation>checkstyle_packages.xml</packageNamesLocation> 
      </configuration> 
     </plugin> 
    </plugins> 
</reporting> 
... 

如果我忽略從Maven的的CheckStyle-插件<version>2.5</version><reporting><plugins><plugin>...<build><pluginManagement><plugins><plugin>,並運行mvn site在根目錄中,我得到頂級項目的失敗:

... 
[INFO] Generating "Checkstyle" report --- maven-checkstyle-plugin:2.8 
... 
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-site-plugin:3.0:site (default-site) on project mytoplevel-project: 
Error during page generation: Error rendering Maven report: Failed during checkstyle configuration: 
cannot initialize module TreeWalker - TreeWalker is not allowed as a parent of com.mycompany.checkstyle.checks.coding.ImportOrderCheck -> [Help 1] 

添加<version>2.5</version>在上面的XML允許頂級項目成功,但同樣的錯誤是出現在第一個孩子的項目:

... 
[INFO] Generating "Checkstyle" report --- maven-checkstyle-plugin:2.5 
... 
[INFO] mytoplevel-project ................................ SUCCESS [4.528s] 
[INFO] buildsupport ...................................... FAILURE [0.489s] 
... 
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-site-plugin:3.0:site (default-site) on project buildsupport: 
Error during page generation: Error rendering Maven report: Failed during checkstyle configuration: 
cannot initialize module TreeWalker - TreeWalker is not allowed as a parent of com.mycompany.checkstyle.checks.coding.ImportOrderCheck -> [Help 1] 

我敢肯定這個錯誤是由於最新當使用規則集作爲針對Checkstyle 5.5的Eclipse插件時,我看到了同樣的錯誤 - 它通過將Checkstyle插件版本回滾到5.0來修復。

我不明白的是孩子項目如何挑選錯誤的Checkstyle版本。

回答

0

答:父pom.xml需要這些元素 -

<project> 
<build> 
    <!-- http://maven.apache.org/guides/mini/guide-configuring-plugins.html --> 
    <!-- doesn't seem to be necessary? --> 
<!-- 
    <pluginManagement><plugins><plugin> 
     <groupId>org.apache.maven.plugins</groupId> 
     <artifactId>maven-checkstyle-plugin</artifactId> 
     <version>${checkstyle.plugin.version}</version> 
    </plugin></plugins></pluginManagement> 
--> 

<plugins> 
    <plugin> 
     <groupId>org.apache.maven.plugins</groupId> 
     <artifactId>maven-site-plugin</artifactId> 
     <version>3.0</version> 
     <configuration> 
      <reportPlugins> 
       <plugin> 
        <artifactId>maven-checkstyle-plugin</artifactId> 
        <configuration> 
         <configLocation>checkstyle/mycompany-coding-rules.xml</configLocation> 
         <packageNamesLocation>checkstyle_packages.xml</packageNamesLocation> 
        </configuration> 
       </plugin> 
      </reportPlugins> 
     </configuration> 
    </plugin> 

    <plugin> 
     <groupId>org.apache.maven.plugins</groupId> 
     <artifactId>maven-checkstyle-plugin</artifactId> 
     <version>${checkstyle.plugin.version}</version> 
     <dependencies> 
      <dependency> 
       <groupId>com.mycompany</groupId> 
       <artifactId>buildsupport</artifactId> 
       <version>X.Y.Z</version> 
      </dependency> 
      <dependency> 
       <groupId>checkstyle</groupId> 
       <artifactId>checkstyle</artifactId> 
       <version>5.0</version> 
      </dependency> 
      <dependency> 
       <groupId>com.mycompany.checkstyle</groupId> 
       <artifactId>mycompany-checkstyle</artifactId> 
       <version>1.1</version> 
      </dependency> 
     </dependencies> 
    </plugin> 
</build> 

<!-- http://maven.apache.org/plugins/maven-checkstyle-plugin/examples/multi-module-config.html --> 
    <!-- doesn't seem to be necessary? --> 
<!-- 
<reporting> 
    <plugins> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-checkstyle-plugin</artifactId> 
      <version>${checkstyle.plugin.version}</version> 
      <configuration> 
       <configLocation>checkstyle/mycompany-coding-rules.xml</configLocation> 
       <packageNamesLocation>checkstyle_packages.xml</packageNamesLocation> 
      </configuration> 
     </plugin> 
    </plugins> 
</reporting> 
--> 

的問題是,TeamCity XML Reporting無法解析之前的Checkstyle報告給CHECKSTYLE 5.1,所以沒有這種幫助我!

相關問題