2

I wanted to change the spring.config.additional-location for my gradle springboot app to run local. There's a properties file in my C:/demo_class_path and it's outside the jar. I was trying to access those properties in code. the command java -jar demo-application.jar spring.config.additional-location=file:C:/demo_class_path to run the jar wtth arguments works and I'll be able get the resource I need. But I was trying to add the arguement in bootRun task and it wasn't successful.

I tried the code below :

bootRun {
    systemProperties = [
        'spring.config.additional-location' : "file:C:/demo_class_path",
        'server.port' : 8090
    ]
}

or

bootRun {
    jvmArgs = [
        "-Dspring.config.additional-location=file:C:/demo_class_path/",
        "-Dserver.port=8090"
    ]
}

With the code above I will be able to change the port to 8090, but my files can't be picken up from the path anymore. I also tried to add spring.config.additional-location=file:C:/demo_class _path to application.properties and that didn't work either. I was wondering if the syntax for the location is wrong. In that case, why would the java command work?

Hannah Han
  • 79
  • 1
  • 8
  • `jvmArgs` are, as the name implies, arguments for the JVM used for running the application. They aren't application arguments. Use `args` instead. – M. Deinum Jan 16 '19 at 18:40
  • Update: with `@Value("${spring.config.additional-location}")` I can actually get this path by all implementations above or with `args` argument in bootRun, it's just this seems not recognized in springboot as a classpath as the java command `java -jar demo-application.jar spring.config.additional-location=file:C:/demo_class_path/` did. – Hannah Han Jan 16 '19 at 19:25
  • `spring.config.location` isn't the same as `spring.config.additional-location`... Also why do you keep referring to it as class path... The `spring.config.additional-location` has nothing to do with a class path.. It is just another path that Spring Boot takes into consideration for detecting property files. – M. Deinum Jan 16 '19 at 19:26
  • Got it. I misunderstood. I thought it's detecting the config by adding it into the classpath. This explains why my code can't find the file since I was trying to access it by looking underclasspath. I can now access the properties file by `@PropertySource("file:${spring.config.additional-location}demo.properties")` Thanks. – Hannah Han Jan 16 '19 at 19:36
  • One last question. Why would `java -jar demo-application.jar spring.config.additional-location=file:C:/demo_class_path/` add this path to classpath? I was able to get it as under classpath with this command. – Hannah Han Jan 16 '19 at 19:37
  • Probably because that is actually wrong... Parameters have to start with `--` so I suspect that this is interpreted differently. – M. Deinum Jan 16 '19 at 19:38

1 Answers1

4

The systemProperties are used to pass properties you normally pass with -D to the runtime.

The jvmArgs is for passing arguments to the JVM.

What you want to use is args instead of either one of the above.

bootRun {
  args = [
    '--spring.config.additional-location=file:C:/demo_class_path/',
    '--server.port=8090'
    ]
}

It is important to include the / at the end for the spring.config.additional-location. When it doesn't end with a / it is interpreted as the base-name of a file instead of a file location.

Community
  • 1
  • 1
M. Deinum
  • 115,695
  • 22
  • 220
  • 224
  • Unfortunately that doesn't work either, this time even the port change doesn't work. Any thoughts on how to debug? – Hannah Han Jan 16 '19 at 19:08
  • you might need to add `--` in front of each arguments. They are passed as is. – M. Deinum Jan 16 '19 at 19:09
  • With `--` the port change is taking effect but the config location still doesn't. I've already added the last slash though – Hannah Han Jan 16 '19 at 19:13
  • Which Spring Boot version are you using and what isn't working? I assume that that directory contains an `application.properties`... – M. Deinum Jan 16 '19 at 19:22
  • Update: with @Value("${spring.config.additional-location}") I can actually get this path by all implementations above or with args argument in bootRun, it's just this seems not recognized in springboot as a classpath as the java command java -jar demo-application.jar spring.config.additional-location=file:C:/demo_class_path/ did. – Hannah Han Jan 16 '19 at 19:26
  • 2.1.1 Release. The directory C:/demo_class_path has a demo.property instead of application.property, and I was trying to access it by `ClassPathResource classPathResource = new ClassPathResource("demo.properties");` and read as input stream. – Hannah Han Jan 16 '19 at 19:29
  • Of course that won't work... The `spring.config.additional-locations` isn't part of the class path. So whatever you will pass it simply won't work. Why are you loading it manually instead of letting Spring Boot load it for you in the first place? Add `spring.config.name=application,demo` and Spring Boot will load it for you. – M. Deinum Jan 16 '19 at 19:30
  • Thanks! does this has to be passed in in command or enviroment variables? – Hannah Han Jan 16 '19 at 20:16