30

when you run mvn --version part of the output includes the locale and pratform encoding. For example: Default locale: en_GB, platform encoding: Cp1252

I would like to know where it picks these up from and how they can be set

cdog
  • 513
  • 2
  • 8
  • 15

5 Answers5

40

maven picking these values from Java system properties. Here is how you could set encoding:

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

Or:

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <configuration>
      <encoding>UTF-8</encoding>
    </configuration>
  </plugin>

Or pass parameter to maven command line:

mvn -Dproject.build.sourceEncoding=UTF-8
maximdim
  • 8,041
  • 3
  • 33
  • 48
  • 11
    The best thing is to put such kind of information into the pom or into a company pom NEVER on command line. – khmarbaise Apr 02 '12 at 13:08
  • 1
    It works in a pom.xml, but it doesn't answer the question of a global configuration for mvn --version – pdem May 18 '18 at 15:40
  • 2
    this first answer has 2 faults: it lacks setting project.reporting.outputEncoding, and even with that, it works for one pom only. If you have a serious project, you have to put those settings into POM in every module. The sensible global solution is https://stackoverflow.com/a/9976788/715269. The second answer is even worse. The last one is acceptable for work in Eclipse. So, it is not universal because of inconvenience. – Gangnus Mar 09 '20 at 15:40
  • If you give several answers, you should say in what case we should use which. – Gangnus Mar 09 '20 at 15:45
33

You could set environment information for maven (on a windows system) with

set "MAVEN_OPTS=-Duser.language=fr -Dfile.encoding=UTF-8"
SWoeste
  • 1,137
  • 9
  • 20
  • 2
    @cdog, it would be nice if you mark this post as an answer so others would see it right away. – imy Jun 19 '14 at 11:20
  • +1. Excellent. That is the only way to set the coding for all maven globally. Or we should create POM in every project and put two properties into every one - obvious nonsense. – Gangnus Mar 09 '20 at 15:37
11
set MAVEN_OPTS= -Dfile.encoding="UTF-8"

Actually it won't work, you need to remove the quotes ("") :

set MAVEN_OPTS= -Dfile.encoding=UTF-8
rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156
DrSeb
  • 111
  • 1
  • 6
  • a least on cygwin you have to replace set with export & the value has to be quoted like : `export MAVEN_OPTS=" -Dfile.encoding=UTF-8"` – bernstein Nov 15 '17 at 09:45
3

Best solution is:

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

More: http://www.sonatype.com/people/2009/11/special-character-encoding-properties/

MariuszS
  • 30,646
  • 12
  • 114
  • 155
3

I had the same problem. The only thing that works is to set the appropriate MAVEN_OPTS. So if you use windows you can adapt mvn.bat in %MAVEN_HOME%/bin as follows:

set MAVEN_OPTS=%MAVEN_OPTS% -Dfile.encoding="UTF-8"

Following this mvn -v shows this:

..
Default locale: de_DE, platform encoding: UTF-8
..
user5101998
  • 209
  • 3
  • 9