Environment variables in Spring Boot run configuration aren't available to Gradle during build
Answered
I have a Spring Boot application that I've set up a Spring Boot run config for and passed an environment variable within this run config; however it is not present to Gradle during the build phase of application--it only becomes available the application runs. If I create a Gradle `bootRun` or Application run configuration and pass an environment variable, it is available during the Gradle build phase.
I have a block in my Gradle task that looks like this to verify the var was passed:
gradle.taskGraph.whenReady { taskGraph ->
println(System.getenv('HELLO'))
}
Is this intended behavior of passing an environment variable in the various run configurations?
Please sign in to leave a comment.
For Spring Boot and Application configuration IntelliJ IDEA may not run Gradle at all, so it's expected that the environment set for run is not available for build.
Your Gradle build will see the environment only if you run Gradle configuration from IntelliJ IDEA.
Thanks for the response. If I'm understanding right, it sounds like an intentional design decision to not set the environment variables from a run configuration before the build happens?
Also, you said that Spring Boot and Application configurations should not set the environment before the build, however Application DOES set the environment variable from the run configuration. It would be nice if the Spring Boot config would follow this behavior as well.
Build and Run are separate processes, so it's by design.
Application configuration doesn't run Gradle, so it's not clear how it sets the environment for your Gradle build.
It may also depend on this setting:
This is my current setting. So that means Gradle should be used to build all 3 run configurations I mentioned?
Yes, it also depends on the IDE version and the run configuration type.
Some run configuration were not delegated before, but would be delegated in the most recent update, see https://youtrack.jetbrains.com/issue/IDEA-241233/Implement-running-Spring-Boot-Run-Configuration-via-Gradle-task-when-Delegate-to-Gradle-option-is-enabled.
And here is a known issue with the delegation and environment variables: https://youtrack.jetbrains.com/issue/IDEA-176326/Delegate-IDE-build-run-actions-to-Gradle-does-not-work-properly-with-Environment-Variables.
It looks like the known issue for Gradle you linked is over 6 years old and a related Maven issue is over 4 years old. Are they going to be investigated?
The other link looks promising. Can you confirm that environment variables are part of the run configuration being passed now from the work completed in IDEA-241233?
What are the IDE versions and run configurations that IntelliJ supports passing environment variables to when the build and run is being delegated to Gradle?
It's also important to describe how and where you access these environment variables. Accessing them inside the app while it's running is one thing, accessing them during the build is a completely different story.
I would recommend submitting an issue at https://youtrack.jetbrains.com/newIssue?project=IDEA and attaching a sample project with the exact steps to reproduce it.