23

In this web application with Spring, I created several application-property files for different deploy environment. They specify different db connection configs.

application-dev.properties
application-qa.properties
application-stg.properties
application-prod.properties

The recommended way according to spring doc is to set spring.profiles.active as JVM option at run time, for example:

-Dspring.profiles.active=prod

However what should I do for deploying the application as war using mvn install. How should I set the spring profile? I am using Eclipse.

EDIT: I set the JVM option under run configuration. It does not seem to get picked up by maven when I deploy it as a war though since I got the following error from tomcat:

Failed to instantiate [javax.sql.DataSource]: Factory method 'dataSource' threw exception
ddd
  • 4,665
  • 14
  • 69
  • 125

2 Answers2

18

Under Run Configuration > go to Environments tab and then add your property values for more please refer image below :Eclipse Environment SetUp

14

Under Run->run configurations, select your maven launch configuration, then select the JRE tab, and type your argument in the VM arguments text area.

hasnae
  • 2,137
  • 17
  • 21
  • I don't see `maven launch configuration` under run configurations though. See attached screenshot. – ddd Aug 22 '16 at 15:27
  • 1
    Under the tab Arguments you can provide the vm options – hasnae Aug 22 '16 at 15:36
  • see new screenshot, I did set jvm option under arguments. It runs fine under eclipse. I only got the problem when I mvn install it and deploy it under tomcat. Apparently the jvm option does not get picked up by maven. – ddd Aug 22 '16 at 15:44
  • you have to set the vm arguments in the launch configuration of tomcat. If you're using eclipse to launch tomcat, in the server configuration click on "open launch configuration", then add your arguments under arguments tab. – hasnae Aug 22 '16 at 15:58
  • that makes sense. can you elaborate on how to set the jvm arguments in the launch config of standalone tomcat though? – ddd Aug 22 '16 at 16:16
  • 1
    If you are using a script to launch tomcat, you have to add your argument to JAVA_OPTS like that (linux syntax):export JAVA_OPTS="$JAVA_OPTS -Dspring.profiles.active=prod" – hasnae Aug 22 '16 at 16:24
  • This may not be practical. Me and my team share the same user account to copy wars on to our Tomcat server. If I set the env variable, it changes it for every one else. I am seeking a way to build different wars based on the parameters we feed to maven when I do `mvn install` – ddd Aug 23 '16 at 14:20
  • You can combine spring profiles and maven profiles, see the answer :http://stackoverflow.com/questions/25420745/how-to-set-spring-active-profiles-with-maven-profiles – hasnae Aug 23 '16 at 17:30