0

I have a Spring Boot Web Application which I need to deploy to Tomcat using Jenkins.

I have setup the pipeline and the deployment happens just fine.

But, I have two profiles in my application. One for development and the other for production. Some properties like the DB URLs etc are different for each of these profiles.

So, I have two property files inside src/main/resources as follows:

application-dev.properties
application-prod.properties

Also, I have another application.properties in the same directory which has only one property (that of the active profile)

spring.profiles.active=dev

Now when I run this through Eclipse, things work pretty fine. Even when Jenkins deploys the WAR to tomcat, everything is good, except for one issue. I am unable to change the value of spring.profiles.active to prod before the deployment to Tomcat happens.

Currently, my SVN has the application.properties file committed with spring.profiles.active = prod

When someone checks out the code and are working in their local environments, they change it to "dev" and continue working, and they just don't commit this change to the SVN. This is temporary workaround I follow, and I feel this can be done in a more efficient way.

How do I have the same WAR file and just change the profile without having to modify my application.properties file? Can this be done in Jenkins? Or should I look at someplace else??

Thanks, Sriram Sridharan

Sriram Sridharan
  • 720
  • 18
  • 43

2 Answers2

2

The first step is to remove the hardcoded profile name from the property file and then you can control what profile to activate via environment variable by setting SPRING_PROFILES_ACTIVE=dev. Its same as setting spring.profiles.active=dev in property file.

Yogesh Badke
  • 4,249
  • 2
  • 15
  • 23
  • Thanks. This solution works when I try in eclipse by altering the Run configurations. So how do I do this in Tomcat which lies in a remote server? Note that the remote server can have more than one Tomcat instances running which may be required to serve different environments. In that case, I cannot set an environment variable. Can I set the property for a specific tomat instance? – Sriram Sridharan Feb 04 '19 at 19:12
  • 1
    If you have more that one tomcat and do not want to use environment variable, only option I see is via setting profiles using `JAVA_OPTS` in `catalina.sh/startup.sh` such as `-Dspring.profiles.active=dev`. – Yogesh Badke Feb 05 '19 at 04:31
  • Thanks, that worked when I was using the startup.sh / startup.bat to start Tomcat. HOwever, when I start Tomcat as a service (on Windows), it does not seem to work. Is there any other file I have to update if I want to start tomcat as a service? – Sriram Sridharan Feb 05 '19 at 06:59
  • Could you check this post? https://stackoverflow.com/a/47211614/1891456 – Yogesh Badke Feb 05 '19 at 07:02
0

If you are using spring boot application then before running the application u should provide -Dspring.profiles.active=prod. By default spring environment variables override configurations present in properties file. But I agree with answer given by yogesh that profile should not be hard coded in the application.

Now coming to developers when they check out the code they can also provide -Dspring.profiles.acitve=dev as arguments in eclipse or intellij run configurations.

It's a one time configuration change in eclipse/intellij and it works really well. We are using this in our project and it really helps.

whysoseriousson
  • 196
  • 2
  • 16