英文:
How to set Environment variables in Run Configuration of an eclipse gradle project?
问题
我在Eclipse中有一个Gradle项目,它在运行时读取环境变量。我可以成功地创建一个JAR文件,然后在Shell中输入export MY_VAL=7
,然后执行这个JAR文件。但是我想要从Eclipse内部运行这个项目,所以我需要在运行配置中的某个地方设置这些环境变量。
对于普通的Java项目来说,这是很简单的:
但是在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:
But in gradle there is no such tab:
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 run
或gradlew 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论