如何在IntelliJ for Gradle任务中设置环境变量

cfr*_*ick 26 intellij-idea gradle

在命令行上运行时,gradle bootRun通过环境变量(例如SPRING_PROFILES_ACTIVE)将弹簧配置文件传递给(对我来说)的最简单方法.

应用程序配置不同,gradle任务的配置不提供环境变量的输入.由于虚拟机选项似乎没有被选中,我无法从IDE运行这些任务.

我知道,我可以用envvar设置启动IntelliJ,但这看起来相当麻烦.

所以我需要的是IntelliJ吊坠SPRING_PROFILES_ACTIVE=dev,testdb gradle bootRun,除非有充分的理由,否则他们就把它留了下来.

系统是linux,intellij 14.有问题的项目是使用springboot,我想从mainIntelliJ中运行到使用springloaded + bootRun和单独compileGroovy调用运行,因为IntelliJ没有完全"理解"gradle文件,因此隐藏了错误.

cfr*_*ick 8

使(或其他)任务System.properties可用bootRun.

bootRun.systemProperties = System.properties
Run Code Online (Sandbox Code Playgroud)

这样我们就可以在设定的IntelliJ VM选项喜欢-Dspring.profiles.active=dev.


Spi*_*der 5

这是我使用 Gradle/IntelliJ 设置 Spring 环境变量/设置的解决方案

首先定义一个基本的属性文件,然后根据你的环境定义一个,例如:

@Configuration
@PropertySources(value = {@PropertySource("classpath:default.properties"),@PropertySource("classpath:${env}.properties")})
Run Code Online (Sandbox Code Playgroud)

在上面的例子中,要特别注意@PropertySource("classpath:${env}.properties"). 这是一个被拉通的环境变量。

接下来,将 VM 参数添加到 IntelliJ(通过 Gradle 任务运行配置) - 或作为参数通过 gradle 命令行。

如何向 Gradle 运行配置添加 VM 参数 最后,复制 gradle 任务中的属性,正如@cfrick 提到的,@mdjnewman 已正确显示:

tasks.withType(org.springframework.boot.gradle.run.BootRunTask) {
    bootRun.systemProperties = System.properties
}
Run Code Online (Sandbox Code Playgroud)