Pages

Thursday, April 7, 2011

Netbeans using checkstyle configuration for formatter

What is cool about Eclipse - is the the huge variety of plugins that are available to improve it functionality. What is cool about Netbeans - is the awesome default set of plugins that is available right after the short installation procedure. For example maven support - it is out there in Netbeans by default, and after copying Eclipse (installation is the simplest possible in this case) in the right folder you should wait a rather long time for installation of m2eclipse and all other plugins that are absolutely necessary for work. This includes even SVN, mercurial and other version control systems support. Except CVSm that is rather outdated and I don't know who else except legacy systems and some weird guys (look at the date!) use it.

Ok, back to maven support. The really cool thing that I have just discovered in Netbeans is that even without installing SQE plugin (as far as I know the best plugin for checkstyle support in Netbeans, and probably for FindBugs and PMD too, but still not so cool as the checkstyle plugin for Eclipse) checkstyle configuration maybe partially used by default.

So, how it works. Looking in the formatter preferences for a maven project I found there the following:
Use Checkstyle Rules For Java Formatting option.
The following Checkstyle rules are recognized and used when the maven-checkstyle-plugin is configured in the project:
I have added links to descriptions of these checks on the official checkstyle site. Another cool thing is the button Generate POM Section. It will create the following piece of default configuration for maven-checkstyle-plugin in the pom.xml of the project
<reporting>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-checkstyle-plugin</artifactId>
        <configuration>
          <configLocation>config/sun_checks.xml</configLocation>
        </configuration>
      </plugin>
    </plugins>
</reporting>

Not so cool thing here is that activating this option is possible only for a single project and not for all projects... It will actually create (if it wasn't created already) the nb-configuration.xml file with this string:
<netbeans.checkstyle.format>true</netbeans.checkstyle.format>
So the formatter preferences are specific to the project and in case you decide to from now and then put a newline before the starting curly brace of the method body you should update tens of your projects... Not very convenient.

Another thing that may cause problems is that with transition to maven3 (and actually this maven version is bundled with the Netbeans 6.9.1) the config for maven-checkstyle-plugin should be placed in build section of the pom.xml, but not in the reporting section, since starting from maven3 configs of reporting do not affect the build process.
But nonetheless the direction of NetBeans is really cool.

No comments:

Post a Comment