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:
I generated the source code with Spring Initializr and added / deleted / edited it as needed.
├── 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.
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."}
Recommended Posts