Search code examples
eclipsemavenm2eclipsecheckstyle

Configure automatically tab character for Eclipse Formatter from Checkstyle config with M2E


I would like to configure entirely and automatically my code formatter regarding my configuration, already externalised. It works almost ... but not entirely : at least tab character is not reflected in Eclipse Formatter.

Configuration

Tools

I'm already using the folowing tools and plugin

Structure

This is a deep multimodule layered project. The main idea is that checkstyle config is externalized (in fmk-qa) and refered as a dependency in fmk-core.

fmk-parent
|-- fmk-core
|   |-- fmk-front
|   |   |-- front-sub1
|   |   |-- front-sub2
|   |   |-- ...
|   |   `-- pom.xml
|   |-- fmk-back
|   |   |-- back-sub1
|   |   |-- back-sub2
|   |   |-- ...
|   |   `-- pom.xml
|   |-- fmk-commons
|   |   `-- pom.xml
|   `-- pom.xml
`-- fmk-qa
    |-- pom.xml
    `-- checkstyle-config.xml

Config

Maven parent configuration

[...]

<build>
    <pluginManagement>
        <plugins>
        <!-- Checkstyle Dependency where configuration is externalized  -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-checkstyle-plugin</artifactId>
                <version>2.17</version>
                <dependencies>
                    <dependency>
                        <groupId>corp.framework</groupId>
                        <artifactId>fmk-qa</artifactId>
                        <version>${project.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
            [...]
        <plugins>
    <pluginManagement>

Core project confiuration

<build>

<plugins>

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-checkstyle-plugin</artifactId>

        <configuration>
            <configLocation>checkstyle-config.xml</configLocation>
        </configuration>

        <!-- Binding to indicate to M2E that it should trigger analysis in Eclipse -->
        <executions>
            <execution>
                <id>validate</id>
                <phase>validate</phase>
                <configuration>
                    <configLocation>checkstyle-config.xml</configLocation>
                    <failsOnError>false</failsOnError>
                    <linkXRef>true</linkXRef>
                </configuration>
                <goals>
                    <goal>check</goal>
                </goals>
            </execution>
        </executions>

        <inherited>true</inherited>
    </plugin>

Checkstyle Config

<module name = "Checker">
[...]
<!-- Checks for whitespace                               -->
<!-- See http://checkstyle.sf.net/config_whitespace.html -->
    <module name="FileTabCharacter">
        <property name="eachLine" value="true"/>
    </module>
    [...]
</module>

What works so far

Eclipse detects automatically a bunch of things and configure them, that's say

  • adds CheckstyleNature to project (thanks to m2e and good maven binding) during import
  • fires checkstyle analysis
  • reports errors directly in Java Editor
  • formats the code when I trigger formatting ... almost like I want

(That also works for PMD and FindBugs).

What don't works

When I try to automatically format the code (CTRL+SHIFT+F), it formats the code, but replace whitespaces by tab character, what is obviously not what I want.

How can I can I tell automatically to eclipse how to achieve that correctly ?

I clearly don't want to let developers configure it alone. Moreover, I'm aware that I can generate manually a Code Formatter based on Checkstyle configuration (How to generate an Eclipse formatter configuration from a checkstyle configuration?). So I hope it can be achieved automatically by the plugin.

All suggestions are welcome, even the "pre setted up workspace distribution", even I'm lookig for a more sophisticated solution. I'm also prepared to see (and that would probably be my own answer) : it's not yet possible, do it on your own and contribute to community ;)


Solution

  • Not out of the box. So, your best option is to submit an enhancement request to m2e-code-quality Plugin to make it generate Eclipse formatter configuration in their M2Eclipse's project configurator when it imports or updates project in Eclipse.