如何在Eclipse Gradle项目的运行配置中设置环境变量?

huangapple go评论70阅读模式
英文:

How to set Environment variables in Run Configuration of an eclipse gradle project?

问题

我在Eclipse中有一个Gradle项目,它在运行时读取环境变量。我可以成功地创建一个JAR文件,然后在Shell中输入export MY_VAL=7,然后执行这个JAR文件。但是我想要从Eclipse内部运行这个项目,所以我需要在运行配置中的某个地方设置这些环境变量。
对于普通的Java项目来说,这是很简单的:

如何在Eclipse Gradle项目的运行配置中设置环境变量?

但是在Gradle中却没有这样的选项卡:

如何在Eclipse Gradle项目的运行配置中设置环境变量?

我该如何为执行时指定这些环境变量?

提前消歧义:
https://stackoverflow.com/q/50860982/3014199 的标题非常相似,但实际上询问的是可以被Gradle读取的环境变量。

英文:

I have a gradle project in Eclipse that reads environment variables at runtime. I can successfully create a jar, put export MY_VAL=7 in the shell and then execute the jar. But I want to run the project from within eclipse, so I need to put the environment variables somewhere in the run configuration.
For an ordinary Java project this is straightforward:

如何在Eclipse Gradle项目的运行配置中设置环境变量?

But in gradle there is no such tab:

如何在Eclipse Gradle项目的运行配置中设置环境变量?

How can I specify the environment variables for execution time?

Preemptive disambiguation:
https://stackoverflow.com/q/50860982/3014199 has a very similar title but actually asks for environment variables that can be read by gradle

答案1

得分: 2

> 对于普通的Java项目来说,这很简单:

是的,因为在普通的Java项目中,Eclipse会运行你的应用程序。但在Gradle项目中,Eclipse会运行Gradle,然后Gradle再运行你的应用程序。如果你只是想告诉Gradle如何运行你的应用程序,就没必要告诉Eclipse应该如何告诉Gradle运行你的应用程序。

使用Gradle,关于项目的所有相关步骤,比如编译代码、运行测试、创建JAR文件或者运行生成的应用程序,都应该由Gradle来处理。主要优势在于所有这些步骤都可以独立于Eclipse(或任何其他IDE)执行。但如果它们应该独立于Eclipse执行,那么在Eclipse内部配置它们就没有意义。相反,你应该配置你的构建脚本。

由于你没有在问题中添加你的build.gradle文件,所以我只能猜测它的样子,但可能你正在使用Gradle应用程序插件及其run任务。run任务属于JavaExec类型,因此提供了一个environment方法来定义环境变量:

run {
    environment 'MY_VAL', '7'
}

从现在开始,你可以从Eclipse中调用run任务(甚至可以从命令行使用gradle rungradlew run)以带有环境变量MY_VAL来运行你的应用程序。

英文:

> For an ordinary Java project this is straightforward:

Yes, because in an ordinary Java project, Eclipse runs your application. But in a Gradle project, Eclipse runs Gradle and Gradle runs your application. There is no need to tell Eclipse how it should tell Gradle to run your application, if you just may tell Gradle how it should run your application.

Using Gradle, all relevant steps regarding the work on your project, e.g. to compile the code, to run tests, to create a JAR or to run the resulting application, should be handled by Gradle. The major advantage is that all these steps may be performed independent from Eclipse (or any other IDE). But if they should be performed independent from Eclipse, there is no point in configurating them inside Eclipse. Instead, you should configure your build script.

Since you did not add your build.gradle to your question, I can only guess how it looks like, but probably you are using the Gradle Application plugin with its task run. The task run is of type JavaExec and therefore provides a method environment to define an environment variable:

run {
    environment 'MY_VAL', '7'
}

From now on, you may just call the task run from Eclipse (or even from command line using gradle run or gradlew run) to run your application with the environment variable MY_VAL.

答案2

得分: 0

根据您所使用的库和框架,有不同的方法,但对于一个简单的应用程序,您可以创建一个属性文件,在应用程序启动时加载该文件。您可以为开发/生产/等情况提供单独的属性文件,并根据构建环境加载它们。

阅读这篇文章,了解简要概述:
https://medium.com/@ubuntudroid/handling-environment-variables-in-gradle-fb1b8bb6c758

编辑
您可以使用这种属性文件的方法将变量加载到Gradle构建过程或您的Java应用程序中。Gradle的Java插件只允许您为Gradle任务指定环境变量,而不是您的实际应用程序。

英文:

There are different approached depending on the libraries and framework you are working in, but for a simple application you could create a properties file which you load in your application startup. You could provide separate properties files for dev/prod/etc. and load them depending on your build environment.

Check out this article for a brief overview:
https://medium.com/@ubuntudroid/handling-environment-variables-in-gradle-fb1b8bb6c758

EDIT
You can use this properties file approach to load variables into either the gradle build process or your java application. Gradle's java plugin only allows you to specify environment variables for gradle tasks, not your actual application.

huangapple
  • 本文由 发表于 2020年9月1日 04:01:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/63677443.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定