【问题标题】:Springboot app not detecting the application properties file when the application is packaged as jarSpringboot应用程序打包为jar时未检测到应用程序属性文件
【发布时间】:2021-08-09 11:15:32
【问题描述】:

我有一个 Springboot Gradle 项目。该应用程序已在 IDE 中启动并完美运行。但是当我将它打包为 jar 并运行 jar 文件时,应用程序没有检测到application.properties(或application.yml)文件属性。我必须明确添加@PropertySource 才能阅读application.properties。我觉得应用程序打包存在一些问题。这是我的 Gradle 插件。

buildscript {
    repositories {
        mavenLocal()
        maven {
            mavenLocal()
            url <Maven Repo>
        }
    }
    dependencies {
        classpath "io.spring.gradle:dependency-management-plugin:1.0.6.RELEASE"
        classpath "com.github.jengelman.gradle.plugins:shadow:4.0.2"

    }
}

plugins {
    id 'java'
    id 'org.springframework.boot' version '2.4.5'
    id 'io.spring.dependency-management' version '1.0.10.RELEASE'
    id "application"
    id "jacoco"

}
apply plugin: 'io.spring.dependency-management'
apply plugin: "com.github.johnrengelman.shadow"
apply plugin: 'project-report'

关于这个问题的任何线索都有帮助。

【问题讨论】:

  • 你能把你的项目src/main、resources...的结构贴出来吗?
  • 你运行的是哪个 jar 文件,常规的 jarbootJar 还是 shadowJar
  • @RenatoIvancic 添加了应用程序结构。
  • @thokuest 我运行常规 jar
  • 它是否适用于bootJar

标签: java spring-boot gradle jar


【解决方案1】:

我不知道您在 gradle buildscript 中还有 org.springframework.boot 插件时如何设法运行 jar 任务。作为来自 java 插件的spring boot plugin should disable jar 任务。 Stack overflow answer.

如果你已经覆盖了这个并且你想要生成工作 jar,你仍然需要配置这些库将被包含或将它们传递给 java 命令。

因此,基本上,如果您使用 Spring 应用程序,标准方法是使用 bootJar Gradle 任务,该任务将生成 Spring Boot classloader will be able to process 的 jar 文件。正如评论中提到的@dan1st。

查看 Spring Boot 打包的 jar 与普通 jar 的结构不同:

+--- spring-boot-jar-0.0.1-snapshot.jar
     +--- meta-inf
     +--- boot-inf
     |    +--- classes                            # 1 Project Classes
     |    |     +--- com.rivancic.boot
     |    |     |    +--- SpringJarApplication.class
     |    |     |
     |    |     \--- application.properties
     |    |
     |    +--- lib                                # 2 Nested Jar Libraries
     |          +--- attoparser-2.0.5.RELEASE.jar
     |          +--- jackson-annotations-2.11.4.jar
     |          \--- ...
     |
     +--- org.springframework.boot.loader         # 3 Spring Boot Loader Classes
          +--- jarlauncher.class
          +--- launchedurlclassloader.class
          \--- ...

普通罐子:

+--- jar-0.0.1-snapshot.jar
     +--- meta-inf  
     +--- com.rivancic.boot                       # 1 Project Classes
     |    +--- SpringJarApplication.class
     |
     \--- application.properties

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-19
    • 2020-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-15
    相关资源
    最近更新 更多