Gradle override environment variables


I recently had some issues with gradle ignoring some environment variables under Linux.
I was trying to retrieve those variables with a System.getenv but the output I got, especially for the $CLASSPATH, were completly different.

I finally found out that in the bin/gradle there was the following assignation CLASSPATH=$APP_HOME/lib/gradle-launcher-5.6.2.jar. Obviously, the fix is to use this one instead CLASSPATH=$CLASSPATH:$APP_HOME/lib/gradle-launcher-5.6.2.jar to keep what is already in the classpath.

Is there a reason why gradle is erasing the variable instead of just adding what it needs in it? And if yes, what are the other options for the user to use the classpath? I use CLASSPATH to share its same value for different things and I don’t want to duplicate it.

Thank you

Just a guess, but probably because you shouldn’t mix class paths for different purposes.
Gradle needs exactly that JAR on the class path to run.
If there are other things on the class path for other reasons, this might make Gradle behave unexpected because suddenly some classes are taken from there, and it makes it slower as the class path in unnecessarily larger and so on.

You should not have any reason actually to modify the class path that Gradle is running with.
For your use-case I’d suggest you set MY_OWN_CLASSPATH that you then can also read from your build script and set your CLASSPATH to "$MY_OWN_CLASSPATH" for the other purposes where you use that, so that you don’t have to duplicate the information. Or you could also use MY_OWN_CLASSPATH to "$CLASSPATH".

But to be honest, it sounds like you are doing nasty things that you maybe should better not do. :slight_smile:


I use the classpath to indicate where to find the jars and .class it will need to run some comands. In my mind that is what classpath is for. And it looks like gradle as well as they are defining the path to their own jar :-).

But I am open to suggestions. In your opinion what would be the better way to share classpath between several executable? I my case, both gradle and bash script are executing commands that will require these classpath (actually my bash script are stand alone but can also be called by gradle). This classpath is loaded by a profile when my user logs in.

The only other options I found so far are :

  1. have all the dependencies duplicated in the gradle repo and add them as dependencies (really not great as it duplicates the jars on the server and force me to hardcode the dependencies in several places)
  2. explicitly export the classpath in every script that gradle could call to be sure that the dependencies are their (totaly loosing all the benefit from having them in a profile)

As a devops engineer, I really hate code duplication or hardcoded values and I am trying to figure out how those library can be used by the different layer without each one having its own declaration

I will try a custom variable tomorrow though, Thanks for the advice!

To be honest, it sounds to me a bit you are using Gradle for thing it is not designed for. :slight_smile:
But well, as it is just for your local specific case anyway, you could also just modify the gradlew script to add to the CLASSPATH variable instead of overwriting it. I’m just not sure whether that will help for what you try to do.