maven-war-plugin的乱码问题 (3 字节的 UTF-8 序列的字节 3 无效)报错

最近用到maven-war-plugin这个插件来进行打包,但是打完包启动tomcat会遇到这个报错:

  1. org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from file [D:\apache-tomcat-7.0.63_eclipse\webapps\Merchant\WEB-INF\classes\config\applicationContext-db.xml]; nested exception is com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: 3 字节的 UTF-8 序列的字节 3 无效。  
  2.     at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:410)  
  3.     at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:336)  
  4.     at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:304)  
  5.     at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:181)  
  6.     at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:217)  
  7.     at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:188)  
  8.     at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:125)  
  9.     at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:94)  
  10.     at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:129)  
  11.     at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:604)  
  12.     at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:509)  
  13.     at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:446)  
  14.     at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:328)  
  15.     at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)  
  16.     at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5017)  
  17.     at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5531)  
  18.     at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)  
  19.     at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)  
  20.     at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)  
  21.     at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:652)  
  22.     at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:1095)  
  23.     at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1930)  
  24.     at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)  
  25.     at java.util.concurrent.FutureTask.run(FutureTask.java:262)  
  26.     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)  
  27.     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)  
  28.     at java.lang.Thread.run(Thread.java:744)  
  29. Caused by: com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: 3 字节的 UTF-8 序列的字节 3 无效。  
  30.     at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.invalidByte(UTF8Reader.java:687)  
  31.     at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.read(UTF8Reader.java:435)  
  32.     at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.load(XMLEntityScanner.java:1753)  
  33.     at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.scanData(XMLEntityScanner.java:1252)  
  34.     at com.sun.org.apache.xerces.internal.impl.XMLScanner.scanComment(XMLScanner.java:778)  
  35.     at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanComment(XMLDocumentFragmentScannerImpl.java:1038)  
  36.     at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2972)  
  37.     at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:606)  
  38.     at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:117)  
  39.     at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)  
  40.     at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:848)  
  41.     at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777)  
  42.     at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)  
  43.     at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:243)  
  44.     at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:347)  
  45.     at org.springframework.beans.factory.xml.DefaultDocumentLoader.loadDocument(DefaultDocumentLoader.java:76)  
  46.     at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadDocument(XmlBeanDefinitionReader.java:429)  
  47.     at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:391)  
  48.     ... 26 more  

就是报
  1. 3 字节的 UTF-8 序列的字节 3 无效  
错误应该是XML文件编码问题。找到报错xml发现里面有中文注释,的确乱码了。

我确认了一下我XML文件的编码:

  1. <?xml version="1.0" encoding="UTF-8"?>  
UTF-8的编码,idea默认编码也是UTF-8,再确认了一下我pom中的encoding:

  1. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
也配置了UTF-8,但是没起作用啊。

想到可能是maven-war-plugin的问题

google一下,查找了一下源码:

发下了一个巨大无比的坑

if ( context.getWebappStructure().registerFile( sourceId, targetFilename ) )  
    {  
        final File targetFile = new File( context.getWebappDirectory(), targetFilename );  
        try  
        {  
            // fix for MWAR-36, ensures that the parent dir are created first  
            targetFile.getParentFile().mkdirs();  
            // TODO: add encoding support (null mean platform encoding)  
            context.getMavenFileFilter().copyFile( file, targetFile, true, context.getFilterWrappers(), null );  
        }  
        catch ( MavenFilteringException e )  
        {  
            throw new MojoExecutionException( e.getMessage(), e );  
        }  
        // Add the file to the protected list  
        context.getLog().debug( " + " + targetFilename + " has been copied (filtered)." );  
        return true;  
    }  
    else  
    {  
        context.getLog().debug(  
            " - " + targetFilename + " wasn't copied because it has already been packaged (filtered)." );  
        return false;  
    }  
}
看到没有!!
  1. context.getMavenFileFilter().copyFile( file, targetFile, true, context.getFilterWrappers(), null );   
encoding是null,也就是取操作系统默认的编码 我用的win

而win环境默认是GBK,所以会乱码了。。。而且win不像Linux可以随意更改系统默认编码!!!所以用这个插件打包,有中文就坑了。。。。。

目前的解决办法是去掉注释中的中文。

2.尝试在pom.xml加入如下配置:

  1.  <build>  
  2. <resources>  
  3.     <resource>  
  4.         <directory>src/main/resources</directory>  
  5.         <filtering>true</filtering>  
  6.     </resource>  
  7. </resources>  
  8.   
  9. <plugins>  
  10.     <plugin>  
  11.         <groupId>org.apache.maven.plugins</groupId>  
  12.         <artifactId>maven-resources-plugin</artifactId>  
  13.         <configuration>  
  14.             <encoding>UTF-8</encoding>  
  15.         </configuration>  
  16.     </plugin>  
  17.              </plugins>  
  18. </build> 

展开阅读全文

Activiti 3 字节UTF-8 序列字节 3 无效

03-31

org.activiti.bpmn.exceptions.XMLException: 3 字节的 UTF-8 序列的字节 3 无效。rn at org.activiti.bpmn.converter.BpmnXMLConverter.convertToBpmnModel(BpmnXMLConverter.java:277)rn at org.activiti.engine.impl.bpmn.parser.BpmnParse.execute(BpmnParse.java:186)rn at org.activiti.engine.impl.bpmn.deployer.BpmnDeployer.deploy(BpmnDeployer.java:112)rn at org.activiti.engine.impl.persistence.deploy.DeploymentManager.deploy(DeploymentManager.java:50)rn at org.activiti.engine.impl.cmd.DeployCmd.execute(DeployCmd.java:103)rn at org.activiti.engine.impl.cmd.DeployCmd.execute(DeployCmd.java:37)rn at org.activiti.engine.impl.interceptor.CommandInvoker.execute(CommandInvoker.java:24)rn at org.activiti.engine.impl.interceptor.CommandContextInterceptor.execute(CommandContextInterceptor.java:57)rn at org.activiti.engine.impl.interceptor.LogInterceptor.execute(LogInterceptor.java:31)rn at org.activiti.engine.impl.cfg.CommandExecutorImpl.execute(CommandExecutorImpl.java:40)rn at org.activiti.engine.impl.cfg.CommandExecutorImpl.execute(CommandExecutorImpl.java:35)rn at org.activiti.engine.impl.RepositoryServiceImpl.deploy(RepositoryServiceImpl.java:78)rn at org.activiti.engine.impl.repository.DeploymentBuilderImpl.deploy(DeploymentBuilderImpl.java:156)rn at com.liu.demo.activiti.ActivitiTest002.test1(ActivitiTest002.java:32)rn at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)rn at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)rn at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)rn at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)rn at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)rn at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)rn at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)rn at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)rn at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)rn at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)rn at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)rn at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)rn at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)rn at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)rn at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)rn at org.junit.runners.ParentRunner.run(ParentRunner.java:309)rn at org.junit.runner.JUnitCore.run(JUnitCore.java:160)rn at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)rn at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)rn at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)rn at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)rn at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)rn at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)rnCaused by: com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: 3 字节的 UTF-8 序列的字节 3 无效。rn at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.invalidByte(UTF8Reader.java:687)rn at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.read(UTF8Reader.java:435)rn at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.load(XMLEntityScanner.java:1753)rn at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.scanLiteral(XMLEntityScanner.java:1074)rn at com.sun.org.apache.xerces.internal.impl.XMLScanner.scanAttributeValue(XMLScanner.java:996)rn at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanAttribute(XMLNSDocumentScannerImpl.java:438)rn at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:254)rn at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2770)rn at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:606)rn at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:117)rn at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)rn at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:848)rn at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777)rn at com.sun.org.apache.xerces.internal.jaxp.validation.StreamValidatorHelper.validate(StreamValidatorHelper.java:155)rn at com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorImpl.validate(ValidatorImpl.java:116)rn at javax.xml.validation.Validator.validate(Validator.java:124)rn at org.activiti.bpmn.converter.BpmnXMLConverter.validateModel(BpmnXMLConverter.java:211)rn at org.activiti.bpmn.converter.BpmnXMLConverter.convertToBpmnModel(BpmnXMLConverter.java:266)rn ... 39 morern以上是错误,但是我不知道我错在什么地方 论坛

没有更多推荐了,返回首页