Bastien Jansen
Bastien Jansen

Reputation: 8846

Encoding issue when building with mvn

I'm currently working on a Maven plugin that uses JAXB. The problem is that whether I launch a clean install in IntelliJ or in a console, I don't get the same results. JAXB is reading an XML file encoded in UTF-8, which contains special characters.

In IntelliJ, these characters are read without any problem. But in the console, these characters are replaced with '?' (I can see it if I do a sysout).

I found the source of my problem, but I don't really understand it and I don't know how to solve it: when IntelliJ runs mvn, it adds an extra parameter -Dfile.encoding=UTF-8

java -classpath /usr/share/maven/boot/plexus-classworlds-2.4.jar -Dclassworlds.conf=/usr/share/maven/bin/m2.conf -Dmaven.home=/usr/share/maven -Dfile.encoding=UTF-8 org.codehaus.plexus.classworlds.launcher.Launcher clean install

When I run mvn in command line, I can add this extra parameter but it will appear after the class name:

java -classpath /usr/share/maven/boot/plexus-classworlds-2.4.jar -Dclassworlds.conf=/usr/share/maven/bin/m2.conf -Dmaven.home=/usr/share/maven org.codehaus.plexus.classworlds.launcher.Launcher -Dfile.encoding=UTF-8 clean install

In both cases, if I sysout the content of System.getProperty("file.encoding"), I get the same value "UTF-8", but a different behaviour.

Of course I configured my pom.xml correctly using a property like this:

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

I also tried to add a second property named "file.encoding" but it does not help.

My question is: why does the position of this system property change the behaviour of my program, and how can I fix my problem when I run mvn from command line?

Thanks in advance.

Upvotes: 3

Views: 6326

Answers (2)

Bastien Jansen
Bastien Jansen

Reputation: 8846

As suggested in other questions, the JVM starts with a default file encoding (usually the system file encoding) which is used every time Readers and Writers are used without an explicit encoding. Overriding the system property file.encoding at runtime does not change this behavior, you really have to specify -Dfile.encoding when starting the JVM if you want it to be the default encoding.

To fix my issue, I reused project.build.sourceEncoding in my Maven plugin (I had this problem in a custom plugin) to override file.encoding at runtime, and use this file.encoding to instantiate InputStreamReader and OutputStreamWriter with an explicit encoding as 2nd parameter.

As resources were correctly copied by the maven-resource-plugin in UTF-8, my problem was gone :D.

Upvotes: 1

Michael
Michael

Reputation: 35351

Maybe try including this XML in your pom.xml file:

<project>
  [...]
  <build>
    [...]
    <plugin>
      <artifactId>maven-resources-plugin</artifactId>
      <version>2.4.3</version>
      <configuration>
        <encoding>UTF-8</encoding>
      </configuration>
    </plugin>

    <plugin>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>2.3.2</version>
      <configuration>
        <source>1.6</source>
        <target>1.6</source>
        <encoding>UTF-8</encoding>
      </configuration>
    </plugin>
  </build>
</project>

Upvotes: 1

Related Questions