Unable to set environmental variables for Gradle build

Answered

Trying to build my plugin with the "buildPlugin" task, however the environmental variables I am supplying are not getting picked by Gradle and when I run my plugin, installed from disk, my variables are empty.
With "runIde" everything works fine, but not with "buildPlugin".

Any idea?
Thanks

9 comments
Comment actions Permalink

Instead of environment variables, like myProperty, please use Gradle properties – they can be specified in the gradle.properties file, if not secret.

For more vulnerable data or properties that may change when running tasks, you can set such properties using environment variables prefixed with ORG_GRADLE_PROJECT_, like ORG_GRADLE_PROJECT_myProperty.

 

0
Comment actions Permalink

How can I get those variables in my application?

Since the environmental variables will get evaluated at runtime, how can I achieve this? How can I start my plugin supplying env variables?
I think what you suggested only works for build time, however in my case I need those variables at runtime, when my plugin is running.

Thanks

0
Comment actions Permalink

Jakub Chrzanowski hello, any help on this? Thanks

0
Comment actions Permalink

Jakub Chrzanowski Hello, could you please answer this question? Otherwise I will have to open a new thread or contact directly the support, I have been stuck for the past 10 days without any solution, thanks.

0
Comment actions Permalink

>Since the environmental variables will get evaluated at runtime, how can I achieve this? How can I start my plugin supplying env variables?
>I think what you suggested only works for build time, however in my case I need those variables at runtime, when my plugin is running.

You cannot use IDE run configuration if you want your plugin to supply the environment variables when you install it and when it is loaded into the IDE. Note that when IDE plugin is initialized - there is no Java Main method to run it - instead it loads into already initialized JVM which is started on IDE launch. I think the entire approach is wrong and you might need to reconsider it. What are you trying to solve with this?

0
Comment actions Permalink

Andrey Dernov it is a simple case of multiple environments build. I need some variables for a test environment and different ones for a production environment.
Since I need to test my plugin before release it, I am not really sure what other approach I can take to switch variables.

0
Comment actions Permalink

How do you use these variables exactly? In a Gradle build script? In your plugin source code?

If you want to pass environment variables to the Gradle daemon itself (use it in build script) please use gradle means for this (mentioned gradle.properties ) see https://docs.gradle.org/current/userguide/build_environment.html about different means of controlling build environment.

If you want to pass them to a process which is spawned by the Gradle task - use Gradle script for this. See this thread: https://stackoverflow.com/questions/36322536/how-to-set-an-environment-variable-from-a-gradle-build

0
Comment actions Permalink

Sorry forgot to mention. I am using it like this in my plugin code:

String myEnv = System.getenv("MY_ENV");

 

0
Comment actions Permalink

Hi Gioioso,

For that syntax I use either user specific environment variables - or global environment variables. 

 

I personally don't like the gradle.properties for any sensitive information as I quickly focus on code and check sensitive information into the source repository... which is an oops by my account.  Furthermore, using IntelliJ Run/Debug Configuration for these parameters are good per project, and/or when you run from within IntelliJ - which is not ideal if you find yourself reusing the same credentials over and over.

 

Having either a global or user specific environment variable means that it is shared between multiple projects - and simply puts the onus of protecting your login (which you have to do any way).  To confirm that my variables are working, I usually (with KTS Gradle script) have a task to print the environment...

 

tasks.register("printEnvironment") {
doLast {
System.getenv().forEach { t, u ->
println("$t -> $u")
}
println(System.getenv("SYSTEMX_USERNAME"))
println(System.getenv("SYSTEMX_PASSWORD"))
}
}

 

This is important as I have seen discrepancies in environment/operating system and JetBrains startup from different display managers, where it simply does not pick up my user variables (forcing me to put it globally etc)...

 

Hope that helps

Regards,

Gawie

 

 

 

0

Please sign in to leave a comment.