简体   繁体   English

Intellij IDEA:使用一组环境变量运行/调试Java应用程序

[英]Intellij IDEA: run/debug java application with a set of environment variables

My java application uses environment variables ( System.getenv ), and this set of variables is large and defined in a file like: 我的Java应用程序使用环境变量( System.getenv ),这组变量很大,并在如下文件中定义:

export VAR1="VAL1"
...
export VAR100500="VAL1500"

when i start the application from command line i use something like this: 当我从命令行启动应用程序时,我会使用以下命令:

source ../.config/.secret-secret && java  -jar build/libs/app-name.jar

and the values are exported right before the launch (actually, the source command may be executed only once per session, since the values are exported) 并且在启动之前就导出了这些值(实际上,由于导出了这些值,因此每个会话只能执行一次source命令)

But when i start the application from IDEA, reasonably those values are not exported to the run environment. 但是,当我从IDEA启动应用程序时,这些值不会合理地导出到运行环境中。

So, the question is: how can i export a set of environment variables to application launch environment in IDEA? 因此,问题是:如何在IDEA中将一组环境变量导出到应用程序启动环境?

So, what i've tried already: 所以,我已经尝试过了:

  • in Run/Debug Configurations -> Before Launch i started a shell script which exports those variables (i use the same script to run the app from command line). 运行/调试配置->启动之前,我启动了一个外壳脚本,该脚本导出这些变量(我使用相同的脚本从命令行运行该应用程序)。 Script executed successfully, but looks like the script is executed in different environment from the application itself, so values are exported (and printed to IDEA run console), but the application still can't read them from System.getenv 脚本已成功执行,但看起来脚本是在与应用程序本身不同的环境中执行的,因此可以导出值(并打印到IDEA运行控制台),但是应用程序仍无法从System.getenv读取它们

  • i can't easily use Run/Debug Configurations -> Environment Variables , because there are a lot of variables to set up manually, also they are changed from time to time. 我不能轻易使用Run / Debug Configurations-> Environment Variables ,因为有很多变量需要手动设置,而且它们会不时更改。

  • It's not enough to run entire IDEA with those variables, because, as i mentioned above, the values are changed often and should be re-read almost every launch. 用这些变量运行整个IDEA是不够的,因为,正如我上面提到的,这些值经常更改,并且几乎在每次启动时都应重新读取。

I can tell you that editing the workspace.xml file (which is where the environment variables entered in the run configuration are stored) directly does not work, so don't bother trying that. 我可以告诉您,直接编辑workspace.xml文件(在运行配置中输入的存储环境变量的位置)是行不通的,因此不必费心尝试。

I think you are going to have to write a plugin that parses your script and updates the run configuration. 我认为您将必须编写一个可解析脚本并更新运行配置的插件。 I haven't written a plugin to do this myself so can't give you a fuller answer. 我自己没有编写插件来执行此操作,因此无法给您更完整的答案。 You can see the relevant plugin in documentation here 您可以在此处的文档中查看相关的插件

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

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