简体   繁体   中英

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

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:
Eclipse: How to set env variables for Gradle run has a very similar title but actually asks for environment variables that can be read by gradle

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, eg 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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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