5

I'm looking for a way to set spring profile in jetty programmatically so that the application that the war file on the server used the given profile, this is my code:

final WebAppContext context = new WebAppContext();
context.setLogUrlOnStart(true);
context.setWar("target/deployables/myapp-rest/myapp-rest.war");
context.setContextPath("/" + TEST_APP_CONTEXT);
context.setParentLoaderPriority(true);
server.setHandler(context);
server.start();

I tried a couple of things but none of them seem to work... I need to pass -Dspring.profiles.active=myProfile

Lukasz
  • 691
  • 1
  • 12
  • 30

1 Answers1

0

This is tested with Jetty 9.3 but webdefault.xml seem to also be available for lower versions (it's placement might be different though).

Go to $JETTY_HOME/etc and open webdefault.xml. Search for context-param in the file. Add this code somewhere below:

<context-param>
   <param-name>spring.profiles.default</param-name>
   <param-value>prod</param-value>
</context-param>

This will work if your web.xml (in the your-app.war file) doesn't contain this context-param.

Otherwise you could also use override-web.xml (docs) but you would need to configure it in jetty-web.xml and jetty-web.xml must be bundled inside the war... So YMMV, but I didn't want to change my war and webdefault.xml is an easier solution for me.

Nux
  • 9,276
  • 5
  • 59
  • 72