Enforcing an encoding for Eclipse in a maven project

In Maven it’s easy to set the encoding for your source code to a defined encoding (e.g. UTF-8) by inserting this part into your pom file:

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

But how to apply this to your Eclipse project?

Until now I committed some of the resources used by Eclipse to the repository to have a consistent behavior for all developers. For the encoding, this is the file “.settings/org.eclipse.core.resources.prefs” containing:

encoding/<project>=UTF-8
eclipse.preferences.version=1

Unfortunately the maven-eclipse-plugin doesn’t provide a property to set this when running “mvn eclipse:eclipse”.
But there is a generic <additionalConfig>…</additionalConfig> that I found a while ago. With that it’s possible to create additional files for Eclipse. To automatically create the “.settings/org.eclipse.core.resources.prefs” containing the encoding defined in the maven pom, you can use this configuration:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.8</version>
<configuration>
<additionalConfig>
<file>
<name>.settings/org.eclipse.core.resources.prefs</name>
<content>
<![CDATA[encoding/<project>=${project.build.sourceEncoding}
eclipse.preferences.version=1
]]>
</content>
</file>
</additionalConfig>
</configuration>
</plugin>

And that even works using parent poms. So you only have to defin e that once for a project that consists of multiple Eclipse projects.

Dieser Beitrag wurde unter Build, config and deploy, Eclipse Universe abgelegt und mit , , , verschlagwortet. Setze ein Lesezeichen auf den Permalink.

Kommentar verfassen

Trage deine Daten unten ein oder klicke ein Icon um dich einzuloggen:

WordPress.com-Logo

Du kommentierst mit Deinem WordPress.com-Konto. Abmelden / Ändern )

Twitter-Bild

Du kommentierst mit Deinem Twitter-Konto. Abmelden / Ändern )

Facebook-Foto

Du kommentierst mit Deinem Facebook-Konto. Abmelden / Ändern )

Verbinde mit %s