Wednesday, February 9, 2011

Use Maven to Configure Checkstyle, PMD and FindBugs Consistently in Eclipse

Fail early! That's the best way to minimize development effort. To achieve this, every developer in a project should use the same build configuration as the continous integration server. His local build should use identical settings and should mandatorily execute unit tests and static code analysis. The rulesets for Checkstyle, PMD and FindBugs should automatically be the same under Maven on the command line and under Eclipse. But the problem is: Eclipse, m2eclipse, Maven and the plugins are still on their way towards a seamless integration. This is this first post of a three part article. It describes the overall setup and the official approach using a project configurator. In the second post, I will point out how third party plugins step into the breach, but why they were not my choice.[1] Finally, in post three, I will share my current best practice for the configuration - taking account of the requirements in enterprise software development.[2]

m2eclipse as pillar of development
There are two main approaches for using Maven and Eclipse efficiently: The Eclipse plugins m2eclipse or IAM aim at integrating Maven and Eclipse seamlessly. Alternatively, the Maven Eclipse Plugin provides the goal eclipse:eclipse and generates the Eclipse project configuration from the command line. I prefer the seamless integration due to the main reasons given in table 1. As it seems more widely adopted than IAM, I have chosen m2eclipse.[3] There is a detailed side-by-side comparison of all three alternatives on the Codehaus wiki.[4]


CriterionEclipse plugin
for Maven
Maven plugin
for Eclipse
eclipse:eclipsem2eclipse
Using Maven to build in Eclipse - identical builds in the IDE and on the CI server
Updating Eclipse project automatically on every change of pom.xml
Configuring Eclipse projects automatically during check out
Providing wildcard search and content assist for adding dependencies and their versions
Table 1: Comparison of approaches for using Maven and Eclipse together (only my main criterions)

m2eclipse is still in the incubation phase. Nevertheless, as of version 0.10.0, I consider it as appropriate for enterprise IT usage - even in multi-module projects with EARs and WARs. But for all known bugs and problems, the developers should be provided with workarounds and information. The most obvious topics are spurious, but harmless error messages[5]-[7] and resource filtering in combination with WTP[8],[9].

The official m2eclipse way with project configurators
As of version 0.9.4 m2eclipse introduced the projectConfigurator extension point to support integration between Maven and Eclipse plugins. If an Eclipse plugin is registered as projectConfigurator, it will participate in the project imports, configurations and reconfigurations triggered by m2eclipse.[10],[11] The project configurator reads configurations of one ore more maven plugin from pom.xml and applies this configuration to the Eclipse project. One prominent project configurator integrates Maven and WTP.[12]

Let's take PMD[13] as our example for integration of Maven and Eclipse. PMD uncovers bad programming style using a set of rules. This ruleset is defined in a XML file called ruleset.xml for example. The PMD Eclipse plugin integrates these checks into the IDE. It uses the file .pmd for the Eclipse specific configuration parts.

Project Configurator
Figure 2: PMD Eclipse plugin as project configurator (click to enlarge)

Figure 2 shows how PMD integrates into Maven and Eclipse. A portable build with Maven outside of Eclipse involves the following steps:
  1. The Maven PMD plugin retrieves its configuration from pom.xml. The configuration states that the plugin depends on the build configuration project.
  2. Maven retrieves the build configuration project from the repository and adds it to the classpath. The build configuration project contains the PMD ruleset.
  3. The Maven PMD plugin reads the ruleset from the classpath and analyzes the java project.
By using the build configuration project and the classpath, the ruleset is available for each java project in each build environment. To leverage this central configuration for Eclipse, the PMD Eclipse plugin acts as project configurator:
  1. On checking out or editing pom.xml, m2eclipse initiates the configuration of the java project. It calls every project configurator that had been registered before.
  2. The project configurator of the PMD Eclipse plugin retrieves the configuration of the Maven PMD plugin from pom.xml.
  3. The project configurator adds the PMD project facet and the PMD builder to the project and creates the file .pmd.
  4. m2eclipse retrieves the build configuration project from the repository and adds it to the classpath.
  5. During builds or on user demand, the PMD Eclipse plugin analyzes the java project.
Unfortunately, this optimal approach is still under development. The PMD Eclipse plugin will offer the project configurator[14] not until version 4.2.x, which has not been released yet.[15] For other Eclipse plugins the situation is even worse: The Checkstyle Eclipse plugin introduced the project configurator with version 5.0.0[16], but removed it with version 5.0.2[17]. The main reasons for the hesitant adoption of project configurators are:
  • m2eclipse is evolving much faster than PMD and Checkstyle.
  • PMD and Checkstyle might consider the m2eclipse integration less important than other new features.
Read how third party plugins step into the breach in post two.[1]

References
  1. ^ Ackermann, M. Solutions for Enterprise IT. "m2e-extensions or m2e-code-quality as Project Configurators for Checkstyle, PMD and FindBugs" [cited 2011 Feb 16]
  2. ^ Ackermann, M. Solutions for Enterprise IT. "Configure Eclipse Projects from Maven with AntRun and XMLTask" [cited 2011 Mar 6]
  3. ^ Stack Overflow Internet Services, Inc. Stack Overflow. "Which is the best Maven Eclipse plugin?" [cited 2011 Feb 6]
  4. ^ McWhirter B., Walding B. The Codehaus. "Eclipse Integration, Eclipse Integration" [cited 2011 Feb 6]
  5. ^ Eclipse Foundation, Inc. Eclipse.org. "IllegalArgumentException from IFolder#refreshLocal" [cited 2011 Feb 6]
  6. ^ Eclipse Foundation, Inc. Eclipse.org. "After installing m2eclipse i get error "A handler conflict occured"" [cited 2011 Feb 6]
  7. ^ Sonatype, Inc. Sonatype Forge. "Plug-in 'org.maven.ide.eclipse.refactoring' contributed an invalid Menu Extension" [cited 2011 Feb 6] 
  8. ^ Sonatype, Inc. Sonatype Forge. "Web resources filtering with m2eclipse" [cited 2011 Feb 6] 
  9. ^ Sonatype, Inc. Sonatype Forge. "Resources are always excluded" [cited 2011 Feb 6] 
  10. ^ Sonatype, Inc. Sonatype Forge. "Extensible project configuration framework" [cited 2011 Jan 23]
  11. ^ Sonatype, Inc. Sonatype Forge. "ProjectConfigurator contributor guide" [cited 2011 Jan 23]
  12. ^ Sonatype, Inc. Sonatype Forge. "Integration with WTP" [cited 2011 Jan 23]
  13. ^ Geeknet, Inc. SourceForge. "PMD" [cited 2011 Jan 23]
  14. ^ Geeknet, Inc. SourceForge. "PMD Project Configurator" [cited 2011 Jan 23]
  15. ^ Hayes, P. Sonatype Forge. "Configure Eclipse project with specified additional natures" [cited 2011 Jan 16]
  16. ^ Geeknet, Inc. SourceForge. "eclipse-cs Release Notes Release 5.0.0final" [cited 2011 Jan 23]
  17. ^ Geeknet, Inc. SourceForge. "eclipse-cs Release Notes Release 5.0.2" [cited 2011 Jan 23]

    No comments:

    Post a Comment