1

I'd like to use two different spring profiles proda and prodb using Spring Boot 2.0.0 (setting the profile in the application.properties).

In Spring Boot, you can also set the active profile in application.properties, as shown in the following example:

spring.profiles.active=production

Source: 74.6


For now I'm only trying to get proda to work. I've got three different property files:

application-proda.yml

database:
  conn:
    blablabla: blablaqwer

and

application-prodb.yml

database:
  conn:
    blablabla: albalbrewq

and also

application.properties

spring.profiles.active=proda

When running the application in the IDE, or packaging it as jar with maven, everything works as expected (active profiles [proda] are set, application-proda.yml is loaded). Calling this in (for example a @PostConstruct of) some class:

System.out.println(Arrays.toString(env.getActiveProfiles()));

will result in

[proda]

but when buildung as war with maven, and deploying it to a Tomcat server, the same code will result in

[]

and application-proda.yml is not loaded. That means the application didn't read the application.properties and therefore didn't load the active profile proda for some reason.

But the finished war has all the needed files under WEB-INF\classes\.

application.war/WEB-INF/classes/


I've seen some solutions where you can set -Dspring.profiles.active=proda as a command line parameter, or set the active profiles in the web.xml, but this is not what I need, as I don't have a web.xml and I'd like to use the Spring Boot feature and declare the profiles in the application.properties. It should work just like in the IDE or packaged as a jar with maven.

Community
  • 1
  • 1
Impulse The Fox
  • 2,638
  • 2
  • 27
  • 52
  • IMO you need to set somewhere your custom profile because Spring Boot will try use the default profile if none other is specified. If you do not want to do it by command line or web.xml then you should specify it in code. Check https://stackoverflow.com/questions/31267274/spring-boot-programmatically-setting-profiles – soulcoder Apr 03 '18 at 08:42
  • How did you "build a war with maven" (on a jar project)? – xerx593 Jan 14 '19 at 15:04
  • @xerx593 I just used `war` in the pom.xml – Impulse The Fox Jan 14 '19 at 22:27

0 Answers0