【问题标题】:Spring DevTools not included in the fat jar packaged with spring-boot-maven-pluginSpring DevTools 不包含在使用 spring-boot-maven-plugin 打包的 fat jar 中
【发布时间】:2019-06-21 05:04:03
【问题描述】:

我正在尝试使用 spring-boot 插件创建一个胖 jar。但它一直给我 NoClassDefFound 异常。

我的 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>

我用

打包
mvn clean package spring-boot:repackage

,我也试过了

mvn clean install spring-boot:repackage

当我稍后尝试运行它时

java -jar myapp.jar

它给了我以下异常

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

我尝试了我在网上找到的所有解决方案,但它不起作用。

【问题讨论】:

  • 也许 devtools 行为不端。能不能先去掉 spring-boot-devtools 依赖,看看是怎么回事?

标签: java spring spring-boot jar


【解决方案1】:

添加了这个:

<excludeDevtools>false</excludeDevtools>

到 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>

默认排除开发工具。

【讨论】:

    猜你喜欢
    • 2020-08-06
    • 2013-01-30
    • 2023-03-20
    • 2021-12-18
    • 2019-12-11
    • 1970-01-01
    • 2015-08-01
    • 1970-01-01
    • 2015-06-10
    相关资源
    最近更新 更多