简体   繁体   English

使用 Spring Boot Gradle 插件和 IntelliJ 设置环境变量

[英]Set Environment Variable with Spring Boot Gradle plugin and IntelliJ

My question is about environment variables.我的问题是关于环境变量。 I am using the Spring Boot Gradle plugin and developing in Intellij 14 on Windows.我正在使用 Spring Boot Gradle 插件并在 Windows 上的 Intellij 14 中进行开发。

I'm trying to get some Pub/Sub example code for Google API's working, but I don't think this is the main point of my question (Noted here since I could be wrong!) If not running on AppEngine, the sample code has a number of ways that you can satisfy it by supplying a file with authentication credentials.我正在尝试获取一些用于 Google API 工作的 Pub/Sub 示例代码,但我认为这不是我的问题的重点(注意这里,因为我可能是错的!)如果不是在 AppEngine 上运行,示例代码有多种方法可以通过提供带有身份验证凭据的文件来满足它。 I currently have no plans to use AppEngine.我目前没有使用 AppEngine 的计划。

I am asked to set an environment variable named GOOGLE_APPLICATION_CREDENTIALS and after some small amount of hair-pulling I've learned that a system property will not suffice.我被要求设置一个名为 GOOGLE_APPLICATION_CREDENTIALS 的环境变量,经过一些简单的梳理后,我了解到系统属性是不够的。 It must be an environment variable.它必须是一个环境变量。

I would like to do this in a way that adjusts the environment for only this IntelliJ project and not for everything on the machine.我想以一种只为这个 IntelliJ 项目而不是为机器上的所有东西调整环境的方式来做到这一点。 Normally in IntelliJ I would do this by adding the environment variable to the launch configuration options.通常在 IntelliJ 中,我会通过将环境变量添加到启动配置选项来做到这一点。 Since I am using the Spring Boot Gradle plugin, I have a 'bootRun' launch configuration for my project.由于我使用的是 Spring Boot Gradle 插件,因此我的项目有一个“bootRun”启动配置。 The launch configuration options screen has the following major options: Name, Gradle Project, Tasks, VM Options, Script Parameters, Before Launch启动配置选项屏幕有以下主要选项:名称、Gradle 项目、任务、VM 选项、脚本参数、启动前

There is NO option for Environment Variables.环境变量没有选项。

I am aware that I could probably fix this problem by doing one of the following:我知道我可以通过执行以下操作之一来解决此问题:

  • Add the environment variable under the windows system control panel在windows系统控制面板下添加环境变量
  • Stop using the Spring Boot plugin so that the 'Environment Variables' section of the launch configuration options comes back as an option停止使用 Spring Boot 插件,以便启动配置选项的“环境变量”部分作为选项返回

What I am hoping is that there is another option I'm not aware of due to my lack of knowledge about Spring [Boot] and/or Gradle.我希望有另一种选择,因为我对 Spring [Boot] 和/或 Gradle 缺乏了解。

In the Gradle Spring Boot plugin, you can set environment variables as an array of key:value pairs. 在Gradle Spring Boot插件中,您可以将环境变量设置为key:value对的数组。

bootRun {
    environment = ["GOOGLE_APPLICATION_CREDENTIALS": "[PATH TO JSON]" ]
}

Set your environment variable in gradle like below 如下所示在gradle中设置环境变量

test {
       environment "GOOGLE_APPLICATION_CREDENTIALS", "<path_to_json>"
     }

You can set the environment variable in the build.gradle below:您可以在下面的 build.gradle 中设置环境变量:

test {
    environment "GOOGLE_APPLICATION_CREDENTIALS", "${project.file("/src/test/resources/service-account-data.json")}"
     }

hopefully, this helps someone out.希望这可以帮助某人。 Enjoy!享受!

You can also try to do this from the command line.您也可以尝试从命令行执行此操作。 You need to set the bootRun.systemProperties to the gradle's system properties.您需要将bootRun.systemProperties设置为 gradle 的系统属性。

This is how to do this in build.gradle :这是在build.gradle执行此操作的build.gradle

bootRun.systemProperties = System.properties

and then you can just run:然后你可以运行:

gradle bootRun -DGOOGLE_APPLICATION_CREDENTIALS=/path/to/json

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM