mvn Checkstyle使用

Usage

The following examples describe the basic usage of the Checkstyle Plugin.

Generate Checkstyle Report As Part of the Project Reports

To generate the Checkstyle report as part of the Project Reports, add the Checkstyle Plugin in the <reporting> section of your pom.xml.

   
   
  1. <project>
  2. ...
  3. <reporting>
  4. <plugins>
  5. <plugin>
  6. <groupId>org.apache.maven.plugins</groupId>
  7. <artifactId>maven-checkstyle-plugin</artifactId>
  8. <version>2.17</version>
  9. <reportSets>
  10. <reportSet>
  11. <reports>
  12. <report>checkstyle</report>
  13. </reports>
  14. </reportSet>
  15. </reportSets>
  16. </plugin>
  17. </plugins>
  18. </reporting>
  19. ...
  20. </project>

Then, execute the site phase to generate the report.

   
   
  1. mvn site

Generate Checkstyle Report As Standalone

You can also generate the Checkstyle report by explicitly executing the checkstyle:checkstyle goal from the command line. You are not required to specify the Checkstyle Plugin in your pom.xml unless you want to use a specific configuration.

   
   
  1. mvn checkstyle:checkstyle

Checking for Violations as Part of the Build

If you want to report to the console or fail the build, you must add an execution of checkstyle::check to the <build> element and configure any options that you need.

(Note in that for Maven 3, as per Maven 3 Compatibility Notes, in Maven 3, options you set in the <reporting> element do not have any effect on executions in the <build> element.)

Note that the phase that checkstyle::check is bound to is very important. If bound to the validate phase, it would check the code prior to compiling the code. If the code is invalid, the parsing errors reported by checkstyle may be different than what would be expected from the javac compiler. However, it's guaranteed to run. Another popular option is to bind it to the verify phase which would run much later (and allow the javac compiler to flag invalid code prior to checkstyle). However, if developers generally just use "mvn test" prior to pushing changes, checkstyle would not run as verify occurs after the test phase.

For example:

   
   
  1. <plugin>
  2. <groupId>org.apache.maven.plugins</groupId>
  3. <artifactId>maven-checkstyle-plugin</artifactId>
  4. <version>2.17</version>
  5. <executions>
  6. <execution>
  7. <id>validate</id>
  8. <phase>validate</phase>
  9. <configuration>
  10. <configLocation>checkstyle.xml</configLocation>
  11. <encoding>UTF-8</encoding>
  12. <consoleOutput>true</consoleOutput>
  13. <failsOnError>true</failsOnError>
  14. <linkXRef>false</linkXRef>
  15. </configuration>
  16. <goals>
  17. <goal>check</goal>
  18. </goals>
  19. </execution>
  20. </executions>
  21. </plugin>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值