方法
build.gradle の bootRun タスク設定の jvmArgs にて、システムプロパティ値 spring.profiles.active にプロファイルを指定すれば良い。
// bootRun タスクの設定を追加する
bootRun {
// foobar プロファイルを指定する
jvmArgs = ['-Dspring.profiles.active=foobar']
}
参考資料:
動作確認
Spring Initializr でソースコードを生成して、必要に応じて追加・削除・編集した。
今回の環境
- Java 11 (OpenJDK 11.0.2)
- Spring Boot 2.2.2
- Spring Boot Gradle Plugin 2.2.2
- Gradle 6.0.1
ソースコード一覧
├── build.gradle
├── settings.gradle
└── src
└── main
├── java
│ └── com
│ └── example
│ └── demo
│ └── DemoApplication.java
└── resources
├── application-foobar.properties
└── application.properties
build.gradle
Gradle 用ビルド設定ファイル。
bootRun タスクの設定を追加している。
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()
}
// bootRun タスクの設定を追加する
bootRun {
// プロファイルを指定する
jvmArgs = ['-Dspring.profiles.active=foobar']
}
settings.gradle
Spring Initializr が生成したものをそのまま使っている。
rootProject.name = 'demo'
DemoApplication.java
トップページにアクセスしたときに JSON を返す処理を追加している。
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;
// トップページにアクセスした際に JSON を返す
@RequestMapping("/")
public ResponseEntity<Map<String, Object>> top() {
Map<String, Object> body = new HashMap<>();
// 適用されているプロファイル
body.put("profiles", env.getActiveProfiles());
// application-*.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
デフォルトのプロパティ設定ファイル。
プロファイルを指定していない場合はこのファイルの値が使用できる。
my.sample=This is a sample property.
my.message=This is a application.properties.
application-foobar.properties
foobar プロファイル使用時のプロパティ設定ファイル。
foobar プロファイル使用時には、application.properties の値がこのファイルの値で上書きされる。
my.message=This is a application-foobar.properties.
動作確認
gradle bootRun コマンドで Spring Boot を起動する。
$ gradle bootRun
> Task :bootRun
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.2.RELEASE)
他のコンソール等から curl でアクセスすると JSON が返ってくる。
gradle.build の bootRun タスク設定が記述されていない場合は、以下のような JSON が返される。
application.properties の値が出力されている。
$ curl http://localhost:8080/
{"my.sample":"This is a sample property.","profiles":[],"my.message":"This is a application.properties."}
gradle.build の bootRun タスク設定で jvmArgs = ['-Dspring.profiles.active=foobar'] が記述されている場合は、以下のような JSON が返される。
application.properties の値より application-foobar.properties の値が優先して出力されている。
$ curl http://localhost:8080/
{"my.sample":"This is a sample property.","profiles":["foobar"],"my.message":"This is a application-foobar.properties."}