试图弄清楚我是否做错了,或者这是maven checkstyle插件中的错误。如果我mvn checkstyle:check
我得到了这个:
jonathanfisher@odin ~/dev/snapjms/snapjms $ mvn checkstyle:check
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building snapjms 1.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-checkstyle-plugin:2.17:check (default-cli) @ snapjms ---
[INFO] There is 1 error reported by Checkstyle 6.11.2 with /Users/jonathanfisher/dev/snapjms/snapjms/target/checkstyle-rules.xml ruleset.
[ERROR] src/main/java/org/xxx/xxx/snapjms/jms/factories/UnsupportedPayloadException.java:[8] (sizes) LineLength: Line is longer than 135 characters (found 144).
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.411 s
[INFO] Finished at: 2016-06-16T15:33:58-05:00
[INFO] Final Memory: 17M/371M
如果我mvn checkstyle:checkstyle
注意到sun_checks.xml
的翻转方式:
jonathanfisher@odin ~/dev/snapjms/snapjms $ mvn checkstyle:checkstyle
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building snapjms 1.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-checkstyle-plugin:2.17:checkstyle (default-cli) @ snapjms ---
[INFO] There are 451 errors reported by Checkstyle 6.11.2 with sun_checks.xml ruleset.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.904 s
[INFO] Finished at: 2016-06-16T15:35:46-05:00
[INFO] Final Memory: 24M/361M
这是我的内联配置:
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.17</version>
<inherited>true</inherited>
<configuration>
<logViolationsToConsole>true</logViolationsToConsole>
<checkstyleRules>
<module name="Checker">
<module name="SuppressionCommentFilter" />
<module name="LineLength">
<property
name="max"
value="135" />
<property
name="ignorePattern"
value="@version|@see" />
</module>
</checkstyleRules>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
</plugin>
</plugins>
</build>
<reporting>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
</plugin>
</reporting>
关于mvn:site
的报告也使用sun_checks.xml
我做错了什么?
答案 0 :(得分:4)
checkstyle
目标从配置的configLocation
中读取规则集文件。默认情况下,这是插件中包含的sun_checks.xml
:
指定要使用的XML配置的位置。 [...]
Maven Checkstyle插件中包含2个预定义的规则集:
sun_checks.xml
:Sun Checks。google_checks.xml
:Google Checks。默认:
sun_checks.xml
与check
目标相反,它没有允许使用内联检查器配置的checkstyleRules
参数,因此,会覆盖为规则集指定位置的需要。
我无法在the maven-checkstyle-plugin
JIRA上找到关于此问题的增强请求,因此可能会要求:MCHECKSTYLE-211中引入了内联配置,但显然只涵盖check
目标。
这解释了你的输出:
mvn checkstyle:check
,使用的规则是<checkstyleRules>
参数中定义的规则; mvn checkstyle:checkstyle
,未设置规则的位置,因此默认为sun_checks.xml
。因此,要将您的规则与check
目标一起使用(例如,当用作mvn site
的报告目标时运行),您需要拥有一个外部文件,并引用它: / p>
<configLocation>checkstyle.xml</configLocation>
答案 1 :(得分:2)
下一段代码来自CheckstyleViolationCheckMojo
@Parameter( property = "checkstyle.config.location", defaultValue = "sun_checks.xml" )
private String configLocation;
这是执行命令时执行的mojo
mvn checkstyle:check
如果仔细观察,@ Parameter注释有一个名为&#34; checkstyle.config.location&#34;的参数名称属性。并且默认值为&#34; sun_checks.xml&#34;,可以通过使用-D标志在命令行传递该值来覆盖该值。因此,如果要使用不同的检查样式配置,只需在执行命令时添加它,如下所示:
mvn checkstyle:check -Dcheckstyle.config.location=[path to my checkstyle file]
mvn checkstyle:check -Dcheckstyle.config.location=google_checks.xml -Dcheckstyle.violationSeverity=warning
mvn checkstyle:check -Dcheckstyle.config.location=~/custom_checks.xml
请注意,Google检查已包含在checkstyle插件中,因此它已作为资源解析,除非您拥有自定义版本,否则不必添加路径。
您可以通过查看源代码来查看可以传递的所有属性。