Spring Boot Profiles

In this technical post, we’ll discuss how to manage different properties files when we run a Spring Boot application, this is helpful when you need to manage several environments. NOTE: If you need to know what tools you need to have installed in your computer in order to create a Spring Boot basic project, please refer my previous post: Spring Boot

Then execute this command in your terminal.

spring init --dependencies=webflux --build=gradle --language=java spring-boot-profiles

This is the build.gradle file generated:

buildscript {
  ext {
    springBootVersion = '2.1.0.RELEASE'
  repositories {
  dependencies {

apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

group = 'com.jos.dem.springboot.profiles'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {

dependencies {

bootRun {
  systemProperties = System.properties

With systemProperties = System.properties in the bootRun task we are specifying to read command line properties. Now let’s add as many properties files as environmets we want, so in that way we can define which one will be active from command line. In this example we are going to define only DEV and QA.





Next, let’s read user.url property using @Value annotation and print it using CommandLineRunner which is a call back interface in Spring Boot, when Spring Boot starts will call it and pass in args through a run() internal method.

package com.jos.dem.springboot.profiles;

import org.springframework.context.annotation.Bean;
import org.springframework.boot.CommandLineRunner;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class ProfilesApplication {

  private String userUrl;

  public static void main(String[] args) {
    SpringApplication.run(ProfilesApplication.class, args);

  CommandLineRunner run(){
    return args -> {
      System.out.println("Environment: " + userUrl);


That’s it, if you run this application you will see user.url property value selected.

gradle -Dspring.profiles.active=dev bootRun

Using Maven

You can do the same using Maven, the only difference is that you need to specify --build=maven parameter in the spring init command line:

spring init --dependencies=webflux --build=maven --language=java spring-boot-profiles

This is the pom.xml file generated:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">


  <description>This project shows how to use profiles in a Spring Boot project with Gradle and Maven</description>

    <relativePath/> <!-- lookup parent from repository -->






To run the project using Maven

mvn -Dspring.profiles.active=dev spring-boot:run

To browse the code go here, to download the project:

git clone git@github.com:josdem/spring-boot-profiles.git

Return to the main article

comments powered by Disqus