[JAVA] Set Spring profile when executing bootRun task with Spring Boot Gradle Plugin

Method

In jvmArgs of bootRun task setting of build.gradle, you can specify the profile in the system property value spring.profiles.active.

build.gradle


//Add settings for the bootRun task
bootRun {
  //Specify the foobar profile
  jvmArgs = ['-Dspring.profiles.active=foobar']
}

Reference:

Operation check

I generated the source code with Spring Initializr and added / deleted / edited it as needed.

This environment

Source code list

├── build.gradle
├── settings.gradle
└── src
    └── main
        ├── java
        │   └── com
        │       └── example
        │           └── demo
        │               └── DemoApplication.java
        └── resources
            ├── application-foobar.properties
            └── application.properties

build.gradle

Build configuration file for Gradle. Adding settings for the bootRun task.

build.gradle


plugins {
  id 'org.springframework.boot' version '2.2.2.RELEASE'
  id 'io.spring.dependency-management' version '1.0.8.RELEASE'
  id 'java'
}

group = 'com.example'
version = '0.0.1'
sourceCompatibility = '11'

repositories {
  mavenCentral()
}

dependencies {
  implementation 'org.springframework.boot:spring-boot-starter-web'
  testImplementation('org.springframework.boot:spring-boot-starter-test') {
    exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
  }
}

test {
  useJUnitPlatform()
}

//Add settings for the bootRun task
bootRun {
  //Specify a profile
  jvmArgs = ['-Dspring.profiles.active=foobar']
}

settings.gradle

The one generated by Spring Initializr is used as it is.

settings.gradle


rootProject.name = 'demo'

DemoApplication.java

A process that returns JSON when the top page is accessed has been added.

package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@SpringBootApplication
@RestController
public class DemoApplication {

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

  @Autowired
  private Environment env;

  //Returns JSON when accessing the top page
  @RequestMapping("/")
  public ResponseEntity<Map<String, Object>> top() {

    Map<String, Object> body = new HashMap<>();

    //Applicable profile
    body.put("profiles", env.getActiveProfiles());

    // application-*.Value obtained from properties
    body.put("my.sample", env.getProperty("my.sample"));
    body.put("my.message", env.getProperty("my.message"));

    return new ResponseEntity<>(body, HttpStatus.OK);
  }
}

application.properties

The default property settings file. If you do not specify a profile, you can use the values in this file.

my.sample=This is a sample property.
my.message=This is a application.properties.

application-foobar.properties

Property settings file when using the foobar profile. When using the foobar profile, the value in application.properties is overwritten with the value in this file.

my.message=This is a application-foobar.properties.

Operation check

Start Spring Boot with the gradle bootRun command.

$ gradle bootRun

> Task :bootRun

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.2.2.RELEASE)

If you access it with curl from another console etc., JSON will be returned.

If the bootRun task setting of gradle.build is not described, the following JSON will be returned. The value of application.properties is output.

$ curl http://localhost:8080/
{"my.sample":"This is a sample property.","profiles":[],"my.message":"This is a application.properties."}

If jvmArgs = ['-Dspring.profiles.active = foobar'] is described in the bootRun task setting of gradle.build, the following JSON will be returned. The value of application-foobar.properties is output with priority over the value of application.properties.

$ curl http://localhost:8080/
{"my.sample":"This is a sample property.","profiles":["foobar"],"my.message":"This is a application-foobar.properties."}

Reference material

Recommended Posts

Set Spring profile when executing bootRun task with Spring Boot Gradle Plugin
Set cookies with Spring Boot
Spring Boot gradle build with Docker
(IntelliJ + gradle) Hello World with Spring Boot
Create a website with Spring Boot + Gradle (jdk1.8.x)
Run Scala applications with Spring Boot through Gradle
Build Spring Boot project by environment with Gradle
I wanted to gradle spring boot with multi-project
View the Gradle task in the Spring Boot project
[Note] Configuration file when using Logback with Spring Boot
Switch environment with Spring Boot application.properties and @Profile annotation
[Java] [Spring Boot] Specify runtime profile --Spring Boot starting with NetBeans
A memorandum when creating a REST service with Spring Boot
Download with Spring Boot
Check how to set the timeout when connecting with Spring + HikariCP + MySQL and executing SQL
I made a plugin to execute jextract with Gradle task
Set context-param in Spring Boot
Generate barcode with Spring Boot
Hello World with Spring Boot
Implement GraphQL with Spring Boot
Spring Boot tutorial task schedule
Get started with Spring boot
Hello World with Spring Boot!
Spring Boot 2 multi-project in Gradle
Run LIFF with Spring Boot
SNS login with Spring Boot
File upload with Spring Boot
Spring Boot starting with copy
Spring Boot starting with Docker
Hello World with Spring Boot
Use Spring JDBC with Spring Boot
Add module with Spring Boot
Getting Started with Spring Boot
Create microservices with Spring Boot
Send email with spring boot
Use Basic Authentication with Spring Boot
Memorandum of understanding when Spring Boot 1.5.10 → Spring Boot 2.0.0
gRPC on Spring Boot with grpc-spring-boot-starter
Create an app with Spring Boot 2
Hot deploy with Spring Boot development
Database linkage with doma2 (Spring boot)
How to set Spring Boot + PostgreSQL
Spring Boot programming with VS Code
Until "Hello World" with Spring Boot
Inquiry application creation with Spring Boot
Get validation results with Spring Boot
(Intellij) Hello World with Spring Boot
Create an app with Spring Boot
Spring profile function, and Spring Boot application.properties
Google Cloud Platform with Spring Boot 2.0.0
Check date correlation with Spring Boot
Implement Spring Boot application in Gradle
I tried GraphQL with Spring Boot
[Java] LINE integration with Spring Boot
Beginning with Spring Boot 0. Use Spring CLI
I tried Flyway with Spring Boot
Message cooperation started with Spring Boot
Personal memo Run Spring Boot + Gradle web project with Codenvy (Eclipse Che)