9

At the moment, all my properties are defined in the file src/main/resources/application.properties. However, I would like to have properties files relating to different profiles in the src/main/resources/config folder, and I want to be able to choose any of them. such as:

  • application-DEV.properties
  • application-TEST.properties
  • application-SERVER1.properties

So, the question is how to select these properties. If I was compiling to a jar file, I could do that easily by specifiying the profile when running the jar file, but here I just copy the generated war file to a Tomcat webapps directory.

MetallicPriest
  • 29,191
  • 52
  • 200
  • 356
  • You mean you have all those profile specific properties available int the packaged war and want to get the war started with a specific one? Or you want to have a profile specific property in the in the webapps directory? – lealceldeiro May 02 '19 at 08:33
  • Say, I have all of them present in the src/main/resources/config folder, and I want to choose one of them. – MetallicPriest May 02 '19 at 08:35
  • JAVA_OPTS in Tomcat Config? – Learner May 02 '19 at 08:38
  • Please take a look at https://stackoverflow.com/questions/15814497/setting-spring-profile-variable. The option can be set as JAVA_OPTS like @Learner already said. – Manuel May 02 '19 at 09:56
  • I prefer to do it by modifying the catalina.properties file. Problem with JAVA_OPTS is that you have to do it differently on Windows and Linux. – MetallicPriest May 02 '19 at 10:07

2 Answers2

10

Well, I've found a way to do that. In the conf directory of Tomcat, add this line to the file catalina.properties there.

spring.profiles.active=<YOUR_PROFILE>

Replace <YOUR_PROFILE> here of course with your profile's name. For example if you are using application-TEST.properties, it would be the following.

spring.profiles.active=TEST
MetallicPriest
  • 29,191
  • 52
  • 200
  • 356
  • 1
    How do you customize for multiple war files? say `app1.war` should activate `prod.properties` where as `app2.war` should activate `production.properties`?? – Arun Gowda Sep 30 '20 at 13:26
  • It could also be done this way: set env in `/opt/tomcat/bin/setenv.sh` using `JAVA_OPTS="$JAVA_OPTS -Dspring.profiles.active=test"`. `catalina.sh`, which is the main startup script, looks for `setenv.sh` for additional java options – ProgramSpree Jul 13 '22 at 03:38
0

You can define Jvm Argument -Dspring.profiles.active=<PROFILE> on server start up file (.bat/.sh) depending on your environment.

Milan Desai
  • 1,228
  • 8
  • 23