首页 > 解决方案 > 阴影 jar 无法找到 XML 模式命名空间的 Spring NamespaceHandler [http://www.springframework.org/schema/data/jpa]

问题描述

当我使用 maven-shaded-plugin 将我的所有依赖项(包括 spring-data-jpa.jar)打包在一个 jar 中并放在 WEB-INF/lib 目录下时,我在 Tomcat 中运行的 Web 应用程序中遇到了异常。

如果我将 spring-data-jpa.jar 与阴影 jar 一起直接打包到 WEB-INF/lib 中,问题会消失吗?

NOTE: I will be running the same package as AWS Lambda hence I need to create a shaded jar.

标签: spring-data-jpaclasspathmaven-shade-plugin

解决方案


为了帮助其他人,问题是多个 spring-*.jar 文件META-INF/spring.handlers在运行时相互覆盖maven-shade-plugin

解决<transformers>插件配置中的使用问题。我的最终插件配置如下所示;

    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <configuration>
        <createDependencyReducedPom>false</createDependencyReducedPom>
    </configuration>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <!-- Remove signatures from transitive dependencies and append spring handlers and schemas -->
            <configuration>
                <transformers>
                    <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>
                <filters>
                    <filter>
                        <artifact>*:*</artifact>
                        <excludes>
                            <exclude>META-INF/*.SF</exclude>
                            <exclude>META-INF/*.DSA</exclude>
                            <exclude>META-INF/*.RSA</exclude>
                        </excludes>
                    </filter>
                </filters>
            </configuration>
        </execution>
    </executions>
</plugin>

以上将所有处理程序合并到最终 jar 中的一个文件中。享受 :-)


推荐阅读