Reputation: 942
I have some data in my gradle.properties
file:
appVersion=1.4.31
appGroup=com.business
appName=projectName
I want to be able to access them in a .java
project. I am attempting to access the variable like this:
@Value("${appVersion}")
private int version;
However, when I try to run the application from the main class, I get the following error:
Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'appVersion' in value "${appVersion}"
Which shows that it can't find the correct variable.
@propertySource
AnnotationI have tried adding this to the main class
@PropertySource(value = "file:build.gradle", ignoreResourceNotFound = true)
which was unsuccessful.
build.gradle
I attempted the following, taken from this StackOverflow answer:
processResources {
filesMatching('application.properties'){
expand(gradle.properties)
}
}
Which was also unsuccessful.
I did what was said in this answer (I had to add an application.properties
file) and I got this error message:
Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Circular placeholder reference 'appVersion' in property definitions
I thought that it might be getting mixed up with using the same names (appVersion = appVersion), so I changed the application.properties line to
version=${appVersion}
And then in my .java
file, I changed the line to:
@Value("${version}")
private String version;
And got the error:
Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'appVersion' in value "${appVersion}"
Upvotes: 3
Views: 2391
Reputation: 15330
This should work:
processResources {
filesMatching('application.properties'){
expand(gradle.properties)
}
}
But it seems like you forgot the other part. This just tells Gradle to process your application.properties
file and replace any placeholders it finds. You need to also put a placeholder into your application.properties
:
version=${appVersion}
Then your value annotation should work (but it should be a String
not an int
)
@Value("${version}")
private String version;
Finally, since you are launching via IDEA, you may need to make sure that gradle:processResources
runs before your application starts so that Gradle has a chance to replace the placeholder you put in application.properties
. You should be able to utilize the "Before Launch Options" section in your run configuration to have IDEA run the processResources
Gradle Task after the build task. This should cause the placeholder to be properly replaced every time you run the application.
Also, just to help with the understanding. Gradle is the build system for the application, it's not part of the application itself. Once the project is built, everything related to Gradle is irrelevant. This is why you can't just do @Value("${appVersion}")
and have it work. The application/Spring doesn't know anything about the tool that's being used to build it (and it shouldn't). So you need to somehow, at build time, inject the Gradle project version into the application if you want access to it. There are a number of ways of accomplishing this but the resource processing method above is a pretty common approach.
Upvotes: 2