How to configure Checkstyle in Eclipse for an maven project automatically

15,185

Solution 1

There are detailed answers to this topic in Can I Configure m2eclipse through pom.xml?:

Completely automatic configuration of the Eclipse Checkstyle Plugin can only be achieved with a ProjectConfigurator, e.g. m2e-code-quality or m2e-extensions.

For m2e as of version 1.0, you can install them via the m2e marketplace under Preferences->Maven->Discovery->"Open Catalog". For details see the bug tracking for integration of m2e-code-quality into the m2e marketplace.

There is a also solution based on AntRun and XMLTask even for FindBugs and Sonar. It needs manual triggering only once after checkout.

Solution 2

The maven eclipse plugin is not the same thing as the m2e plugin for eclipse, which I'm assuming you are trying to use. Basically you shouldn't use both at the same time.

You can use mvn eclipse:eclipse from the command line to generate project files and then you can import the projects into eclipse. M2e works very differently and instead uses the pom files when you import projects as maven plugins. Probably what you want is to configure the checkstyle plugin and hopefully eclipse would pick up the settings. Same for findbugs and other maven plugins.

However, I actually prefer using mvn eclipse:eclipse and haven't actually verified the above works as expected. M2e is just too much of a time waster for me and seems to go off and do the wrong things frequently. I particularly hate how it has a habit of basically rebuilding everything after command line interaction with maven. But lots of people seem to like m2e.

Solution 3

One must install the "Checkstyle m2e" connector

applies configuration form the maven-checkstyle-plugin to the eclipse checkstyle Plugin

Under window\preferences\Maven\Discover\Open Catalog

And of course one has to change the pom, so that the check style plugin is configured, instead of the eclipse plugin.

Unfortunately it does not working for me! (I will not delete the answer because it may work for somebody else.)

Share:
15,185
Ralph
Author by

Ralph

Updated on June 13, 2022

Comments

  • Ralph
    Ralph almost 2 years

    I have a maven-multi project, when I create a new eclipse project from it (by M2E 1.0 "Checkout Maven Project from SCM) I want that the eclipse checkstyle plugin is configured for that project automatically.

    So I added the maven-eclipse-plugin in parent pom's <pluginManagement> section and configured it to generate .checkstyle file as well as the additional project nature CheckstyleNature. In the modul poms I added the name of the maven-eclipse-plugin in the build section. But when I checkout the project nothing happend, the file is not generated and the nature is not added.

    So I think I am doing something completely wrong, but how is it done correct?


    Parent pom - plugin management section:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-eclipse-plugin</artifactId>
        <version>2.8</version>
        <configuration>
            <downloadSources>true</downloadSources>
            <downloadJavadocs>false</downloadJavadocs>
            <wtpversion>2.0</wtpversion>
            <additionalBuildcommands>
                <buildCommand>
                    <name>org.eclipse.ajdt.core.ajbuilder</name>
                    <arguments>
                        <aspectPath>org.springframework.aspects</aspectPath>
                    </arguments>
                </buildCommand>
                <buildCommand>
                    <name>org.springframework.ide.eclipse.core.springbuilder</name>
                </buildCommand>
            </additionalBuildcommands>
            <additionalProjectnatures>
                <projectnature>org.eclipse.ajdt.ui.ajnature</projectnature>
                <projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
                <projectnature>com.atlassw.tools.eclipse.checkstyle.CheckstyleNature</projectnature>
            </additionalProjectnatures>
            <additionalConfig>
                <file>
                    <name>.checkstyle</name>
                    <content>
    <![CDATA[
    <?xml version="1.0" encoding="UTF-8"?>
    <fileset-config file-format-version="1.2.0" simple-config="true" sync-formatter="false">
      <fileset name="all" enabled="true" check-config-name="My Checkstyle rules" local="false">
        <file-match-pattern match-pattern="." include-pattern="true"/>
      </fileset>
      <filter name="FilesFromPackage" enabled="true">
        <filter-data value="target" />
        <filter-data value="src/main/resources"/>
        <filter-data value="src/test/java"/>
        <filter-data value="src/test/resources"/>
        <filter-data value="src/main/webapp" />
      </filter>
    </fileset-config>
    ]]>
                    </content>
                </file>
            </additionalConfig>
        </configuration>
    </plugin>
    

    Module pom - plugins section:

    <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-eclipse-plugin</artifactId>
    </plugin>
    
  • Ralph
    Ralph over 12 years
    You are right, the plugins are different. But I will use M2e instead of the eclipse-plugin.
  • Ralph
    Ralph over 12 years
    sounds very good, I have not tested it yet, but fron the projekt description it is what I needed
  • matbrgz
    matbrgz over 11 years
    Is there any way of having this discovery trigger automatically?
  • Ralph
    Ralph over 11 years
    @Thorbjørn Ravn Andersen: I don't know. But it sounds like a good question.
  • oberlies
    oberlies almost 11 years
  • Jean-Rémy Revy
    Jean-Rémy Revy over 8 years
    Unless this answer is relevant explaning difference between the Eclipse plugin (m2e) and the Maven plugin (maven-eclipse-plugin), it seems that integration between the build system and the IDE was really improved from latest version (from 2013 to 2015 especially). I would actually recommend M2E.
  • Jilles van Gurp
    Jilles van Gurp over 8 years
    It was relevant because the original question is about m2e but the sample xml configures the maven eclipse plugin, which clearly won't work; as I pointed out. My last paragraph was relevant when I provided this answer since m2e was rather unstable and tricky to deal with at the time. Since then things have improved substantially and I now use m2e as well.
  • Jean-Rémy Revy
    Jean-Rémy Revy over 8 years
    You're right, and you answer still focuses on giving answer to the original question. I would just add some actual feedback, for one reading this good question few years later, like me indeed. To be clear, I wouldn't harm you in anyway :)
  • Ralph
    Ralph over 7 years
    the m2e-code-quality home page (m2e-code-quality.github.io/m2e-code-quality) seams to be quite old, but its market place page marketplace.eclipse.org/content/m2e-code-quality shows that the project is still alive