环境
- 操作系统:win10
- JDK:1.8.0_144
- maven:3.5.0
问题
- 普通的 maven 工程,java 源码注释中有中文,如下:
package com.bolingcavalry.addservice;
/**
* @author wilzhao
* @description 加法服务对应的接口
* @email [email protected]
* @time 2018/10/13 10:07
*/
public interface AddService {
int add(int a, int b);
}
mvn clean compile -Dmaven.test.skip=true
- 虽然提示编译成功,但是有警告编码 GBK 的不可映射字符,如下所示:
[WARNING] /C:/temp/201810/07/customizestarter/addservice/src/main/java/com/bolingcavalry/addservice/AddService.java:
[5,30] 编码GBK的不可映射字符
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.853 s
[INFO] Finished at: 2018-10-13T11:07:16+08:00
[INFO] Final Memory: 13M/162M
[INFO] ------------------------------------------------------------------------
- maven 用的是 win10 的字符集,在 winndows 命令行输入 chcp,返回 936 表示 GBK,如下图:
- 而 JAVA 的字符集是 UTF8,所以只要把 maven 编译时的字符集改为 UTF8 即可;
解决办法
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ addservice ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to C:\temp\201810\07\customizestarter\addservice\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.332 s
[INFO] Finished at: 2018-10-13T11:28:40+08:00
[INFO] Final Memory: 13M/163M
[INFO] ------------------------------------------------------------------------