You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Gradle属性注入Spring Boot application.yml在IntelliJ IDEA中失效

Hey, I’ve dealt with this exact frustrating mismatch between Gradle command-line and IntelliJ IDEA launches before—let’s get this sorted out.

Why This Happens

When you run gradle bootRun from the command line, the Gradle task automatically exposes project properties (like version) to the Spring Boot application’s environment. But when you start your app directly from IntelliJ IDEA (by clicking the run button on your main class), IDEA doesn’t automatically pass those Gradle project properties into the Spring context by default. That’s why Spring can’t resolve ${version} in your application.yml.

Fixes You Can Try

1. Configure IDEA to Run via Gradle’s bootRun Task

This is the most straightforward fix since it aligns IDEA’s behavior with the command line:

  • Open Run/Debug Configurations (click the dropdown next to the run button, then select "Edit Configurations...")
  • Find your Spring Boot run configuration (the one that’s failing)
  • Under the Build and run using dropdown, select Gradle instead of "IntelliJ IDEA"
  • Save the configuration and restart your app—IDEA will now use the Gradle bootRun task to start your app, so the version placeholder will resolve correctly.

2. Inject the Version via Gradle’s processResources Task

If you want the version to be baked into your application.yml during build (so it works regardless of how you launch the app), add this to your build script:

Groovy DSL (build.gradle)

processResources {
    filesMatching('application.yml') {
        expand(project.properties)
    }
}

Kotlin DSL (build.gradle.kts)

tasks.processResources {
    filesMatching("application.yml") {
        expand(project.properties)
    }
}

This tells Gradle to replace all placeholders in application.yml with the corresponding project properties during the build process. After running gradle build, your compiled application.yml will have the actual version number instead of ${version}, so both IDEA and command-line launches will work.

3. Manually Add the Version to IDEA’s Run Configuration

If you prefer to keep using IDEA’s native run setup, you can manually pass the version as a system property:

  • Open your Spring Boot run configuration
  • Go to the VM options field and add: -Dversion=${project.version}
  • Or, in the Environment variables section, add a variable with key version and value ${project.version}
  • Save and restart your app. Note: This requires updating the value if you change your project version, so it’s less ideal than the first two methods.
Bonus Tip

If you’re using Spring Boot 2.4+, you can also leverage Gradle’s spring-boot plugin to expose project properties directly. Add this to your build script:

Groovy DSL

springBoot {
    properties {
        property("version", project.version)
    }
}

Kotlin DSL

springBoot {
    properties {
        set("version", project.version)
    }
}

This adds the version as a Spring property, which will be available for resolution in application.yml regardless of how you launch the app.


内容的提问来源于stack exchange,提问作者L_G

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:02:42