{

How to configure Character Encoding in Maven


Sometimes, java files compiling with maven or building war files get encoding errors as described below.

  • File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
  • maven character encoding source not working

maven read the source code for building and installing artifacts, So It should define the character encoding to read and write files of different encoding types- ASCII, UTF-8, or UTF-16 maven uses default encoding of the running platform, i.e ISO-8859-1.

Character encoding maven

This post helps developers to solve maven character encoding issues.

There are multiple ways we can configure encoding in maven projects.

You can check my other posts on maven commands

Configure character encoding in the maven application

You can define properties project.build.sourceEncoding project.reporting.outputEncoding in pom.xml to have encoding set at application level.

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

The advantage of this approach is, You need not define encoding manually for each plugin that you use.

Once this encoding is defined, all the plugins will reuse this configuration

Change encode to UTF-8 in maven resource plugin

resource plugin encoding can be fixed by configuring the property

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

Or you can also add project.build.sourceEncoding to UTF-8 in property.

Configure Command-line maven options

It is required to run maven goals via command line, So We have to use either the -D option or the MAVEN_OPTS environment variable.

maven with -D options used to set environment variables, We can use encoding-related properties as described below.

mvn -Dproject.build.sourceEncoding=UTF-8

or

set "MAVEN_OPTS=-Duser.language=fr -Dfile.encoding=UTF-8"
set MAVEN_OPTS= -Dfile.encoding=UTF-8

encoding in maven compiler plugin

A compiler plugin is a plugin used to compile java source files, Which means if any characters are present in source files, you might get encoding issues.

Below is the way we can set the character encoding to UTF-8 in the maven compiler plugin

<plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.6.0</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
          <encoding>UTF-8</encoding>
        </configuration>
      </plugin>
</plugins>

How to change encoding for the maven test?

when you are compiling and testing your java classes, You might get encoding issues for a line of code that has UTF-8 characters.

Maven tests are run with the maven surefire plugin, So you have to configure the plugin with the configuration option - argLine.

It enables the surefire plugin to read all files with UTF-8 encoding.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <argLine>-Dfile.encoding=UTF-8</argLine>
    </configuration>
</plugin>

Conclusion

As we discussed above, Changing character encoding from default to UTF-8 involves the global level, plugin, or command-line level, and also please update maven standard properties like Dfile.encoding, project.build.sourceEncoding, or project.reporting.outputEncoding in different ways.

I hope you liked this post:-) and please share it.

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.