2

I've got a Spring Boot project that builds an executable war and is configured with an application.properties file and a application-dev.properties in src/main/resources.

The first line in the application.properties is spring.profiles.active=dev, which triggers the inclusion of application-dev.properties when running as expected.

My problem is that I can't seem to override these properties with an external properties source (after everything is jarred / warred up). For simple debugging, I've created a file settings.properties in the root of my project, and specified spring.profiles.active=dev,deploy in it hoping to also trigger the "deploy" profile.

However, when I run the following:

java -jar target/project-0.1.0-LOCAL-NA.war --spring.config.location=file:./settings.properties

the deploy profile is never enabled. I thought I had this working with a previous version (running 1.1.6.RELEASE now, coming from 1.0.2.RELEASE) but rolling back didn't help either.

Am I missing something obvious?

Update 1:

So while debugging I discovered that Environment.getActiveProfiles() will only return 1 item, even if there's multiple active profiles (dev & deploy in this case). So, while my reporting was only listing dev, deploy was there also.

I've got things running again by using environment variables rather than properties files for profiles, but I'd like to get everything setup with just the files...

David Welch
  • 1,941
  • 3
  • 26
  • 34
  • Also, I've tried it with `--spring.config.location=settings.properties` as well, with no luck – David Welch Sep 21 '14 at 21:12
  • Is that `settings.properties` file actually in the directory you're running `java` from, or is it in the classpath? – chrylis -cautiouslyoptimistic- Sep 21 '14 at 21:30
  • Yes. It's in the root of the project next to the pom.xml – David Welch Sep 22 '14 at 00:00
  • What happens if you use an absolute filename, either with `file:///` or starting with `\`? – chrylis -cautiouslyoptimistic- Sep 22 '14 at 01:32
  • How about calling the external config `application.properties` and putting it inside a `/config` directory that is in same path as the jar? Then Spring Boot will automatically use that file with higher precedence that the properties files inside the jar – geoand Sep 22 '14 at 05:53
  • No dice on either :( – David Welch Sep 22 '14 at 14:35
  • I can't reproduce the problem that you've described. Both `./config/application.properties` and a file pointed to by `--spring.config.location` override the jar's `application.properties` file. Can you please share a simple project that illustrates the problem? – Andy Wilkinson Sep 23 '14 at 15:27
  • Sorry to waste your time but it turns out it was totally unrelated to properties. Answer posted below for the curious. Thanks for the willingness to help! – David Welch Sep 23 '14 at 21:01

2 Answers2

1

When starting up with a jar file we discovered that you can't use "-D" to inject system variables. You have to use "--".

this worked for us assuming you coded your app to use profiles

    java -jar myapplication.jar
    --spring.profiles.active=override
    --spring.config.location=file:/path/to/file/application-override.properties
branchgabriel
  • 4,241
  • 4
  • 34
  • 48
0

It's embarrassing, but I've got to own up to it: I totally misread the results I was dealing with.

--spring.config.location=settings.properties was indeed working properly. I had a combination of a logging issue & a mismatched dependency version that made things appear as if properties were not being properly populated.

FWIW

It turns out Logback interpolates the following

log.info("Profiles: {}", profileArray)

(where the array is [dev, deploy]) as Profiles: dev -- basically only showing the first element in the array. Adjusting my logging to:

log.info("Profiles: {}", Arrays.toString(profileArray) )

lead me to figure out the other issue quickly.

Community
  • 1
  • 1
David Welch
  • 1,941
  • 3
  • 26
  • 34