本文介绍了IntelliJ IDEA 全局环境变量配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的所有想法运行配置中使用环境变量.我目前使用运行->编辑配置->然后在选定的配置中输入环境变量.然而,当我需要运行独立的测试场景时,这是非常乏味的,因为每个场景都会创建一个新的运行配置,我需要重新输入变量.

我尝试在各种会话配置文件中使用 export SOME_VAR="some value" 在我的 linux 系统中设置环境变量:/etc/profile,/etc/bash.bashrc,~/.bashrc,~/.profile 但intellij 似乎在运行期间忽略了这些变量,即使当我启动 echo ${SOME_VAR} 从 intellij 内置终端显示正确的输出.

我也尝试使用

请注意,您只有几个变量,而且 MY_VAR 为空.

这是我用来加载环境变量的配置:

  1. 单击项目中的选择运行/调试配置"并选择编辑配置":
  1. 然后,单击环境变量"部分右侧带有..."的按钮:
  1. 您会看到一个弹出窗口.选中左下角带有包括父环境变量"标签的复选框:

就是这样!!!

如果你现在运行你的程序,你会在你的控制台上看到这样的东西:

您可以看到所有环境变量,当然还有您的MY_VAR"变量,以及正确的值!

超越基础

通常,出于安全原因,我们不想让所有环境变量都可见.我们想要做的是使这些变量仅在 IntelliJ(或我们的程序)运行时可见.

因此,无论是在调用 Intellij 之前还是在关闭它之后,环境中都不应该显示任何敏感变量.

此外,出于安全原因,您希望将这些变量保存在一个文件中(通常带有 .env 扩展名),以便于操作.

为了实现这一点,有一些有用的程序(你可以谷歌它们),但我最喜欢的是

但是如果你退出 IntelliJ,并寻找你的变量,你会看到该变量不存在(你可以使用 env 来确认):

此时,我希望您能够使用自己的解决方案:创建内部设置变量的shell脚本,测试其他程序(direnvautoenv 等)等.>

享受吧!

...

I need to use an envirnoment variable in all of my idea run configurations. I currently use run->edit configurations->and then enter the env variables in selected configuration. However that's very tedious when I need to run isolated test scenarios because each one creates a new run configuration and I need to enter the variables all over again.

I tried to set the env variables in my linux system using export SOME_VAR="some value" in various session profile files: /etc/profile,/etc/bash.bashrc,~/.bashrc,~/.profile but intellij seems to ignore those vars during run, even though when I launch echo ${SOME_VAR} from intellij built-in terminal it displays the correct output.

I also tried using intellij .env file plugin and then set SOME_VAR=some value in .env file in project root. Didn't work either.

解决方案

I found a solution to set environment variables on IntelliJ that has been working very well for me, and is incredibly simple. Let me show you.

This is the program (you can copy and paste it) we're using to test:

package com.javasd.intelijenv;

import java.util.Map;

public class Main {
    public static void main(String[] args) {
        Map<String, String> env = System.getenv();
        for (String envName : env.keySet()) {
            System.out.format("%s=%s%n", envName, env.get(envName));
        }

        System.out.println("My home directory: " + System.getenv("MY_VAR"));
    }
}

This program basically loads all environment variables, show them on the console, and try to show an env variable. Also, it assumes that you had created the MY_VAR env variable before calling IntelliJ IDEA, by doing something like:

$ export MY_VAR="This is my adorable var :)"
$ idea

Please, notice that we're calling IntelliJ IDEA in the same terminal (or session, or window) where we created the environment variable. If you create the variable and call the IDEA from the icon, the solution won't work because the IDEA will create its own session.

So, if run it without the correct configuration you will get something line this in your console:

Please, notice that you have just a few variables, and that MY_VAR is null.

Here's configuration I use to load the environment variables:

  1. Click on the "Select Run/Debug Configurations" in your project and select "Edit Configurations":
  1. Then, click on the the button with "..." on the right side of the "Environment Variables" section:
  1. You'll see a pop-up. Check the checkbox on the bottom-left which has the label "Include parent environment variables":

That's it!!!

If you run your program now you will see something like this on your console:

You can see all the environment variables and, of course, your "MY_VAR" variable, with the right value!

Beyond the Basics

Usually, for security reasons, we don't want to keep all the environment variables visible. What we want to do is to make those variables visible only while the IntelliJ (or our program) is running.

So, no sensitive variables should be visible on the environment neither before you call Intellij nor after you close it.

Also, you want to keep those variables in a file (typically with a .env extension) to make it easy to manipulate and for security reasons.

To achieve this, there are some useful programs (you can Google them), but my favorite one is the env-cmd.

Let's say you have a test.env file with the following content:

MY_TEST_VAR=I live in the test.env file.

If you call IntelliJ by doing this:

$ env-cmd test.env idea

And edit your program to show "MY_TEST_VAR", and run it, you will see this on the IntelliJ's console:

But if you quit the IntelliJ, and look for your variable, you will see that the var doesn't exist (you can use env to confirm):

At this point, I hope you're able to play with your own solutions: create shell scripts with variables set inside, test other programs (direnv, autoenv, etc.), and so on.

Enjoy!

...

这篇关于IntelliJ IDEA 全局环境变量配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-18 12:51