In a previous post on Eclipse plugins, two excellent plugins were mentioned which can automatically set up your Eclipse project to use the same Checkstyle configuration that is configured in your pom file. However, the only problem with both of these plugins is that they will only be able to find your Checkstyle configuration if it is configured in the build section of the pom. If you just run Checkstlye as a reporting plugin, the plugins won’t pick up the configuration.
A very simple workaround for this is to take advantage of the skip parameter in the Checkstyle configuration. You can add the Checkstyle configuration into your build section of the pom and set the skip parameter to true. This will then prevent Checkstyle from running every time you build your project.
Example code to use in your pom.xml
<properties>
<checkstylePluginVersion>2.6</checkstylePluginVersion>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>${checkstylePluginVersion}</version>
<executions>
<execution>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
<configuration>
<skip>true</skip>
<configLocation>customCheckstyle.xml</configLocation>
</configuration>
<dependencies>
<dependency>
<groupId>com.company.web</groupId>
<artifactId>company-maven-resources</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>${checkstylePluginVersion}</version>
<configuration>
<skip>false</skip>
<configLocation>customCheckstyle.xml</configLocation>
</configuration>
</plugin>
</plugins>
</reporting>
In the above, we are also using a custom artifact as a dependency of the Checkstyle plugin. This artifact is a jar file which contains the customCheckstyle.xml file. We have also got the example code above in a parent pom file, which is in it’s own maven module with a packaging of pom, and artifactId of custom-super-pom. This way, any modules can specify custom-super-pom as their parent. Then when imported into Eclipse, they will automatically have Ckeckstyle configured, which will be referencing the customCheckstyle.xml file. Also, as these modules will inherit the reporting section from the custom-super-pom, they don’t need to have Checkstyle specified in them. This is great, since now all the configuration is in one single pom, making it much easier to maintain and update.
To change the customCheckstyle.xml file, all you would need to do is edit the file within the company-maven-resources artifact, and then install the artifact into your local repository. If you are also using a repository manager like Nexus, then deploy it there also. The following command takes care of it all:
mvn clean deploy
We have used a snapshot version for our super pom and custom maven resources modules so that when these are changed, we can just deploy new versions on top of the older ones. Then any modules that reference these snapshots should pick up the latest version of the artifcats the next time they are built. You can also force the update to happen by passing in -U on the command line when building, if you find that it doesn’t seem to be checking for updates to snapshots automatically.

