1

I'm having some trouble trying to pass JVM arguments to a Spring Boot application from build.gradle file.

My build.gradle looks like this:

buildscript ...

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

...
def devConfigFolder = "/abc"
applicationDefaultJvmArgs = ["-DconfigFolder=$devConfigFolder"]
dependencies {
   ...
}

And the class where I try to use the placeholder:

@Component
public class PClass {

   private static final String CONF = "configFolder";
   @Value("${" + CONF+ "}")
   private String configFolder;
}

And this is the exception I'm receiving:

IllegalArgumentException: Could not resolve placeholder 'configFolder' in value "${configFolder}"

I've tried with bootRun{jvmArgs}, but it doesn't work.

B. Bal
  • 121
  • 1
  • 2
  • 11

1 Answers1

0

You can set these values in application.property /yml file and use that property key in @value annotation.

application-prod.properties

configFolder="somefolder/path"

These property files can be set/modified in runtime.

You can take a look at this (5.1) section: https://www.baeldung.com/properties-with-spring Is that fine for you?? If not please let me know.

Anand Varkey Philips
  • 1,811
  • 26
  • 41
  • I know about application.properties/yml file, but I want to do something like this: def devConfigFolder; if (condition) { devConfigFolder = System.getProperty("user.dir") + "/src/config/dev/" } else { devConfigFolder = System.getProperty("user.dir") + "/core/src/config/dev/" } And I can't do a conditional check in a configuration file. – B. Bal Aug 29 '18 at 12:42
  • My problem is that I can't set the JVM args from gradle. – B. Bal Aug 29 '18 at 12:46
  • @Value("${user.dir}" == ""?"/src/config/dev/" : "/core/src/config/dev/") Can you try like this? May I know why you want to set in gradle specifically? – Anand Varkey Philips Aug 29 '18 at 12:52
  • 2
    Because I'm using that placeholder in multiple places and I don't want to modify everywhere. https://stackoverflow.com/questions/25079244/how-to-pass-jvm-options-from-bootrun – B. Bal Aug 29 '18 at 13:06