如何在Maven构建中使用较旧的Checkstyle版本?
问题描述:
我将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版本。
答
答:父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,所以没有这种帮助我!