user3173510
user3173510

Reputation: 33

Switching between Spring profiles on different environments with the same WAR archive

When I try to build the application via Maven mvn install, it says

java.lang.IllegalArgumentException: Could not resolve placeholder 'example_token' in value "${example_token}"

I assume it happens because the example_token does not exist in application.yml, it is both in application-dev.yml and application-prod.yml though.

The idea is to build one single WAR archive of the Maven application and switch the profiles according to the environment variables (SPRING_PROFILES_ACTIVE=dev/SPRING_PROFILES_ACTIVE=prod), for example. What is the best practice way to build a project via Maven to achieve this? One of the ways to solve this is to make mocked properties I suppose but I don't think it's a good approach. I am thinking of linking Maven and Spring Boot profiles but still I am not sure how to properly create WAR archive because of the mentioned error.

I have two Spring Boot application profiles with three correspondent YAML files: application.yml, application-dev.yml and application-prod.yml. When I run the application locally from the Idea with, for instance, dev active profile, everything works great.

I am using Spring-boot 3.0.13 and Maven 3.9.5

UPD: Thankfully to @M.Deinum from the comments, I found the root of the issue: during the CI pipeline execution, it was needed to explicitly set "skip tests". The profile setting is required otherwise even if there are no tests in the app. If someone wants to do the same locally, I assume that mvn install -DskipTests would help

Upvotes: 0

Views: 155

Answers (1)

Vinayak Nair
Vinayak Nair

Reputation: 203

Since you have already created different environment file. When you run your application, pass below profile information in vm arguments. No need to change anything on application.yml file.

Eclipse-> Run as run configuration->In vm args section:

-Dspring.profile.active=dev

Upvotes: 0

Related Questions