Wednesday, December 29, 2010

Configuring checkstyle in eclipse via the maven-eclipse-plugin

I like to keep my eclipse configuration files in a central place, so that I can change them more easily. Currently, I tend to have a build-resources Maven project around. Inside this, amongst other things, lies the checkstyle configuration for use during the Maven build. The build-resources project is then added as a build extension, so that the files are accessibly during the build.

For projects using this way of configuration, it is desirable to have the eventually created Eclipse project to be able to use the same configuration file. The following steps allow me to access the checkstyle configuration file, create a configuration for the Eclipse Checkstyle plugin, and have that set up automatically whenever i call mvn eclipse:eclipse.

Create a template configuration file for the Eclipse Checkstyle Plugin

Head over to the project's configuration in Eclipse. In the "Checkstyle" menu entry, switch to "Local Check Configurations". Here, create a "New" configuration with the following data:


Type:
Remote Configuration
Location: jar:file:///[your local maven repo]/[path-to-build-resources-project]/build-resources-1.1.5.jar!/checkstyle/checkstyle-maven-config.xml

This configuration will access the build-resources project's jar file and select the proper file inside it. It uses Java's built-in jar-Protocol to access this file.

Use this configuration set with your Eclipse project. Configure any inclusions/exclusions or such as you like.

Configure eclipse:eclipse to use your configuration template

Now that we have our template, we must make sure it will be used whenever eclipse:eclipse is called. To do so, first copy the Eclipse Checkstyle configuration file (".checkstyle" in your project directory inside the Eclipse workspace) over into your build-resources project. I saved it to eclipse/checkstyle-config.xml. Now you can tell the maven-eclipse-plugin to copy over that file during the creation by addin this to your global maven parent pom:

<pluginManagement>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-eclipse-plugin</artifactId>
            <version>2.8</version>
            <configuration>
                <additionalConfig>
                    <file>
                        <name>.checkstyle</name>
                        <location>eclipse/checkstyle-config.xml</location>
                    </file>
                </additionalConfig> 
                <additionalProjectnatures>
                    <projectnature>
                        net.sf.eclipsecs.core.CheckstyleNature
                    </projectnature>
                </additionalProjectnatures>       
            </configuration>
        </plugin>
    </plugins>
</pluginManagement>
Since you have added the build-resources project as a build extension, its contents will be on the classpath. Hence, the location "eclipse/checkstyle-config.xml" will be copied to [project base dir]/.checkstyle and picked up by the Checkstyle Plugin. So from now on, every time you release a new version of your build-resources, all eclipse projects will automatically be updates to these changes as soon as the new build-resources version makes it to your local repository (i.e.: on the next run of maven).

All that's left to do is run mvn eclipse:eclipse and refresh/import the Eclipse project.

Note: I have not checked if the m2eclipse plugin will pick up the configuration too. I do not use it much for bigger projects, in my experience it slows down the builds, especially on projects with many modules. The eclipse:eclipse way gives me the best of two worlds: a clean eclipse projects without any Maven magic - and a console in which I can use Maven without eclipse getting confused.