Posts Tagged ‘plugins’

Maven pom Checkstyle configuration with Eclipse

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.

Useful Eclipse Plugins

A list of handy plugins for the Eclipse IDE

  • eCobertura – Allows Cobertura code coverage to be run from the IDE directly
  • eclipse-cs – Enforces coding standards using the CheckStyle tool
  • PMD – Scans Java code and reports potential problems
  • M2Eclipse – Maven integration from Sonatype
    • m2e-extensions – Works with version 0.10 of the M2Eclipse plugin. Can automatically setup Checkstyle and PMD configuration for Eclipse to match their configuration from the pom file if they are in it
    • m2e-code-quality – Same as m2e-extensions, except this one works with version 0.12 of the M2Eclipse plugin

Note: both m2e-extensions and m2e-code-quality only work if you have the pmd and checkstyle plugins configured in the build section of the pom. They do not currently work if you have pmd and checkstyle configured in the reporting section of the pom.

Maven site generation error: DTDDVFactoryImpl does not extend from DTDDVFactory

When generating a site with Maven, we encountered the following exception when a particular reporting plugin was executed:

org.apache.xerces.impl.dv.DVFactoryException:
DTD factory class org.apache.xerces.impl.dv.dtd.DTDDVFactoryImpl does not extend from DTDDVFactory.

This basically meant that an incompatible version of xerces was trying to be run. We have everything set to use Java 1.6, which ships with its own version of xerces. After some debugging, it was tracked down to the maven-site-plugin which we had recently upgraded to use version 2.1 of the plugin by specifying it in our pom file like so:

<pluginManagement>
	<plugins>
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-site-plugin</artifactId>
			<version>2.1</version>
		</plugin>
	</plugins>
</pluginManagement>

The maven-site-site plugin has the following dependency structure:

  • org.apache.maven.plugins:maven-site-plugin:maven-plugin:2.1
    • org.apache.maven.doxia:doxia-module-xhtml:jar:1.1.2 (compile)
      • org.apache.maven.doxia:doxia-core:jar:1.1.2 (compile)
        • xerces:xercesImpl:jar:2.8.1 (compile)

It is the dependency of the xercesImpl jar 2.8.1 that is the problem. To get around this issue, you can tell the maven-site-plugin to exclude a particular dependency. This can be done like so:

<pluginManagement>
	<plugins>
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-site-plugin</artifactId>
			<version>2.1</version>
			<dependencies>
				<dependency>
					<groupId>org.apache.maven.doxia</groupId>
					<artifactId>doxia-core</artifactId>
					<version>1.1.2</version>
					<exclusions>
						<exclusion>
							<groupId>xerces</groupId>
							<artifactId>xercesImpl</artifactId>
						</exclusion>
					</exclusions>
				</dependency>
			</dependencies>
		</plugin>
	</plugins>
</pluginManagement>

There are a couple of maven reporting plugins that we found produced this exception when run with maven-site-plugin 2.1. These plugins are:

If you use an older version of the maven-site-plugin then the above mentioned reporting plugins should work ok. It is only when specifying a version of 2.1 or above that the exception occurs.

Some of the solutions suggested on the net didn’t work for us, like adding the xercesImpl jar as a dependency to the actual reporting plugin in question, so hopefully the solution mentioned in this post is helpful.