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.
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.
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
bootRuntask to start your app, so theversionplaceholder 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
versionand 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.
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

