1.问题描述
使用maven给spring项目打jar包,在执行java -jar lance-spring.jar的时候报错,具体异常信息如下:
Exception in thread "main"
org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException:
Line 1 in XML document from class path resource [applicationContext.xml] is invalid;
nested exception is org.xml.sax.SAXParseException; systemId:
[http://www.springframework.org/schema/beans/spring]
(http://www.springframework.org/schema/beans/spring)-beans.xsd;
lineNumber: 1; columnNumber: 41; s4s-elt-character:
Non-whitespace characters are not allowed in schema
elements other than 'xs:appinfo' and 'xs:documentation'.
Saw 'Juniper Web Filtering'.
2.问题原因
spring-core,spring-beans,spring-tx,spring-context等jar包都分别包含了自己spring.handlers,spring.schemas文件,在打包过程中,假如maven首先打包引用了spring-tx,那么就会生成spring.handlers,spring.schemas文件,当后面打包spring-core,spring-beans,spring-context等其他spring的jar的时候,发现已经有了spring.handlers,spring.schemas这两个文件,于是就不再解析spring-core,spring-beans,spring-context等jar中的handlers和schemas内容,导致生成的spring.handlers,spring.schemas内容如下:
spring.handler和spring.schemas的位置.jpgspring.handler只有TxNamespaceHandler
spring.handler.jpg
spring.schemas只有spring-tx(会保存所有spring的历史版本,实现兼容)
spring.schemas.jpg
3.解决方式
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
//排除*.SF,*.DSA,*.RSA文件文件,否则会报java.lang.SecurityException: Invalid signature... 异常
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
//AppendingTransformer以累加的 方式放入到spring.handlers文件中
<resource>META-INF/spring.handlers</resource>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
//AppendingTransformer以累加的 方式放入到spring.schemas文件中
<resource>META-INF/spring.schemas</resource>
</transformer>
<transformer
// 指定主类 implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.lance.demo.main.MyMain</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
4.重新打包后的结果:
handler-all.jpg schemas-all.jpg然后
java -jar lance-spring.jar执行成功。
网友评论