How to Configure Maven Character Encoding
In this example we will explain how you can configure Maven Character Encoding. There are various ways you can specify encoding for Maven. Each with there benefits and drawbacks.
The problem
Whenever you see this warning in your console, this means that you have not set any character encoding for your project/environment. Lets solve this problem. There are a couple of options you can consider.
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
Configuring Maven Character Encoding
1. Properties
Most popular and common way to set Maven Character Encoding is to use properties. These properties are supported by most plugins. These properties are easy to add. Just add them as a child element of the project
element.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
[...]
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
[...]
</project>
2. Maven Resources Plugin
You can also specify Maven Character Encoding using the maven resources plugin. The only drawback is that you have to include this plugin to your Maven pom.xml file.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
3. Commandline
If you cannot alter the source code of a maven project, or you need to specify maven character encoding on a built server like Jenkins, Hudson or Bamboo you can also add the encoding through the command line.
mvn -Dproject.build.sourceEncoding=UTF-8 -Dproject.reporting.outputEncoding=UTF-8 clean deploy
4. Maven Options
If you do a lot of small project for personal gain you can also set this property globaly in MAVEN_OPTS
. The only drawback is that if you share your code base with another developer then the developer also has to add these MAVEN_OPTS
. That’s why I do not recommend it.
set MAVEN_OPTS= -Dfile.encoding="UTF-8"