如何从IntelliJ运行时激活Spring Boot配置文件? [英] How do I activate a Spring Boot profile when running from IntelliJ?

查看:104
本文介绍了如何从IntelliJ运行时激活Spring Boot配置文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有5个环境:

$ p $ - local(我的开发机器)
- dev
- qc
- uat
- live
- staging

我想不同的应用程序属性用于每个环境,所以我有以下属性文件,每个都有不同的数据源URL:

   -  application.properties(包含公共属性)
- application-local.properties
- application-dev.properties
- application-qc.properties
- application-uat.properties
- application-live.properties

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



我试过添加:


- spring.profiles.active = local


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



我已经尝试添加

lockquote

-Dspring.profiles.active = local

添加到虚拟机选项下的运行配置中。



都不行。我一直在启动时看到INFO消息:没有活动配置文件集,回到默认配置文件:default



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

  gradle bootRun 

但我首先设置环境变量

  set SPRING_PROFILES_ACTIVE = local 

然后一切正常。

所以我的问题是,怎么做我从IntelliJ运行bootRun时激活了我的本地spring启动配置文件?

解决方案

我最终将以下代码添加到了我的build.gradle中:

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

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

现在从IntelliJ运行bootRun时,它默认为本地 profile。



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

我从这里找到的评论中了解到这一点: https://github.com/spring -projects / spring-boot / pull / 592


I have 5 environments:

 - 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:

 - 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.

I have tried adding:

--spring.profiles.active=local

to the run configuration under script parameters.

I have tried adding

-Dspring.profiles.active=local

to the run configuration under VM options.

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

If I run my app from the windows command line using

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 ?

解决方案

I ended up adding the following to my 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.

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

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

这篇关于如何从IntelliJ运行时激活Spring Boot配置文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆