2023年12月27日 星期三

Spring Boot 執行時不同 Profile 時, Eclipse 的處理

這是我使用 Spring boot 時遇到的狀況

我的系統有複數個 profile,如下:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.7.0</version>
		<relativePath /> 
	</parent>
	<groupId>com.example</groupId>
	<artifactId>foobar</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	<name>Demo</name>
	<description>Demo project</description>
	<properties><!-- 中略--></properties>
	<dependencies><!-- 中略--></dependencies>
	<build><!-- 中略--></build>
	<profiles>
		<profile>
			<id>local</id>
			<properties>
				<active.profile>local</active.profile>
			</properties>
			<activation>
				<activeByDefault>true</activeByDefault>
			</activation>
		</profile>
		<profile>
			<id>product</id>
			<properties>
				<active.profile>product</active.profile>
			</properties>
		</profile>
	</profiles>
</project>

如果我要使用 profile local 執行的話,maven 執行以下指令:

mvn spring-boot:run -Plocal

但如果我是使用 Eclipse 的 Java application 執行,那要這樣處理:

  1. 在 Debug / Run Configuration 中選擇你的 Configuration 
  2. 進去 Arguments tab
  3. VM arguments 中填入 
    -Dspring.profiles.active=XXX
    假設我要使用 local profile,那就是
    -Dspring.profiles.active=local

沒有留言:

張貼留言