maven生成jar包编码问题

要做一个jar文件供外部调用,此jar的源代码中注释为中文,用maven打包后在其它工程中导入后总不能正常显示中文,记录解决方法如下: 

 

在pom.xml中设置默认编码类型为UTF-8:

<project> 
... 
  <properties> 
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
  </properties> 
... 
</project>

  

在pom.xml中设置编码类型:

<plugins> 
... 
  <plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-compiler-plugin</artifactId> 
    <version>2.3.2</version> 
    <configuration> 
      <source>1.7</source> 
      <target>1.7</target> 
      <encoding>${project.build.sourceEncoding}</encoding> 
    </configuration> 
  </plugin> 
... 
  <plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-resources-plugin</artifactId> 
    <version>2.4.3</version> 
    <configuration> 
      <encoding>${project.build.sourceEncoding}</encoding> 
    </configuration> 
  </plugin> 
... 
</plugins>

  

    • 执行mvn,生成好jar包并在其它工程引用此jar及其源码jar,如果还是乱码,可能就是eclipse的配置问题了,确认windows->Preferences->general->Workspace->Text file encoding设置为UTF-8
    • 确认windows->Preferences->general->Content Types->右侧Context Types树,点开Text,选择Java Source File,在下面的Default encoding输入框中输入UTF-8,点Update
posted @ 2017-12-05 16:35  潸然  阅读(5456)  评论(0编辑  收藏  举报