Social Media

Applying Eclipse formatting using Maven

I had an issue recently where a branch had been formatted by eclipse and made a merge from Trunk to Branch reasonably difficult. These things happen – so the solution was to find a plugin that would allow me to format both HEAD and Branch consistently outside of eclipse.

The first step was to export my eclipse preferences from eclipse –

Preferences > Java > Code Style > Formatter

Eclipse Preferences

Eclipse Preferences

The next step was to find a plugin that would apply these changes. I opted for formatter-maven-plugin

Then it was a matter of adding it to both master poms –

<plugin>
     <groupId>net.revelc.code</groupId>
     <artifactId>formatter-maven-plugin</artifactId>
     <version>0.5.2</version>
     <configuration>
        <configFile>${session.executionRootDirectory}\eclipse-formatter.xml</configFile>
     </configuration>
     <dependencies>
        <dependency>
            <groupId>net.revelc.code</groupId>
            <artifactId>formatter-maven-plugin</artifactId>
            <version>0.5.2</version>
        </dependency> 
      </dependencies>
      <executions>
           <execution>
                <goals>
                    <goal>format</goal>
                </goals>
           </execution>
      </executions>
 </plugin>

Then run the plugin on both branch and trunk –

mvn formatter:format

Commit then continue with your merge

About the Author Martin Farrell

My name is Martin Farrell. I have almost 20 years Java experience. I specialize inthe Spring Framework and JEE. I’ve consulted to a range of businesses, and have provide Java and Spring mentoring and training. You can learn more at About or on my consultancy website Glendevon Software

follow me on:

Leave a Comment: