避免在单个 jar 中合并多个 spring 依赖项时覆盖 spring.handlers/spring.schemas 的想法 [英] Idea to avoid that spring.handlers/spring.schemas get overwritten when merging multiple spring dependencies in a single jar
问题描述
我收到错误 Unable to locate NamespaceHandler when using context:annotation-config
运行 (java -jar) 一个由 maven-assembly-plugin 组装并包含我的项目及其所有依赖项的 jar.
I got the error Unable to locate NamespaceHandler when using context:annotation-config
running (java -jar) a jar assembled by the maven-assembly-plugin and containing my project and all its dependencies.
正如其他人在 forum.springsource.org 上正确发现的 线程(消息 #7/8) 出现问题是因为文件 META-INF/spring.handlers
和 META-INF/spring.schemas
存在于不同的 jars 中,当 maven-assembly 被覆盖时-plugin 将 jars 重新打包到一个文件中.
As other people correctly spotted on the forum.springsource.org thread (message #7/8) the problem occurs because the files META-INF/spring.handlers
and META-INF/spring.schemas
that are present in different jars, get overwritten when the maven-assembly-plugin repackages the jars in a single file.
查看两个 spring-*.jar 文件的内容,您可以看到这些文件相对于类路径位于相同的位置
Looking at the content of two spring-*.jar files you can see the files sits in the same position relatively to the classpath
$ jar tf spring-oxm-3.0.3.RELEASE.jar
META-INF/spring.handlers
META-INF/spring.schemas
org/springframework/oxm/GenericMarshaller.class
...
$ jar tf spring-context-3.0.3.RELEASE.jar
META-INF/spring.handlers
META-INF/spring.schemas
org/springframework/context/ApplicationContext.class
是不是可以将 META-INF 文件夹放在特定的包中?如果是这样,我建议的想法(希望它适用)是将 META-INF/spring.shemas
和 META-INF/spring.handlers
文件放在他们所指的包.
Isn't it is possible to put the META-INF folder in a specific package? If so the idea I'd suggest, (hope it's applicable) is to put the META-INF/spring.shemas
and META-INF/spring.handlers
files under the package they refer to.
$ jar tf spring-oxm-3.0.3.RELEASE.jar
org/springframework/oxm/META-INF/spring.schemas
org/springframework/oxm/META-INF/spring.handlers
org/springframework/oxm/GenericMarshaller.class
...
$ jar tf spring-context-3.0.3.RELEASE.jar
org/springframework/context/META-INF/spring.handlers
org/springframework/context/META-INF/spring.schemas
org/springframework/context/ApplicationContext.class
这样它们在合并到一个 jar 中时就不会发生冲突.你怎么看?
This way they won't conflict when merged in a single jar. What do you think about it?
推荐答案
我设法使用着色器插件而不是(有问题的)汇编器插件摆脱了错误:
I managed to get rid of the bug using the shader plugin instead of the (buggy) assembler plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>at.seresunit.lecturemanager_connector.App</mainClass>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.handlers</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.schemas</resource>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
我想我在 springsource 论坛上找到了解决方案.. 好久没查了.. 记不清作者了.无论如何都要向他致敬:p
I think I found the solution on the springsource forums.. it has been quite some time since I looked it up.. can't really remember the author. Kudos to him anyways :p
干杯
这篇关于避免在单个 jar 中合并多个 spring 依赖项时覆盖 spring.handlers/spring.schemas 的想法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!