Reputation: 16574
When I run maven install
on my multi-module Maven project I always get the following output:
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
So, I googled around a bit, but all I could find was that I have to add:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
...to my pom.xml file. But it's already there (in the parent pom.xml).
Configuring <encoding>
for the maven-resources-plugin or the maven-compiler-plugin also doesn't fix it.
So what's the problem?
Upvotes: 534
Views: 390948
Reputation: 11088
must be this:
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.3.1</version>
<configuration>
...
<propertiesEncoding>UTF-8</propertiesEncoding>
...
</configuration>
</plugin>
</plugins>
...
</build>
...
</project>
Upvotes: 0
Reputation: 64030
(As of 2023, but actually has always been so)
If you use Spring Boot, you don't need to do anything.
It already applies such properties in the parent.
<properties>
...
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF8</project.reporting.outputEncoding>
...
</properties>
And in the general case, these two lines above are enough.
And you should not add something in any other places or plugins, unless you know what you are doing.
If you see advice to do more, most likely it is something outdated.
Upvotes: 3
Reputation: 1689
If your operanin system is Windows, you used other recomendation and didn't success, then go to Windows Settings > Time & language > Language & region > Administrative language settings > Change system locale, and check Beta: Use Unicode UTF-8 for worldwide language support. Then reboot the PC for the change to take effect.
Upvotes: 0
Reputation: 9515
It seems people mix a content encoding with a built files/resources encoding. Having only Maven properties is not enough. Having -Dfile.encoding=UTF8
is not effective. To avoid having issues with encoding, you should follow the following simple rules:
Set Maven encoding, as described above:
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
Always set encoding explicitly, when work with files, strings, and I/O in your code. If you do not follow this rule, your application depends on the environment. The -Dfile.encoding=UTF8
exactly is responsible for run-time environment configuration, but we should not depend on it. If you have thousands of clients, it takes more effort to configure systems and to find issues because of it. You just have an additional dependency on it which you can avoid by setting it explicitly. Most methods in Java that use a default encoding are marked as deprecated because of it.
Make sure the content, you are working with, also is in the same encoding that you expect. If it is not, the previous steps do not matter! For instance, a file will not be processed correctly, if its encoding is not UTF-8, but you expect it. To check file encoding on Linux:
file --mime F_PRDAUFT.dsv
Force clients/server set encoding explicitly in requests/responses. Here are examples:
@Produces("application/json; charset=UTF-8")
@Consumes("application/json; charset=UTF-8")
Upvotes: 10
Reputation: 16574
OK, I have found the problem.
I use some reporting plugins. In the documentation of the failsafe-maven-plugin
I found, that the <encoding>
configuration - of course - uses ${project.reporting.outputEncoding}
by default.
So I added the property as a child element of the project
element and everything is fine now:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
Upvotes: 772
Reputation: 1640
This would be in addition to the previous answer, if someone meets a problem with Scandinavian letters that isn't solved with the solution in the previous answer.
If the Java source files contain Scandinavian letters, they need to be interpreted correctly by the Java used for compiling (e.g., Scandinavian letters used in constants).
Even that the files are stored in UTF-8 and the Maven is configured to use UTF-8, the system Java used by the Maven will still use the system default (e.g., in Windows: Windows-1252).
This will be visible only running the tests via Maven (possibly printing the values of these constants in tests. The printed Scandinavian letters would show as '< ?>'.) If not tested properly, this would corrupt the class files as the compile result and be left unnoticed.
To prevent this, you have to set the Java used for compiling to use UTF-8 encoding. It is not enough to have the encoding settings in the Maven pom.xml file; you need to set the environment variable:
JAVA_TOOL_OPTIONS = -Dfile.encoding=UTF8
Also, if using Eclipse in Windows, you may need to set the encoding used in addition to this (if you run individual tests via Eclipse).
Upvotes: 66
Reputation: 3495
If you combine the previous answers, here is finally a pom.xml, that configured for UTF-8, should seem like that.
pom.xml
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>YOUR_COMPANY</groupId>
<artifactId>YOUR_APP</artifactId>
<version>1.0.0-SNAPSHOT</version>
<properties>
<project.java.version>1.8</project.java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<dependencies>
<!-- Your dependencies -->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>${project.java.version}</source>
<target>${project.java.version}</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
Upvotes: 58
Reputation: 23590
In my case I was using the maven-dependency-plugin
so in order to resolve the issue I had to add the following property:
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
See Apache Maven Resources Plugin / Specifying a character encoding scheme
Upvotes: 2
Reputation: 727
Try this:
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<configuration>
...
<encoding>UTF-8</encoding>
...
</configuration>
</plugin>
</plugins>
...
</build>
...
</project>
Upvotes: 8