如何阻止maven-shade-plugin阻止opensaml-impl类型的java.util.ServiceLoader初始化
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了如何阻止maven-shade-plugin阻止opensaml-impl类型的java.util.ServiceLoader初始化,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3998字,纯文字阅读大概需要6分钟。
内容图文
![如何阻止maven-shade-plugin阻止opensaml-impl类型的java.util.ServiceLoader初始化](/upload/InfoBanner/zyjiaocheng/735/64ab69da4fa34cb893487426b470e0c2.jpg)
使用OpenSAML 3时,必须首先使用以下代码行从opensaml-saml-impl工件加载组件:
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.如果我将代码打包到超级jar中,则上述条件不正确:
mvn package
java -jar /path/to/my.jar
在这种情况下,我观??察到只有9个unmarshallers已经加载(那些在opensaml-core中,而不是那些在opensaml-saml-impl中.但是,当我观察mvn包的输出时,我可以看到类型包含在阴影罐:
[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 https://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>
解决方法:
当您使用ServiceLoader API将Maven Shade插件与依赖项一起使用时,您应该使用专用于将文件合并在一起的ServicesResourceTransformer.如果插件是relocating classes,它还将正确地重新定位每个服务文件中的类名,与AppendingTransformer不同.
因此,您只需将当前的AppendingTransformer替换为
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
它将确保合并您的依赖项的META-INF /服务下的每个服务文件,而无需全部声明它们.
内容总结
以上是互联网集市为您收集整理的如何阻止maven-shade-plugin阻止opensaml-impl类型的java.util.ServiceLoader初始化全部内容,希望文章能够帮你解决如何阻止maven-shade-plugin阻止opensaml-impl类型的java.util.ServiceLoader初始化所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。