如何在 Maven 中配置编码?
当我在多模块 Maven 项目上运行 maven install
时,我总是得到以下输出:
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
因此,我用 google 搜索了一下,但我能找到的只是我必须添加:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
...我的 pom.xml 文件。但它已经存在了(在父 pom.xml 中)。
为 maven-resources-plugin 或 maven-compiler-plugin 配置
也无法解决此问题。
那么问题出在哪里呢?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
好的,我已经找到问题了。
我使用一些报告插件。在
failsafe-maven-plugin 的文档中
我发现,
配置 - 当然 - 默认使用${project.reporting.outputEncoding}
。因此,我将该属性添加为
project
元素的子元素,现在一切都很好:另请参阅如何防止“[警告]使用平台编码(实际上是 CP-1252)复制过滤的资源,即构建依赖于平台!”。
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:See also How do I prevent "[WARNING] Using platform encoding (CP-1252 actually) to copy filtered resources, i.e. build is platform dependent!".
这是对之前的答案的补充,如果有人遇到斯堪的纳维亚字母的问题,并且之前的答案中的解决方案无法解决。
如果Java 源文件包含斯堪的纳维亚字母,则需要由用于编译的Java 正确解释它们(例如,常量中使用的斯堪的纳维亚字母)。
即使文件以 UTF-8 存储并且 Maven 配置为使用 UTF-8,Maven 使用的系统 Java 仍将使用系统默认值(例如,在 Windows 中:Windows-1252)。
仅通过 Maven 运行测试才可见(可能在测试中打印这些常量的值。打印的斯堪的纳维亚字母将显示为“<?>”。)
如果没有正确测试,这会损坏编译结果的类文件并且不会被注意到。
为了防止这种情况,您必须将用于编译的Java设置为使用UTF-8编码。
仅在 Maven pom.xml 文件;您需要设置环境变量:
此外,如果在 Windows 中使用 Eclipse,您可能还需要设置除此之外使用的编码(如果您通过 Eclipse 运行单独的测试)。
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:
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).
如果您结合前面的答案,这里最终是一个 pom.xml,配置为 UTF-8,应该是这样的。
pom.xml
If you combine the previous answers, here is finally a pom.xml, that configured for UTF-8, should seem like that.
pom.xml
人们似乎将内容编码与内置文件/资源编码混合在一起。仅拥有 Maven 属性是不够的。使用
-Dfile.encoding=UTF8
无效。为了避免出现编码问题,您应该遵循以下简单规则:设置 Maven 编码,如上所述:
在代码中处理文件、字符串和 I/O 时,始终显式设置编码。如果不遵循此规则,您的应用程序取决于环境。
-Dfile.encoding=UTF8
正是负责运行时环境配置,但我们不应该依赖它。如果您有数千个客户端,则需要花费更多精力来配置系统并查找问题。您只是对它有额外的依赖,您可以通过显式设置它来避免这种依赖。 Java 中使用默认编码的大多数方法都因此被标记为已弃用。确保您正在使用的内容也采用您期望的相同编码。如果不是,前面的步骤就没有关系了!例如,如果文件的编码不是 UTF-8(但您期望如此),则该文件将无法正确处理。要检查 Linux 上的文件编码:
强制客户端/服务器在请求/响应中显式设置编码。以下是示例:
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:
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:
Force clients/server set encoding explicitly in requests/responses. Here are examples:
试试这个:
Try this:
(截至 2023 年,但实际上一直如此)
如果你使用 Spring Boot,
你不需要做任何事情。
它已经在父级中应用了这些属性。
在一般情况下,上面这两行就足够了。
并且您不应该在任何其他地方或插件中添加某些内容,除非您知道自己在做什么。
如果您看到采取更多措施的建议,很可能它已经过时了。
(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.
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.
就我而言,我使用的是
maven-dependency-plugin
因此为了解决问题,我必须添加以下属性:请参阅 Apache Maven 资源插件/指定字符编码方案
In my case I was using the
maven-dependency-plugin
so in order to resolve the issue I had to add the following property:See Apache Maven Resources Plugin / Specifying a character encoding scheme