如何使用“PropertiesMergingResourceTransformer"将 SpringBoot 配置添加为 <dependencyManagement> 时使用 maven-shade-plugin标签 [英] How to use "PropertiesMergingResourceTransformer" of maven-shade-plugin when adding SpringBoot configuration as <dependencyManagement> tag

查看:232
本文介绍了如何使用“PropertiesMergingResourceTransformer"将 SpringBoot 配置添加为 <dependencyManagement> 时使用 maven-shade-plugin标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 maven-shade-plugin 和 Sprint Boot.我已将 spring-boot 依赖项定义为:

I am using maven-shade-plugin along with Sprint Boot. I have defined spring-boot dependencies as:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.0.2.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

我已经在我的 pom.xml 的 <parent> 标签中定义了我的项目特定的 uber-pom,因此无法使用 spring-boot-starter-parent 标签中.

I already have my project specific uber-pom defined in <parent> tag of my pom.xml so not able to use spring-boot-starter-parent in <parent> tag.

现在,当我执行 mvn clean install 时,出现以下异常:

Now, when I do mvn clean install, I get below exception :

`[ERROR] Failed to execute goal org.apache.maven.plugins:maven-shade-plugin:3.1.1:shade (default) on project myapp: Unable to parse configuration of mojo org.apache.maven.plugins:maven-shade-plugin:3.1.1:shade for parameter transformers: Cannot load implementation hint 'org.springframework.boot.maven.PropertiesMergingResourceTransformer'`, 

明确表示找不到 PropertiesMergingResourceTransformer 的实现.如果我使用 <parent> 标签中定义的 Spring-boot 配置,它工作正常.

which clearly says implementation of PropertiesMergingResourceTransformer cannot be found. If I use Spring-boot configuration as defined in <parent> tag, it works fine.

但是,如果我在 maven-shade-plugin 配置中完全删除 PropertiesMergingResourceTransformer 配置,则无法执行捆绑的 jar,并给出如下异常:

However, if I completely remove PropertiesMergingResourceTransformer configuration in maven-shade-plugin configuration, then bundled jar cannot be executed, and give exception as below :

`java.lang.IllegalArgumentException: No auto configuration classes found in META-INF/spring.factories. If you are using a custom packaging, make sure that file is correct.
    at org.springframework.util.Assert.notEmpty(Assert.java:450) ~[myapp-1.0-SNAPSHOT.jar:na]
    at org.springframework.boot.autoconfigure.AutoConfigurationImportSelector.getCandidateConfigurations(AutoConfigurationImportSelector.java:160) ~[myapp-1.0-SNAPSHOT.jar:na]
    at org.springframework.boot.autoconfigure.AutoConfigurationImportSelector.selectImports(AutoConfigurationImportSelector.java:96) ~[myapp-1.0-SNAPSHOT.jar:na]`

有人可以帮忙吗.

供参考:这是已解决的 maven-shade-plugin 问题:https://github.com/spring-projects/spring-boot/issues/11200

For reference : This is the maven-shade-plugin issue which was resolved : https://github.com/spring-projects/spring-boot/issues/11200

但它似乎只有在 标签中配置 spring-boot 依赖项而不是在

But it seems to work only if spring-boot dependencies are configured in <parent> tag and not in <dependencyManagement>

在尝试使用 spring-boot-maven-plugin 时,我在运行 jar 时遇到以下异常:

While trying to use spring-boot-maven-plugin I get below exception while running the jar:

`Exception in thread "main" java.lang.IllegalStateException: Failed to get nested archive for entry BOOT-INF/lib/spring-boot-2.0.2.RELEASE.jar
    at org.springframework.boot.loader.archive.JarFileArchive.getNestedArchive(JarFileArchive.java:108)
    at org.springframework.boot.loader.archive.JarFileArchive.getNestedArchives(JarFileArchive.java:86)
    at org.springframework.boot.loader.ExecutableArchiveLauncher.getClassPathArchives(ExecutableArchiveLauncher.java:70)
    at org.springframework.boot.loader.Launcher.launch(Launcher.java:49)
    at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)
Caused by: java.io.IOException: Unable to open nested jar file 'BOOT-INF/lib/spring-boot-2.0.2.RELEASE.jar'
    at org.springframework.boot.loader.jar.JarFile.getNestedJarFile(JarFile.java:254)
    at org.springframework.boot.loader.jar.JarFile.getNestedJarFile(JarFile.java:239)
    at org.springframework.boot.loader.archive.JarFileArchive.getNestedArchive(JarFileArchive.java:103)
    ... 4 more
Caused by: java.lang.IllegalStateException: Unable to open nested entry 'BOOT-INF/lib/spring-boot-2.0.2.RELEASE.jar'. It has been compressed and nested jar files must be stored without compression. Please check the mechanism used to create your executable jar file
    at org.springframework.boot.loader.jar.JarFile.createJarFileFromFileEntry(JarFile.java:282)
    at org.springframework.boot.loader.jar.JarFile.createJarFileFromEntry(JarFile.java:262)
    at org.springframework.boot.loader.jar.JarFile.getNestedJarFile(JarFile.java:250)
    ... 6 more`

推荐答案

希望你现在已经解决了这个问题,但是在我用同样的问题敲了几个小时后,我意识到包含 spring-boot-maven-plugin 作为 maven-shade-plugin 的依赖项是必需的:

Hopefully, you have solved this issue by now, but after banging my head for a few hours with the same problem, I realised the inclusion of spring-boot-maven-plugin as a dependency of maven-shade-plugin is required:

<groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>${maven-shade-plugin.version}</version>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <version>${spring-boot.version}</version>
                </dependency>
            </dependencies>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals><goal>shade</goal></goals>
                    <configuration>
                        <transformers>
                            <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                <resource>META-INF/spring.handlers</resource>
                            </transformer>
                            <transformer
                                    implementation="org.springframework.boot.maven.PropertiesMergingResourceTransformer">
                                <resource>META-INF/spring.factories</resource>
                            </transformer>
                            <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                <resource>META-INF/spring.schemas</resource>
                            </transformer>
                            <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
                            <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>${start-class}</mainClass>
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>

这篇关于如何使用“PropertiesMergingResourceTransformer"将 SpringBoot 配置添加为 &lt;dependencyManagement&gt; 时使用 maven-shade-plugin标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆