Spring Boot with Maven Resource Filter (Profile)

If you want to know how to use Maven resource filter with Spring Boot, this page is for you.

This example is a part of  Spring Boot with all examples

Full Code with Spring Boot 1.5.8.RELEASE:

11_spring-boot-maven-resource-filter.zip

What you need for run this:

  • Maven 3
  • Java 8

What the code will do:

  • Call a url and it will return a value which is defined in a profile in Maven profile

Explain:

This is the file structure:

Let say you have two maven profiles in pom.xml, see the yellow lines:

The “local” profile is for the local computer.

The “production” profile is for  the production environment.

Note: normally with Maven, you have to  have these lines in the <build> tag.

But you do not need this here because Spring Boot will take care it.

In application.properties looks like this:

Note: normally with Maven, you have to use ${database.url}, but here it is @database.url@

In TestRestController.java, it will get the property from application.properties and just return it if “/api/show” is called:

Key point:

  • Line 10: @Value(“${database.url}”) : it is the Spring Boot annotation to get value from application.properties.

Try:

run this in console:

Then run this to start (this will use the default profile which is “local”):

You should see something like this:

Open a browser and try to go to this url:

You will get this:

Then stop the console by Ctrl + C (on Windows).

And start with this:

Open a browswer and try to go to this url again:

You will get this: