10

I don't want to be dependable on a external environment variable to force maven to build my classes with UTF-8. On Mac, I was getting all sorts of problems when building with maven. Only the option below solved the problem:

export JAVA_TOOL_OPTIONS=-Dfile.encoding=UTF-8
mvn clean install

However I am distributing my project and it does NOT make sense to rely on the user to set this environment variable to build the project correctly.

Tried everything as described here: enabling UTF-8 encoding for clojure source files

Anyone has a light on that awesome Maven issue?

Community
  • 1
  • 1
chrisapotek
  • 6,007
  • 14
  • 51
  • 85

3 Answers3

35

@Joop Eggen gave the right answer here: https://stackoverflow.com/a/10367745/962872

It is not enough to define that property. You MUST pass it inside the appropriate plugins. It won't go by magic inside there.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>...</version>
    <configuration>
        <source>1.6</source>
        <target>1.6</target>
        <encoding>${project.build.sourceEncoding}</encoding>
    </configuration>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <version>...</version>
    <configuration>
        <encoding>${project.build.sourceEncoding}</encoding>
    </configuration>
</plugin>
...
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
Community
  • 1
  • 1
chrisapotek
  • 6,007
  • 14
  • 51
  • 85
  • Nice and thanks for writing it up. Glad you got an answer to both questions in the end. – andyb Apr 29 '12 at 21:08
  • 2
    The answer is right, but just for information : According to the official Maven's documentation, only adding the `` tag should be OK. Quote : _For plugins **that follow our guideline for source file encoding**, this is as easy as adding the following property to your POM (or one of its parent POMs):..._ Source : http://maven.apache.org/general.html#encoding-warning – mithrop Jun 27 '14 at 09:04
  • 1
    maven-compiler-plugin and maven-resources-plugin documentation says: parameter encoding: Default value is: `${project.build.sourceEncoding}`. So I suppose setting the property is enough nowadays. – leo Oct 12 '15 at 18:45
1

Yes there is, define

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
Alexander Pogrebnyak
  • 44,836
  • 10
  • 105
  • 121
  • This does NOT work on Mac. I tested here many times without success. :( – chrisapotek Apr 29 '12 at 00:28
  • 2
    Than you have other problems. Which version of Maven are you using? And which problems did you get? error messages etc. ? – khmarbaise Apr 29 '12 at 14:40
  • This is the encoding that Maven should use for the source files, it has nothing to do with the encoding that is used at runtime by sure fire or any other plugin than launches a JVM. – zakmck Jun 21 '22 at 14:54
1

I was running into this problem, but only when running the compile from Emacs. I could not change the project's poms. What worked for me was to put the following in ~/.mavenrc

LANG=en_US.UTF-8
dave_nedde
  • 21
  • 2