【问题标题】:How to use older Checkstyle version with Maven build?如何在 Maven 构建中使用旧的 Checkstyle 版本?
【发布时间】:2011-12-12 13:11:12
【问题描述】:

我正在将 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>
...

如果我在 &lt;reporting&gt;&lt;plugins&gt;&lt;plugin&gt;...&lt;build&gt;&lt;pluginManagement&gt;&lt;plugins&gt;&lt;plugin&gt; 的 maven-checkstyle-plugin 中省略了 &lt;version&gt;2.5&lt;/version&gt;,并在根目录中运行 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 中添加&lt;version&gt;2.5&lt;/version&gt; 可以让顶级项目成功,但在第一个子项目中会看到相同的错误:

...
[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 版本。

【问题讨论】:

    标签: maven maven-3 checkstyle


    【解决方案1】:

    答案:父 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 报告,所以这些都对我没有帮助!

    【讨论】:

      猜你喜欢
      • 2012-02-25
      • 2015-08-02
      • 1970-01-01
      • 2018-12-19
      • 1970-01-01
      • 1970-01-01
      • 2016-06-18
      • 2020-10-24
      • 1970-01-01
      相关资源
      最近更新 更多