Cra*_*ntz 3 java maven opensaml maven-shade-plugin
使用OpenSAML 3时,必须首先opensaml-saml-impl
使用以下代码行从工件加载组件:
InitializationService.initialize();
Run Code Online (Sandbox Code Playgroud)
这用于java.util.ServiceLoader
加载任何实现的类型Initializer
.
当我编写测试并运行它时mvn integration-test
,这工作正常,我可以看到所有内容都已加载:
Assert.assertTrue(
XMLObjectProviderRegistrySupport
.getUnmarshallerFactory()
.getUnmarshallers()
.size() > 400);
Run Code Online (Sandbox Code Playgroud)
但是,我的项目使用maven-shade-plugin
.如果我将代码打包到超级jar中,则上述条件不正确:
mvn package
java -jar /path/to/my.jar
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我观察到只有9 unmarshallers已加载(这些opensaml-core
,而不是那些在opensaml-saml-impl
然而,当我看的输出.mvn package
我可以看到,类型都包含在阴影罐子:
[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.
Run Code Online (Sandbox Code Playgroud)
我可以使用以下哑代码解决此问题:
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();
}
Run Code Online (Sandbox Code Playgroud)
这完全违背了插件系统的要点,但它确实允许我的程序运行.
供参考,以下是相关位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>
Run Code Online (Sandbox Code Playgroud)
当您使用带有依赖关系的Maven Shade插件时ServiceLoader
,您应该使用ServicesResourceTransformer
专用于将文件合并在一起的.如果插件正在重定位类,它还将正确地重新定位每个服务文件中的类名,不像AppendingTransformer
.
所以,你可以替换当前AppendingTransformer
与
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
Run Code Online (Sandbox Code Playgroud)
它将确保META-INF/services
合并您的依赖项下的每个服务文件,而无需全部声明它们.
归档时间: |
|
查看次数: |
752 次 |
最近记录: |