Manu
Manu

Reputation: 337

How to exclude default application.properties add custom properties file using profiles in maven for spring boot project?

I have developed a web application using spring boot. I have three resource folders in src/main/resources staging,qa,production which consists of application properties and logging configuration for the specific environment. Along with these folders I have application.properties and logging configuration in resources folder which I use it for dev environment. I want to package war file according to the environment using spring-boot-maven plugin. I am new to maven any help would be appreciated?

Upvotes: 0

Views: 4729

Answers (2)

Dimitri Mestdagh
Dimitri Mestdagh

Reputation: 44675

Rather than using Maven to add different folders to your classpath for each environment, you can use Spring profiles.

First of all create different application.properties for each environment, such as:

  • application-staging.properties
  • application-qa.properties
  • application-production.properties

For the logging you can use the logging.config property. So, in application-staging.properties you could use:

logging.config=classpath:logback-staging.xml

In the other properties files you can use different logging.config properties.

Now just run your application with the spring.profiles.active property.


However, an easier solution would be to use externalized configuration. Rather than having to rebuild each time you want to change configuration for a specific profile, you can externalize it by putting an application.properties file next to your JAR/WAR in the correct environment, rather than on your classpath. Spring boot will pick this up automatically.

Now you can also externalize your logging config by placing a logback.xml (or log4j2.xml, ...) file next to your JAR/WAR and just configure your (externalized) application properties with:

logging.config=file:logback.xml

This allows you to edit your configuration and logging without having to change your JAR/WAR.

Upvotes: 1

Kamil Witkowski
Kamil Witkowski

Reputation: 2083

Convetion is application-{profileName}.properties

Point 10 and 11:

http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

e.g application-test.properties it overrides the application.properties Make profile in pom.xml

Introduction to profiles: http://maven.apache.org/guides/introduction/introduction-to-profiles.html

Upvotes: 2

Related Questions