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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39738901/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:13:27  来源:igfitidea点击:

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

springintellij-ideagradlespring-boot

提问by dleerob

I have 5 environments:

我有 5 个环境:

 - local (my development machine)
 - dev
 - qc
 - uat
 - live
 - staging

I want different application properties to be used for each environment, so I have the following properties files each which have a different URL for the datasource:

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

 - application.properties  (containing common properties)
 - application-local.properties
 - application-dev.properties
 - application-qc.properties
 - application-uat.properties
 - application-live.properties

I am using IntelliJ and running my app using bootRun in the Gradle plugin on my local machine. I will be using deploying the same application war file on all other environments which run Tomcat.

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

I have tried adding:

我试过添加:

--spring.profiles.active=local

--spring.profiles.active=本地

to the run configuration under script parameters.

到脚本参数下的运行配置。

I have tried adding

我试过添加

-Dspring.profiles.active=local

-Dspring.profiles.active=本地

to the run configuration under VM options.

到 VM 选项下的运行配置。

Neither work. I keep seeing the INFO message on startup say: No active profile set, falling back to default profiles: default

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

If I run my app from the windows command line using

如果我使用 Windows 命令行运行我的应用程序

gradle bootRun

but I first set the environment variable

但我首先设置了环境变量

set SPRING_PROFILES_ACTIVE=local

Then everything works.

然后一切正常。

So my question is, how do I activate my local spring boot profile when running bootRun from IntelliJ ?

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

回答by Thom

I added -Dspring.profiles.active=testto VM Options and then re-ran that configuration. It worked perfectly.

我添加-Dspring.profiles.active=test到 VM 选项,然后重新运行该配置。它工作得很好。

This can be set by

这可以通过设置

  • Choosing Run | Edit Configurations...
  • Go to the Configurationtab
  • Expand the Environmentsection to reveal VM options
  • 选择 Run | Edit Configurations...
  • 转到Configuration选项卡
  • 展开该Environment部分以显示VM options

回答by Daniel Bubenheim

If you actually make use of spring boot run configurations (currently only supported in the Ultimate Edition) it's easy to pre-configure the profiles in "Active Profiles" setting.

如果您确实使用了 Spring Boot 运行配置(目前仅在 Ultimate Edition 中支持),则很容易在“Active Profiles”设置中预先配置配置文件。

enter image description here

在此处输入图片说明

回答by emerson moura

Try add this command in your build.gradle

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

enter image description here

在此处输入图片说明

So for running configure that shape:

因此,对于运行配置该形状:

enter image description here

在此处输入图片说明

回答by dleerob

I ended up adding the following to my build.gradle:

我最终将以下内容添加到我的 build.gradle 中:

bootRun {
  environment SPRING_PROFILES_ACTIVE: environment.SPRING_PROFILES_ACTIVE ?: "local"
}

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

So now when running bootRun from IntelliJ, it defaults to the "local" profile.

所以现在从 IntelliJ 运行 bootRun 时,它默认为“本地”配置文件。

On our other environments, we will simply set the 'SPRING_PROFILES_ACTIVE' environment variable in Tomcat.

在我们的其他环境中,我们将简单地在 Tomcat 中设置“SPRING_PROFILES_ACTIVE”环境变量。

I got this from a comment found here: https://github.com/spring-projects/spring-boot/pull/592

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

回答by Hubert Str?bitzer

A probable cause could be that you do not pass the command line parameters into the applications main method. I made the same mistake some weeks ago.

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

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

回答by WesternGun

Spring Boot seems had changed the way of reading the VM options as it evolves. Here's some way to try when you launch an application in Intellij and want to active some profile:

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

1. Change VM options

1. 更改虚拟机选项

Open "Edit configuration" in "Run", and in "VM options", add: -Dspring.profiles.active=local

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

It actually works with one project of mine with Spring Boot v2.0.3.RELEASEand Spring v5.0.7.RELEASE, but not with another project with Spring Boot v2.1.1.RELEASEand Spring v5.1.3.RELEASE.

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

Also, when running with Maven or JAR, people mentioned this:

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

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

or

或者

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

(See here: how to use Spring Boot profiles)

(参见此处:如何使用 Spring Boot 配置文件

2. Passing JVM args

2.传递JVM参数

It is mentioned somewhere, that Spring changes the way of launching the process of applications if you specify some JVM options; it forks another process and will not pass the arg it received so this does not work. The only way to pass args to it, is:

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

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

Again, this is for Maven. https://docs.spring.io/spring-boot/docs/current/maven-plugin/examples/run-debug.html

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

3. Setting (application) env var

3. 设置(应用)env var

What works for me for the second project, was setting the environment variable, as mentioned in some answer above: "Edit configuration" - "Environment variable", and:

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

SPRING_PROFILES_ACTIVE=local

回答by user2293536

In my case I used below configuration at VM options in IntelliJ, it was not picking the local configurations but after a restart of IntelliJit picked configuration details from IntelliJand service started running.

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

-Dspring.profiles.active=local

回答by Ryan Wibawa

I use the Intellij Community Edition. Go to the "Run/Debug Configurations" > Runner tab > Environment variables > click button "...". Add: SPRING_PROFILES_ACTIVE = local

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

spring.profiles.active

spring.profiles.active

回答by Mr. K.

For Spring Boot 2.1.0and higher you can use

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

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

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

回答by Amrit Malla

Replace your profile name with BE

用 BE 替换您的个人资料名称

You can try the above way to activate a profile

您可以尝试以上方式激活配置文件