如何使用Maven版本的旧版Checkstyle版本?

时间:2011-12-12 13:11:12

标签: maven maven-3 checkstyle

我正在将Maven项目与现有的自定义Checkstyle规则集集成,编译为jar并在Nexus存储库中可用。规则集依赖于Checkstyle 5.0,并且与较新版本不兼容。

我无法将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>
...

如果我从<version>2.5</version><reporting><plugins><plugin>...中的maven-checkstyle-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]

在上面的XML中添加<version>2.5</version>允许顶级项目成功,但在第一个子项目中看到相同的错误:

...
[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.0,因为我在使用规则集作为针对Checkstyle 5.5的Eclipse插件时看到了同样的错误 - 它通过回滚Checkstyle插件版本来修复到5.0。

我不明白的是,儿童项目是如何挑选错误的Checkstyle版本的。

1 个答案:

答案 0 :(得分: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 5.1之前解析Checkstyle报告,所以这些都无法帮助我!