Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring DevTools not included in the fat jar packaged with spring-boot-maven-plugin

I am trying to create a fat jar using spring-boot plugin. But it keeps giving me NoClassDefFound exception.

I have the following in my pom:

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpmime</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                    <mainClass>${start-class}</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

I package it using

mvn clean package spring-boot:repackage

,i also tried

mvn clean install spring-boot:repackage

When I try to run it later with

java -jar myapp.jar

It gives me the following exception

Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/boot/devtools/filewatch/FileChangeListener
        at java.lang.Class.getDeclaredMethods0(Native Method)
        at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
        at java.lang.Class.getDeclaredMethod(Unknown Source)
        at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:47)
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
        at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)
Caused by: java.lang.ClassNotFoundException: org.springframework.boot.devtools.filewatch.FileChangeListener
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at org.springframework.boot.loader.LaunchedURLClassLoader.loadClass(LaunchedURLClassLoader.java:93)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 7 more

I tried everything i've found online as a solution and it doesn't work.

like image 732
D.Tomov Avatar asked Jan 28 '19 08:01

D.Tomov


People also ask

How do I add Devtools to spring boot?

You can configure global devtools settings by adding a file named . spring-boot-devtools. properties to your $HOME folder (note that the filename starts with “.”). Any properties added to this file will apply to all Spring Boot applications on your machine that use devtools.

Which configuration is used to include Devtools in the repackaged archive?

Repackaged archives do not contain devtools by default. If you want to use a certain remote devtools feature, you need to disable the excludeDevtools build property to include it. The property is supported with both the Maven and Gradle plugins.

What is Devtools in spring boot What is the use of it?

spring-boot-devtools module includes an embedded LiveReload server that is used to trigger a browser refresh when a resource is changed. For this to happen in the browser we need to install the LiveReload plugin one such implementation is Remote Live Reload for Chrome.


1 Answers

Added this:

<excludeDevtools>false</excludeDevtools>

To the spring-boot-maven-plugin:

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                    <excludeDevtools>false</excludeDevtools>
                    <mainClass>${start-class}</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

Dev tools is excluded by default.

like image 56
D.Tomov Avatar answered Sep 28 '22 09:09

D.Tomov