我有5个环境:
- local (my development machine)
- dev
- qc
- uat
- live
- staging
我希望为每个环境使用不同的应用程序属性,所以我有以下属性文件,每个文件都有不同的数据源 URL:
- 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=本地
到脚本参数下的运行配置。
我试过添加
-Dspring.profiles.active=本地
到VM选项下的运行配置。
都不工作。我一直在启动时看到 INFO 消息:没有设置活动配置文件,回退到默认配置文件:默认
如果我使用从 Windows 命令行运行我的应用程序
gradle bootRun
但我先设置环境变量
set SPRING_PROFILES_ACTIVE=local
然后一切正常。
所以我的问题是,从 IntelliJ 运行 bootRun 时如何激活我的本地弹簧引导配置文件?
我将 -Dspring.profiles.active=test
添加到 VM 选项,然后重新运行该配置。它工作得很好。
这可以通过
选择运行 |编辑配置...
转到配置选项卡
展开 Environment 部分以显示 VM 选项
如果您实际使用 spring boot 运行配置(目前仅在 Ultimate Edition 中支持),则可以轻松在“Active Profiles”设置中预先配置配置文件。
https://i.stack.imgur.com/d6oIb.png
Application.main()
中传递 args
,例如 SpringApplication.run( Application.class, args );
default, staging
。最右边的优先级最高,因此例如 foo=bar
(默认)和 foo=baz
(暂存),将导致 foo=baz
随着 Spring Boot 的发展,它似乎改变了读取 VM 选项的方式。当您在 Intellij 中启动应用程序并想要激活某些配置文件时,可以尝试以下方法:
1.更改虚拟机选项
在“运行”中打开“编辑配置”,在“虚拟机选项”中添加:-Dspring.profiles.active=local
它实际上适用于我的一个带有 Spring Boot v2.0.3.RELEASE
和 Spring v5.0.7.RELEASE
的项目,但不适用于另一个带有 Spring Boot v2.1.1.RELEASE
和 Spring v5.1.3.RELEASE
的项目。
此外,当使用 Maven 或 JAR 运行时,人们提到了这一点:
mvn spring-boot:run -Drun.profiles=dev
或者
java -jar -Dspring.profiles.active=dev XXX.jar
(见此处:how to use Spring Boot profiles)
2.传递JVM参数
某处提到,如果您指定一些 JVM 选项,Spring 会改变启动应用程序进程的方式;它分叉了另一个进程,并且不会传递它收到的 arg,因此这不起作用。将 args 传递给它的唯一方法是:
mvn spring-boot:run -Dspring-boot.run.jvmArguments="..."
同样,这是给 Maven 的。 https://docs.spring.io/spring-boot/docs/current/maven-plugin/examples/run-debug.html
3.设置(应用程序)env var
第二个项目对我有用的是设置环境变量,如上面的一些答案中所述:“编辑配置”-“环境变量”,并且:
SPRING_PROFILES_ACTIVE=local
SPRING_PROFILES_ACTIVE
。
~/.profile
或类似的,您必须使用 SPRING_PROFILES_ACTIVE
- spring.profiles.active
可能仅用作命令行上的参数。
使用 IntelliJ 社区版 2021.x 测试
您可以创建多个配置,每个配置用于一个特定的配置文件,在下面的例子中,我创建了一个带有开发配置文件环境变量的开发配置。
转到运行 > 编辑配置 在左侧的应用程序下选择要编辑的配置。在右侧 > 在 Environment Variable 下,更新 spring.profiles.active=
注意:- 您还可以使用同一面板中的可用选项创建新配置或复制上述步骤 2 中的现有配置。
https://i.stack.imgur.com/3IkGR.png
尝试在你的 build.gradle 中添加这个命令
https://i.stack.imgur.com/ptAgq.png
因此,为了运行配置该形状:
https://i.stack.imgur.com/Je0Os.png
对于 Spring Boot 2.1.0 及更高版本,您可以使用
mvn spring-boot:run -Dspring-boot.run.profiles=foo,bar
spring-boot:run -Dspring-boot.run.profiles=local
我最终将以下内容添加到我的 build.gradle 中:
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 方法中。几周前我犯了同样的错误。
public static final void main(String... args) {
SpringApplication.run(Application.class, args);
}
Application
类,所以是从内存中手动生成的。错过了 args
步骤,因此运行配置中的“Active Profiles”框不起作用——我不得不在 VM 选项框中手动传入 -Dspring.profiles.active
。
我使用 Intellij 社区版。转到“运行/调试配置”> 运行器选项卡 > 环境变量 > 单击按钮“...”。添加:SPRING_PROFILES_ACTIVE = 本地
在我的情况下,我在 IntelliJ
的 VM 选项中使用了以下配置,它没有选择本地配置,但在重新启动 IntelliJ
后,它从 IntelliJ
选择了配置详细信息并且服务开始运行。
-Dspring.profiles.active=local
所以为了恢复...
如果您拥有 IntelliJ Ultimate,则正确答案是 Daniel Bubenheim 提供的答案
但如果你不这样做,请在 Run->Edit Configurations 中创建并在 Configuration 选项卡中添加下一个环境变量:
SPRING_PROFILES_ACTIVE=profilename
并执行 jar 做:
java -jar -Dspring.profiles.active=profilename XXX.jar
尝试这个。如下编辑您的 build.gradle 文件。
ext { profile = project.hasProperty('profile') ? project['profile'] : 'local' }
https://i.stack.imgur.com/a77E1.png
您可以尝试上述方式来激活个人资料
这里有2种方式
使用 gradle 项目属性
在 build.gradle 中,添加
bootRun{
//https://github.com/spring-projects/spring-boot/pull/592#issuecomment-880263914
if (project.hasProperty('profiles')) {
environment SPRING_PROFILES_ACTIVE: profiles
} else {
def profiles = 'dev'
environment SPRING_PROFILES_ACTIVE: profiles
}
}
在 intellij gradle 配置中,根据您要运行的环境更改“-Pprofiles”中的值“test”
https://i.stack.imgur.com/nAWgK.png
使用环境属性
关注@Hubert https://stackoverflow.com/a/39749545/3333878 的回答
并将运行配置配置为
https://i.stack.imgur.com/ZyXnz.png
在程序参数下设置 -Dspring.profiles.active=local
。
不定期副业成功案例分享