【问题标题】:Application.properties not detected by spring boot 2.5.3Spring Boot 2.5.3 未检测到 Application.properties
【发布时间】:2021-10-25 18:51:10
【问题描述】:

我已将我的项目从 gradle 5 迁移到 7.1 和 spring boot 2.5.3。在我构建 jar 之后,我尝试执行相同的操作。早些时候它工作正常。但是现在,它没有检测到 application.properties 文件。我在 Stackoverflow 和其他网站上尝试了很多解决方案,但对我没有帮助。如果有人能指出我的错误,那将非常有帮助。我试图在 Windows 机器上执行。 application.properties 文件存在于与 jar 平行的 config 文件夹中

尝试 1

java -Dloader.path=.,config\ -jar xyz-1.0.0.jar

错误

class path resource [application.properties] cannot be opened because it does not exist

尝试 2

java -Dspring.config.location=classpath:/application.properties -jar xyz-1.0.0.jar

错误:

ConfigDataResourceNotFoundException: Config data resource 'class path resource [application.properties]' via location 'classpath:/application.properties' cannot be found

尝试 3

java -jar xyz-1.0.0.jar -Dspring.config.location=classpath:/application.properties

错误

 FileNotFoundException: class path resource [application.properties] cannot be opened because it does not exist

尝试 4

java -Dspring.config.location=file:c:\Sheljith\tools\config\application.properties  -jar xyz-1.0.0.jar

错误

FileNotFoundException: class path resource [application.properties] cannot be opened because it does not exist

尝试 5

java --spring.config.location=file:c:\Sheljith\tools\config\application.properties  -jar xyz-1.0.0.jar

错误

Unrecognized option: --spring.config.location=file:c:\Sheljith\tools\config\application.properties

尝试 6

java -Dspring.config.name=application -Dspring.config.location=file:///C:/Sheljith/tools/drc-reports-generator/config/ -jar xyz-1.0.0.jar

错误

.FileNotFoundException: class path resource [application.properties] cannot be opened because it does not exist

尝试 7

java -Dspring.config.location=file:///C:/Sheljith/tools/config/application.properties -jar xyz-1.0.0.jar

错误

class path resource [application.properties] cannot be opened because it does not exist

build.gradle

    buildscript {
    repositories {
        mavenCentral()
        mavenLocal()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin")
    }
}
plugins {
    id 'org.springframework.boot' version '2.5.3'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
}
jar.archiveName = "drc-reports-generator.jar"
version = '1.0.0'
sourceCompatibility = '1.8'
jar {
    manifest {
        attributes 'Implementation-Title': 'Gradle Quickstart',
                   'Implementation-Version': version
    }
}



repositories {
    mavenCentral()
    mavenLocal()
}
processResources{
    exclude '*/**'
}

springBoot{
    mainClass = "com.xyz.Application"
}

dependencies {
    implementation group: 'commons-collections', name: 'commons-collections', version: '3.2'
    implementation("org.springframework.boot:spring-boot-starter") 
    implementation 'com.googlecode.json-simple:json-simple:1.1.1'
    testImplementation("org.springframework.boot:spring-boot-starter-test")
    implementation("org.springframework.boot:spring-boot-starter-data-jpa")
    testImplementation group: 'junit', name: 'junit', version: '4.+'
    testImplementation group: 'com.tngtech.java', name: 'junit-dataprovider', version: '1.13.1'
    implementation 'com.itextpdf:itextpdf:5.5.11'
    implementation 'org.springframework:spring-web:4.3.6.RELEASE'
    implementation 'com.microsoft.sqlserver:sqljdbc4:4.0'
    implementation("com.h2database:h2")
    implementation("org.apache.commons:commons-lang3:3.0")
    implementation group: 'com.github.ulisesbocchio', name: 'jasypt-spring-boot-starter', version: '3.0.0'
}

test {
    useJUnitPlatform()
}

【问题讨论】:

  • 您能否分享您在迁移过程中所做的与 pom.xml 相关的更改?
  • 你可以试试 application.yml 文件来存储你的项目配置吗?
  • @vaibhavsahu,我正在使用 gradle,已将我的 build.gradle 添加到问题中
  • @vaibhavsahu 感谢您的快速回复。我们正在关注所有工具的 .properties。我们只能将其更改为最后一个选项。
  • 如果它是并行的(所以在你的 jar 文件旁边)你不需要做任何事情,因为那是 Spring Boot 已经扫描的standard location。不相关但麻烦的来源是您正在混合来自不同版本的 Spring 的 jar。 Spring web 4.x 与 Spring Boot 2.5 不兼容(删除版本以便包含正确的版本)。

标签: java spring spring-boot gradle spring-boot-2


【解决方案1】:

因为 application.properties 文件位于与 jar 文件平行的位置。试试这个:

java -jar xyz-1.0.0.jar --spring.config.location=application.properties

编辑: 你可以试试这个吗:

java -Dspring.config.additional-location=application.properties -jar xyz-1.0.0.jar

将现有的 processResources 更改为以下内容: // 从 jar 中排除所有资源

jar {
    processResources.exclude('*')
}

【讨论】:

  • 实际上 config 文件夹与 jar 平行,并且 config 文件夹包含 application.properties。但是为了测试这一点,我将属性复制到与 jar 相同的文件夹中并尝试过。但同样的结果。 “FileNotFoundException:类路径资源 [application.properties] 无法打开,因为它不存在”
猜你喜欢
  • 2019-11-09
  • 2014-11-30
  • 2016-03-04
  • 2021-02-07
  • 1970-01-01
  • 2014-11-04
  • 1970-01-01
  • 1970-01-01
  • 2017-08-07
相关资源
最近更新 更多