2

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>

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.

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'`, 

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

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]`

Can somebody help in this.

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

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

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`
Anshuli
  • 21
  • 1
  • 4
  • Why are you using maven-shade-plugin with SpringBoot ? What is the reason? Why not using spring-boot-maven-plugin ? – khmarbaise Jul 25 '18 at 14:58
  • Your are absolutely correct, I was trying to use maven-apache-plugin only but due to this issue as mentioned in this stackoverflow i could not use it. https://stackoverflow.com/questions/29999671/unable-to-run-repacked-spring-boot-jar-caused-by-unable-to-open-nested-entry – Anshuli Jul 25 '18 at 20:16
  • Sorry have you tried to use spring-boot-maven-plugin and seen a problem? What is the problem you have ? Where are you trying to run the spring boot app? – khmarbaise Jul 25 '18 at 20:34
  • Yes, I tried using spring-boot-maven-plugin, and then while deploying the jar to server I was getting error. Have updated same in question above. – Anshuli Jul 25 '18 at 20:40
  • What kind of server? Have you build a war file? – khmarbaise Jul 25 '18 at 20:43
  • Its a jar file that I have built. and server is spring boot integrated server Apache tomcat. And its Linux machine. – Anshuli Jul 25 '18 at 20:50
  • Also, using this command to start the services of jar `java -jar jarfilename. jar` – Anshuli Jul 25 '18 at 20:59
  • So you are running it from plain command line with Java..so I don't any need to run maven-shade-plugin...Apart from that it's hard to guess without the full code etc. where the real problem is located...Have you turned on checking of checksums in Maven? I would suggest to clean the local cache $HOME/.m2/repository and rebuild with https://maven.apache.org/settings.html#repositories – khmarbaise Jul 25 '18 at 21:13
  • Ok, will try this, but what the problem is, when we upload a jar to our server, jar is unpacked and packed again so that it only takes the new changes in the code instead of uploading complete jar.When we place the complete jar on server without the unpacking, it runs fine with `spring-boot-maven-plugin` as well. Due to this we were forced to use `maven-shade-plugin`, which seemed to work well for us, but then we cannot import any other config in tag apart from spring-boot-starter-parent.As a workaround I am pasting contents of parent pom in my pom as of now, and everything works. – Anshuli Jul 26 '18 at 07:11
  • Why does a server unpack and packed it again this does not make sense...a spring boot app must be uploaded in whole ..otherwise it will not work...so remove this stupid server which is unpacking etc. cause this server is causing more problems/effort etc than it helps... – khmarbaise Jul 26 '18 at 08:01
  • Ok, I understand. We will check how we can handle this problem more gracefully. Thanks for your time :) But still I would have been more happy with spring-boot behaving similarly in importing it in 2 different ways. – Anshuli Jul 26 '18 at 08:25
  • The problem is not spring boot...it sounds as your server which is unpacking / repacking things causing the problem...cause the server seemed to be doing wrong things...and the question is what kind of advantage does this have? Which I don't see ... – khmarbaise Jul 26 '18 at 08:50
  • This is not the server which is unpacking/repacking the jar. While uploading jar from local machine to server we have a script which unpacks the jar takes the changes and replace the changes into already present jar on server. This is in place to reduce network transfer time and quick deployments in case of large jars. – Anshuli Jul 26 '18 at 10:36
  • sorry just remove that stupid script cause it causes issues (obviously) and will not really help..in the end you are not testing/running what you are building....furthermore you have to violate convention over configuration paradigms..which doing things manually what is coming from spring-boot out of the box...which in the end produces more effort than it should be... – khmarbaise Jul 26 '18 at 12:27

2 Answers2

6

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>
Reynaldo
  • 630
  • 1
  • 7
  • 19
1

I encountered this issue with version 2.3 of maven shade plugin when building a 2.6.x Spring boot jar. I removed the explicit version declaration in the maven shade plugin config and the issue was avoided. So likely I was originally using an incompatible maven shade version (2.3)

mancini0
  • 4,285
  • 1
  • 29
  • 31