【问题标题】:Can't run my jar after upgrading SpringBoot from 2.4.X to 2.6.X将 Spring Boot 从 2.4.X 升级到 2.6.X 后无法运行我的 jar
【发布时间】:2022-01-22 19:41:41
【问题描述】:

我最近将我的库从 gradle 5.X 升级到 7.3.1,并将 Spring-Boot 从 2.4.X 升级到 2.6.X,但我遇到了问题。我正在使用 gradle build 创建一个 jar 并在我的服务器上执行这个 jar,它在 Spring-Boot 2.4.X 上运行良好,但现在在 2.6.X 上我遇到了一些问题:

没有主要的清单属性

所以我添加到了我的 build.gradle

jar {
   manifest {
       attributes "Main-Class": "my.package.MainClass"
   }

   from {
       configurations.compileClasspath.collect { it.isDirectory() ? it : zipTree(it) }
       configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
   }
}

现在我得到了错误

无法找到或加载主类 my.package.MainClass

如果我切换回 2.4.X,它工作正常。 我的罐子来自:

  • spring-boot 2.4.X -> 90Mo
  • spring-boot 2.6.X -> 500 Ko

所以我尝试用 shadowJar 创建一个胖 jar 并得到这个错误

错误 org.springframework.boot.SpringApplication - 应用程序运行失败 12 月 21 日 10:04:09 vps-11602ed1 项目 [12706]:org.springframework.context.ApplicationContextException:无法启动 Web 服务器;嵌套异常是 org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean。

如果有人得到有关如何使其与 Spring-boot 2.6.X 一起使用的答案。

提前致谢

【问题讨论】:

  • 我试过了: - 我的主类得到了@SpringBootApplication 并且已经从 SpringBootServletInitializer 扩展 - 添加到每个配置文件 spring.main.web-application-type=none - 案例 3 也已经完成 - 我没有使用 tomcat - 我是从 gitlab VM 构建的,所以不需要重新打包或清理 - 尝试添加 spring-boot-starter-parent - spring.profiles.active=default 没有任何效果

标签: java spring-boot gradle jar executable-jar


【解决方案1】:

我发现了问题。当我从 springboot 2.4.X 升级到 2.6.X 时,现在 gradle build 正在用我的休闲 jar 生成一个“普通”的新 jar。我将我的 jar 重命名为相同的名称。所以我没有使用最大的罐子,而是使用最小的罐子。刚刚将其添加到我的 gradle.build 中:

jar {
enabled = false
}

所以现在,它没有创建“普通”的 jar,一切正常

【讨论】:

    猜你喜欢
    • 2022-08-24
    • 1970-01-01
    • 1970-01-01
    • 2021-01-11
    • 2022-01-25
    • 1970-01-01
    • 2018-10-07
    • 1970-01-01
    • 2018-12-20
    相关资源
    最近更新 更多