从IntelliJ运行时如何激活Spring Boot配置文件?

How do I activate a Spring Boot profile when running from IntelliJ?

我有5个环境:

1
2
3
4
5
6
 - local (my development machine)
 - dev
 - qc
 - uat
 - live
 - staging

我希望每个环境使用不同的应用程序属性,因此我有以下属性文件,每个属性文件的数据源都有不同的URL:

1
2
3
4
5
6
 - application.properties  (containing common properties)
 - application-local.properties
 - application-dev.properties
 - application-qc.properties
 - application-uat.properties
 - application-live.properties

我正在使用IntelliJ,并在本地计算机上的Gradle插件中使用bootRun运行我的应用程序。我将在所有其他运行Tomcat的环境中使用相同的应用程序war文件。

我尝试添加:

--spring.profiles.active=local

脚本参数下的运行配置。

我尝试添加

-Dspring.profiles.active=local

到VM选项下的运行配置。

都不起作用。我一直在启动时看到INFO消息,说:未设置活动配置文件,回退到默认配置文件:默认

如果我使用以下命令从Windows命令行运行应用程序

1
gradle bootRun

但是我首先设置环境变量

1
set SPRING_PROFILES_ACTIVE=local

然后一切正常。

所以我的问题是,从IntelliJ运行bootRun时如何激活本地spring引导配置文件?


我将-Dspring.profiles.active=test添加到VM Options,然后重新运行该配置。效果很好。

可以通过以下方式设置

  • 选择Run | Edit Configurations...
  • 转到Configuration选项卡
  • 展开Environment部分以显示VM options


如果您实际使用Spring Boot运行配置(当前仅在Ultimate Edition中受支持),则可以在"活动配置文件"设置中轻松配置配置文件。

enter image description here


尝试在build.gradle中添加此命令

enter image description here

因此,要运行,请配置该形状:

enter image description here


我最终在build.gradle中添加了以下内容:

1
2
3
4
5
6
7
bootRun {
  environment SPRING_PROFILES_ACTIVE: environment.SPRING_PROFILES_ACTIVE ?:"local"
}

test {
  environment SPRING_PROFILES_ACTIVE: environment.SPRING_PROFILES_ACTIVE ?:"test"
}

因此,现在从IntelliJ运行bootRun时,它默认为"本地"配置文件。

在其他环境中,我们只需在Tomcat中设置" SPRING_PROFILES_ACTIVE"环境变量。

我是从这里的评论中得到的:https://github.com/spring-projects/spring-boot/pull/592


可能的原因可能是您没有将命令行参数传递给应用程序main方法。几周前我犯了同样的错误。

1
2
3
public static final void main(String... args) {
    SpringApplication.run(Application.class, args);
}


随着Spring Boot的发展,似乎已经改变了读取VM选项的方式。当您在Intellij中启动应用程序并希望激活某些配置文件时,可以尝试以下方法:

1.更改虚拟机选项

在"运行"中打开"编辑配置",然后在" VM选项"中添加:-Dspring.profiles.active=local

它实际上适用于我的一个带有Spring Boot v2.0.3.RELEASESpring v5.0.7.RELEASE的项目,但是不适用于另一个带有Spring Boot v2.1.1.RELEASESpring v5.1.3.RELEASE的项目。

另外,当使用Maven或JAR运行时,人们提到了这一点:

1
mvn spring-boot:run -Drun.profiles=dev

要么

1
java -jar -Dspring.profiles.active=dev XXX.jar

(请参阅此处:如何使用Spring Boot配置文件)

2.传递JVM参数

在某处提到,如果您指定一些JVM选项,Spring会更改启动应用程序过程的方式。它派生另一个进程,并且不会传递收到的arg,因此这不起作用。将args传递给它的唯一方法是:

1
mvn spring-boot:run -Dspring-boot.run.jvmArguments="..."

同样,这是给Maven的。
https://docs.spring.io/spring-boot/docs/current/maven-plugin/examples/run-debug.html

3.设置(应用程序)环境变量

对于第二个项目,对我有用的是设置环境变量,如上面的一些回答所述:"编辑配置"-"环境变量",以及:

1
SPRING_PROFILES_ACTIVE=local


在我的情况下,我在IntelliJ中的VM选项中使用了以下配置,它没有选择本地配置,但是在重新启动IntelliJ之后,它从IntelliJ中选择了配置详细信息并开始运行服务。

1
-Dspring.profiles.active=local

我使用Intellij社区版。
转到"运行/调试配置">"运行器"选项卡>"环境变量">单击按钮" ..."。加:
SPRING_PROFILES_ACTIVE =本地

spring.profiles.active


Replace your profile name with BE

您可以尝试上述方式来激活个人资料


对于Spring Boot 2.1.0及更高版本,可以使用

mvn spring-boot:run -Dspring-boot.run.profiles=foo,bar


尝试这个。如下编辑您的build.gradle文件。

1
ext { profile = project.hasProperty('profile') ? project['profile'] : 'local' }

在程序参数下设置-Dspring.profiles.active=local