0

I am following the example provided in the answer: Spring Boot With Maven Shade Plugin - Controllers not mapped (404 Error)

But when loading the application I get:

java.lang.IllegalArgumentException: Cannot instantiate interface org.springframework.context.ApplicationContextInitializer : org.springframework.boot.autoconfigure.SharedMetadataReaderFactoryContextInitializer
    at org.springframework.boot.SpringApplication.createSpringFactoriesInstances(SpringApplication.java:445)
    at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:427)
    at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:420)
    at org.springframework.boot.SpringApplication.<init>(SpringApplication.java:272)
    at org.springframework.boot.SpringApplication.<init>(SpringApplication.java:253)
    at org.springframework.boot.builder.SpringApplicationBuilder.createSpringApplication(SpringApplicationBuilder.java:105)
    at org.springframework.boot.builder.SpringApplicationBuilder.<init>(SpringApplicationBuilder.java:93)
    at br.com.clamed.frontend.loja.ApplicationStartupService$1.call(ApplicationStartupService.java:39)
    at br.com.clamed.frontend.loja.ApplicationStartupService$1.call(ApplicationStartupService.java:36)
    at javafx.graphics/javafx.concurrent.Task$TaskCallable.call(Task.java:1425)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at javafx.graphics/javafx.concurrent.Service.lambda$executeTask$6(Service.java:725)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at javafx.graphics/javafx.concurrent.Service.lambda$executeTask$7(Service.java:724)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.ClassNotFoundException: org.springframework.boot.autoconfigure.SharedMetadataReaderFactoryContextInitializer
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
    at java.base/java.lang.Class.forName0(Native Method)
    at java.base/java.lang.Class.forName(Class.java:398)
    at org.springframework.util.ClassUtils.forName(ClassUtils.java:285)
    at org.springframework.boot.SpringApplication.createSpringFactoriesInstances(SpringApplication.java:438)
    ... 16 common frames omitted

Guess something is missing.

It looks like this:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>${springBootVer}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

...

<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.2.3</version>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${springBootVer}</version>
            </dependency>
        </dependencies>
        <configuration>
            <minimizeJar>true</minimizeJar>
            <createDependencyReducedPom>false</createDependencyReducedPom>
            <keepDependenciesWithProvidedScope>true</keepDependenciesWithProvidedScope>
            <filters>
                <filter>
                    <!-- filter out signature files from signed dependencies, else repackaging fails with security ex -->
                    <artifact>*:*</artifact>
                    <excludes>
                        <exclude>META-INF/*.SF</exclude>
                        <exclude>META-INF/*.DSA</exclude>
                        <exclude>META-INF/*.RSA</exclude>
                    </excludes>
                </filter>
                <filter>
                    <artifact>org.openjfx</artifact>
                    <includes>
                        <include>**</include>
                    </includes>
                </filter>
            </filters>
        </configuration>
        <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.apache.maven.plugins.shade.resource.AppendingTransformer">
                            <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>${mainClass}</mainClass>
                        </transformer>
                    </transformers>
                </configuration>
            </execution>
        </executions>
    </plugin>

Anyone got it working?

A solution to make not-so-fat jars (meaning no unnecessary classes) with traditional spring boot plugin is also welcome.

Thiago Sayão
  • 2,197
  • 3
  • 27
  • 41
  • Why would you like to use maven-shade-plugin in a spring boot project where you have spring boot maven plugin? Does not make sense... – khmarbaise May 13 '20 at 07:46
  • Because of the true option. – Thiago Sayão May 13 '20 at 11:28
  • That will not work with Spring boot.. spring boot has a particular way how a jar is composed also you need to have the starter of spring boot at the correct location which will be remove with maven-shade-plugin or simply is not there. Reducing the size of the resulting jar is simpler by reducing the dependencies which are being packaged (parts which are on provided) or transitively which not needed at all ... – khmarbaise May 13 '20 at 11:34

0 Answers0