Maven打包后配置文件乱码的问题

把web工程的war包发布到linux服务器上总是报错exception is com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: Invalid byte 3 of 3-byte UTF-8 sequence,具体如下:

[2015-09-02 09:32:52 uq_www] INFO [pool-2-thread-3] XmlBeanDefinitionReader.loadBeanDefinitions(315) | Loading XML bean definitions from class path resource [applicationContext.xml]
[2015-09-02 09:32:53 uq_www] INFO [pool-2-thread-3] XmlBeanDefinitionReader.loadBeanDefinitions(315) | Loading XML bean definitions from class path resource [applicationContext/db-context.xml]
[2015-09-02 09:32:53 uq_www] ERROR [pool-2-thread-3] ContextLoader.initWebApplicationContext(331) | Context initialization failed
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Failed to import bean definitions from relative location [applicationContext/db-context.xml]
Offending resource: class path resource [applicationContext.xml]; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [applicationContext/db-context.xml]; nested exception is com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: Invalid byte 3 of 3-byte UTF-8 sequence.

对比db-context.xml文件的编码方式是UTF-8,文件首行是也不错。然后使用maven build,命令是clean package。生成的war包里,使用notepad++打开db-context.xml总是有部分中文注释乱码,但在eclipse里显示是没有乱码的。想必就是此问题了。于是暂时把问题定位在maven的插件上,根据一些博客的提示陆续把pom.xml做成这样的配置:

<plugin>
    <groupId>org.apache.maven.pluginsgroupId>
    <artifactId>maven-war-pluginartifactId>
    <configuration>
        <warSourceDirectory>WebRootwarSourceDirectory>
        <webResources>
            <resource>
                <directory>${package.environment}directory>
                <targetPath>WEB-INF/classestargetPath>
                <filtering>truefiltering>
            resource>
        webResources>
    configuration>
    <version>2.2version>
plugin>
<plugin>
    <groupId>org.apache.maven.pluginsgroupId>
    <artifactId>maven-compiler-pluginartifactId>
    <version>2.4version>
    <configuration>
        <source>1.6source>
        <target>1.6target>
        <encoding>UTF-8encoding>
    configuration>
plugin>
<plugin>
       <groupId>org.apache.maven.pluginsgroupId>
       <artifactId>maven-resources-pluginartifactId>
       <version>2.6version>
       <configuration>
         <encoding>UTF-8encoding>
       configuration>
  plugin>

还有这样:

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

同样不可以。

那么接下来莫非是我eclipse环境有问题,于是这样:

- 修改Eclipse中文本文件的默认编码:windows -> Preference -> general -> Workspace -> Text file encoding设置为UTF-8
- 修改JAVA源文件的默认编码:windows->Preferences->general->Content Types->右侧Context Types树,点开Text,选择Java Source File,在下面的Default encoding输入框中输入UTF-8,点Update

这样就可以了。后来我又把pom.xml配置改为原来的,即不配置maven-resources-plugin的编码方式,也删除UTF-8此项,然而生成的war包仍是正常的。这样看来其实是eclipse的环境问题了。

你可能感兴趣的:(J2EE,生产环境)