Maven learning summary (ten) - the unmappable problem of using Maven to compile the project gbk


Maven learning summary (ten) - the unmappable problem of using Maven to compile the project gbk


1. Problem description

  When using Maven to compile the project source code in MyEclipse today, the result is as follows:

  

  I can't figure it out, the java source code is displayed in MyEclipse without any error, but the above error will occur when the "maven install" command is executed to compile the project, resulting in the project compilation failure. This is the first time I have encountered this problem. Fortunately, I saw a solution to the problem here http://capslk.iteye.com/blog/1419958.

Second, the solution

  Ideas to solve this problem: Declare the correct character set encoding in the maven compilation plugin - the character set encoding used for compilation is consistent with the character set encoding used by the code file! !

  After installing the system, the default character set of the general Chinese system is GBK. The software we install generally inherits the default character set of the operating system. Therefore, when developing in Chinese XP or win7 system, when compiling the project with maven (mvn compile), there will be "unmappable characters encoding gbk": This is because the code uses UTF-8, and maven uses it when compiling for the sake of GBK. By modifying the project's pom file, you can tell maven that the project is compiled using UTF-8 encoding. Add the following configuration to the project's pom.xml file :

1 <!-- 指明编译源代码时使用的字符编码,maven编译的时候默认使用的GBK编码, 通过project.build.sourceEncoding属性设置字符编码,告诉maven这个项目使用UTF-8来编译 -->
2     <properties>
3         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
4     </properties>

Or add the following configuration to the compilation plugin declaration  under /project/build/plugins/ in pom.xml :

1 <encoding>utf8</encoding>

which is:

copy code
 1 <plugin>
 2     <artifactId>maven-compiler-plugin</artifactId>
 3     <configuration>
 4         <source>1.6</source>
 5         <target>1.6</target>
 6         <!-- 指明编译源代码时使用的字符编码,
 7         maven编译的时候默认使用的GBK编码, 
 8           通过encoding属性设置字符编码,
 9           告诉maven这个项目使用UTF-8来编译 -->
10         <encoding>utf8</encoding> 
11     </configuration>
12 </plugin>
copy code

  Both of these two solutions can solve the above problems and can be set according to personal habits. As shown in the figure below, the configuration <project.build.sourceEncoding> property is used to specify the character encoding used for compilation.

  

  I have been tossing about this problem for a long time today. Fortunately, I found a solution in time. I will record the solution here.

Reprinted in: https://my.oschina.net/zhanghaiyang/blog/593035

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324088933&siteId=291194637