java - 如何阻止 maven-shade-plugin 阻止 opensaml-impl 类型的 java.util.ServiceLoader 初始化

标签 java maven maven-shade-plugin opensaml

使用 OpenSAML 3 时,您必须首先使用以下代码行从 opensaml-saml-impl Artifact 加载组件:

InitializationService.initialize();

uses java.util.ServiceLoader to load any type which implements Initializer .

当我编写测试并使用 mvn integration-test 运行它时,这工作正常,我可以看到一切都已加载:

Assert.assertTrue(
    XMLObjectProviderRegistrySupport
        .getUnmarshallerFactory()
        .getUnmarshallers()
        .size() > 400);

但是,我的项目使用maven-shade-plugin。如果我将代码打包到 uber-jar 中,则上述条件为真:

mvn package
java -jar /path/to/my.jar

在这种情况下,我观​​察到只有 9 个解码器已加载(opensaml-core 中的那些,而不是 opensaml-saml-impl 中的那些。但是,当我观察 mvn package 的输出,我可以看到类型 包含在阴影 jar 中:

[INFO] Including org.opensaml:opensaml-saml-impl:jar:3.2.0 in the shaded jar.
[INFO] Including org.opensaml:opensaml-profile-api:jar:3.2.0 in the shaded jar.
[INFO] Including org.opensaml:opensaml-messaging-api:jar:3.2.0 in the shaded jar.
[INFO] Including org.opensaml:opensaml-saml-api:jar:3.2.0 in the shaded jar.
[INFO] Including org.opensaml:opensaml-xmlsec-api:jar:3.2.0 in the shaded jar.
[INFO] Including org.opensaml:opensaml-soap-api:jar:3.2.0 in the shaded jar.
[INFO] Including org.opensaml:opensaml-storage-api:jar:3.2.0 in the shaded jar.
[INFO] Including org.opensaml:opensaml-security-impl:jar:3.2.0 in the shaded jar.
[INFO] Including org.opensaml:opensaml-security-api:jar:3.2.0 in the shaded jar.

我可以使用以下愚蠢的代码解决这个问题:

private static void initManuallyInsteadOfWithInitializationServiceSoThatMavenShadePluginDoesNotRemoveThem() throws InitializationException {
    new ApacheXMLSecurityInitializer().init();
    new ClientTLSValidationConfiguratonInitializer().init();
    new GlobalAlgorithmRegistryInitializer().init();
    new GlobalParserPoolInitializer().init();
    new GlobalSecurityConfigurationInitializer().init();
    new JavaCryptoValidationInitializer().init();
    new SAMLConfigurationInitializer().init();
    new org.opensaml.core.xml.config.XMLObjectProviderInitializer().init();
    new org.opensaml.xmlsec.config.XMLObjectProviderInitializer().init();
    new XMLObjectProviderInitializer().init();
}

这完全违背了插件系统的意义,但它确实允许我的程序运行。

作为引用,这里是 pom.xml 的相关部分:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>2.3</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <transformers>
                    <transformer
                            implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <manifestEntries>
                            <Main-Class>com.example.Server</Main-Class>
                        </manifestEntries>
                    </transformer>
                    <transformer
                            implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                        <resource>META-INF/services/io.vertx.core.spi.VerticleFactory</resource>
                    </transformer>
                </transformers>
                <artifactSet>
                </artifactSet>
                <outputFile>${project.build.directory}/${project.artifactId}-${project.version}-fat.jar
                </outputFile>
                <filters>
                    <filter>
                        <!-- Fix java.lang.SecurityException: Invalid signature file digest for Manifest main attributes
                             when server starts inside Docker container due to inclusion of OpenSAML and use of
                             uber-jar / maven-shade-plugin. See http://stackoverflow.com/a/6743609 -->
                        <artifact>*:*</artifact>
                        <excludes>
                            <exclude>META-INF/*.SF</exclude>
                            <exclude>META-INF/*.DSA</exclude>
                            <exclude>META-INF/*.RSA</exclude>
                        </excludes>
                    </filter>
                    <filter>
                        <!-- This was one of my attempts to fix the problem.
                             Unfortunately, it doesn't work. -->
                        <artifact>org.opensaml:opensaml-saml-impl</artifact>
                        <includes>
                            <include>**</include>
                        </includes>
                    </filter>
                </filters>
            </configuration>
        </execution>
    </executions>
</plugin>

最佳答案

当您使用带有依赖项的 Maven Shade 插件时,使用 ServiceLoader API,您应该使用 ServicesResourceTransformer ,专用于将文件合并在一起。如果插件是 relocating classes ,它还会正确地重新定位每个服务文件中的类名,这与 AppendingTransformer 不同。

因此,您只需将当前的 AppendingTransformer 替换为

<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>

它将确保您的依赖项的 META-INF/services 下的每个服务文件都被合并,而无需全部声明。

关于java - 如何阻止 maven-shade-plugin 阻止 opensaml-impl 类型的 java.util.ServiceLoader 初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42540485/

相关文章:

java - 如何通过 jar 文件更新使用 DCEVM/Hotswap Agent?

java - 使用命令行参数启动 .jar 文件(但没有控制台窗口)

maven - 根据配置文件更改部署的 Artifact 名称

maven - 使用 maven-shaded-plugin 安装 uber jar

java - maven-shade-plugin 的用途是什么,为什么要重新定位 Java 包?

java - 使用连接池

java - 无法启动 ComponentInfo Activity ,可能是因为 Sqlitedatabase 对象

maven - SEVERE : Context Initialization failed java. io.FileNotFoundException:类路径资源[应用程序]

java - 升级后启动服务器时出现 Resteasy 错误。 "both mapped to the url-pattern [/RESTEASY_HttpServlet30Dispatcher] which is not permitted"

maven - 如何在 Maven 阴影插件中设置 list 类路径?