Mario560
Mario560

Reputation: 91

How to get version variable from build.gradle file into java spring boot controller?

I know how to get it if version is defined in application.properties, but how do I get it from build.gradle?

Upvotes: 3

Views: 3846

Answers (1)

nickb
nickb

Reputation: 59699

The general flow is:

  1. Define a property in your application.properties that has placeholders, i.e. gradleVersion=${version}.
  2. Configure Gradle's default task that copies your resource files out to the build directory (called processResources) to filter / expand those properties
  3. Read in the gradleVersion property like any other Spring property

Note that it'll require you to invoke Gradle in order to properly resolve the gradleVersion property (as Gradle is the one putting the value in there). bootRun should already depend on processResources, so if you're using that you should be fine.

Upvotes: 2

Related Questions