user3564870
user3564870

Reputation: 395

Checkstyle plugin doesn't care what's in external configuration file

I'm trying to use the Maven Checkstyle plugin with an external configuration file. The plugin works with or without specifying an external configuration file, and the checksytle warnings are different depending on if configLocation is set. So it seems like it should be working fine, but it's not. No matter what I enter in the external configuration file, the same warnings appear to be generated. In fact, I can clear out the file completely and nothing appears to change.

In the pom:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-checkstyle-plugin</artifactId>
    <version>3.0.0</version>
    <executions>
        <execution>
            <id>validate</id>
            <phase>validate</phase>
            <configuration>
                <configLocation>google_checks.xml</configLocation>
                <encoding>UTF-8</encoding>
                <consoleOutput>true</consoleOutput>
                <failsOnError>true</failsOnError>
            </configuration>
            <goals>
                <goal>check</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Trying to use this file:

https://github.com/checkstyle/checkstyle/blob/master/src/main/resources/google_checks.xml

In the pom, if I change the name to say "does_not_exist.xml" it will report it cannot find the file with the following message:

Unable to find configuration file at location: does_not_exist.xml

Since it does not print this message with the correct file name, it tells me it can find the file with the correct name (google_checks.xml).

In the file:

<module name="LineLength">
    <property name="max" value="100"/>
    <property name="ignorePattern" value="^package.*|^import.*|a href|href|http://|https://|ftp://"/>
</module>

If I change the value to say 10, I still get the warning:

Line is longer than 100 characters...

I'm running:

mvn clean verify

I've tried putting the google_checks.xml file in the root of the project and in the src/main/resources directory. Same results either location.

I'd ultimately like to change the rules for line length and indentation, but since it doesn't care what I enter in the file, I'm not sure how to do this. Any ideas on how to make it honor what's in the external file?

Upvotes: 5

Views: 1552

Answers (1)

Mark
Mark

Reputation: 5239

Quite a dig but: The configLocation you've set the XML in your question gets injected into CheckstyleViolationCheckMojo#configLocation of the maven-checkstyle-plugin, since this variable is private it did not show up on their Javadoc but it is documented in source code.

This description states that:

This parameter is resolved as resource, URL, then file. If successfully resolved, the contents of the configuration is copied into the ${project.build.directory}/checkstyle-configuration.xml file before being passed to Checkstyle as a configuration.

There are 2 predefined rulesets.

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

What I think is happening is that it's not resolving the parameter the normal way, since google_checks.xml is a predefined ruleset and therefore the default google_checks.xml will be loaded.

If you want to use your own custom configurator you should name it something other than google_checks.xml and sun_checks.xml

Upvotes: 3

Related Questions