Jonathan S. Fisher
Jonathan S. Fisher

Reputation: 8807

Maven "checkstyle:checkstyle" always defaults to "sun_checks.xml" with inline checkstyle configuration

Trying to figure out whether I'm doing something wrong or if this is a bug in the maven checkstyle plugin. If I do mvn checkstyle:check I get this:

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

If I do mvn checkstyle:checkstyle notice how it flips to 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

Here's my inline config:

<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>

The report on mvn:site also uses sun_checks.xml What am I doing wrong?

Upvotes: 5

Views: 7890

Answers (2)

Isaac Villanueva
Isaac Villanueva

Reputation: 274

The next fragment of code is from the CheckstyleViolationCheckMojo

@Parameter( property = "checkstyle.config.location", defaultValue = "sun_checks.xml" )
private String configLocation;

This the mojo that gets executed when you execute the command

mvn checkstyle:check

If you look carefully the @Parameter annotation has an an argument name property called "checkstyle.config.location" and the default value is "sun_checks.xml", that value can be overwritten by passing it at command line using -D flag. so if you want to use a different check style configuration just add it when executing the command, as follows:

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

Notice that google checks is already included in checkstyle plugin so it is resolved as a resource and you don't have to add the path unless you have a custom version of it.

You can take a look at all the properties you can pass by checking the Source Code.

https://github.com/apache/maven-plugins/blob/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugins/checkstyle/CheckstyleViolationCheckMojo.java

Upvotes: 2

Tunaki
Tunaki

Reputation: 137064

The checkstyle goal reads the rulesets file from the configured configLocation. By default, this is sun_checks.xml that is included in the plugin:

Specifies the location of the XML configuration to use. [...]

There are 2 predefined rulesets included in Maven Checkstyle Plugin:

  • sun_checks.xml: Sun Checks.
  • google_checks.xml: Google Checks.

Default: sun_checks.xml

Contrary to the check goal, it does not have the checkstyleRules parameters that enables the use of an inline checker configuration, and, as such, overrides the need to specify a location for the rulesets.

I cannot find an enhancement request on the maven-checkstyle-plugin JIRA regarding this so maybe it could be asked for: inline configuration was introduced in MCHECKSTYLE-211 and it apparently only covered the check goal.

This explains your output:

  • with mvn checkstyle:check, the rules used are those defined in the <checkstyleRules> parameter;
  • with mvn checkstyle:checkstyle, no location for the rules are set so it defaults to sun_checks.xml.

Therefore, to use your rules with the check goal (run when used as a reporting goal with mvn site for example), you will need to have a external file, and reference it with:

<configLocation>checkstyle.xml</configLocation>

Upvotes: 6

Related Questions