Checking root resources with Checkstyle and Maven

The Maven plug-in for Checkstyle allows to run Checkstyle on your project’s resources. However, options were specialized for a Maven structure. It means it was designed to work on source, resource and test directories. But nothing is built-in for root resources, like your pom.xml files.

I had submitted an enhancement request on Apache’s JIRA. At the time, I had even though about submitting a patch for it. Other things happened then and I somehow put this issue apart to focus on other priorities. Fortunately, Noémi Balassa posted a workaround yesterday. I tested it this evening and it works great. I though it might be useful to share it here.

Here is the sample configuration I used for the Maven Checkstyle plug-in.

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-checkstyle-plugin</artifactId>
	<version>2.13</version>
	<configuration>
		<consoleOutput>true</consoleOutput>
		<logViolationsToConsole>true</logViolationsToConsole>
		<includeTestSourceDirectory>true</includeTestSourceDirectory>
	</configuration>
			
	<executions>
		<!-- ... -->
					
		<!-- XML Files -->
		<execution>
			<id>check-xml</id>
			<phase>process-sources</phase>
			<goals>
				<goal>check</goal>
			</goals>
			<configuration>
				<configLocation>${build.resources.url}/checkstyle/checkstyle-xml-rules.xml</configLocation>
				<headerLocation>${build.resources.url}/checkstyle/header-xml.txt</headerLocation>

				<includeResources>false</includeResources>
				<includeTestResources>false</includeTestResources>

				<!-- Process the pom.xml file too -->
				<sourceDirectory>${project.basedir}</sourceDirectory>
				<testSourceDirectories />
				<includes>**/*.xml, **/*.xsd</includes>
				<excludes>**/target/**/*, **/*.properties</excludes>
			</configuration>
		</execution>
	</executions>
</plugin>

It goes in the build section of your POM.

As you can see, we consider the project’s directory as a source one. We exclude the Maven’s target directory from the search. Resource and test directories are already searched in the source processing. So, no need to enable something specific for them.

It works well and it is not that much complicated.


About this entry