Sometimes, java files compiling with maven or building war file get encoding error 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 running platform, i.e ISO-8859-1.
This post helps developer to solve maven character encoding issues.
There are multiple ways we can configure encoding in maven projects.
Configure character encoding at 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 advantages with 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 configure below 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 -D option or MAVEN_OPTS environment variable.
maven with -D options used to set environment variables, We can use encoding related properties as described below.
set "MAVEN_OPTS=-Duser.language=fr -Dfile.encoding=UTF-8" set MAVEN_OPTS= -Dfile.encoding=UTF-8
encoding in maven compiler plugin
Compiler plugin is a plugin used to compile java source files, That means if any characters present in source files, you might get encoding issues.
Below is the way we can set character encoding to UTF-8 in 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 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 maven sure fire plugin, So you have to configure the plugin with configuration option - argLine.
This enables sure fire 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>
As we discussed above, Changing character encoding from default to UTF-8 involves global level, plugin or command line level, and also please update maven standard properties like
project.reporting.outputEncoding in different ways.
I hope you liked this post:-) and please share it.