Exec / execute in Gradle doesn't use PATH?

(Robert Stroud) #1


I’m using Gradle on Mac OS X and running Unix commands from my build script using execute and exec. However, Gradle doesn’t seem to be picking up my PATH setting - in particular, it’s not finding commands in /usr/local/bin unless I specify the full pathname.

I’d prefer not to specify a full pathname for every command - why is Gradle not picking up the PATH variable from my environment? How do I change the default PATH in Gradle?



(James Justinic) #2

Gradle inherits and passes on the environment from the JVM’s System.getenv() call. These should match the environment that was available to the JVM process. To troubleshoot, it may help to see if env reports the PATH value that you expect when executing in the same environment. You can use this task or the same value in project.exec { }.

task environment(type: Exec) {
    commandLine 'env'

You can’t change the default PATH, but you can add / replace it to be whatever you want for a given exec. For example, adding two paths:

project.exec {
    environment 'PATH', "${environment.PATH}:/opt/random-stuff:/opt/other-random-stuff"

or replacing everything with a new location:

project.exec {
    environment 'PATH', '/opt/my-new-bin'

(Robert Stroud) #3

Dear James,

Thank you for your reply - I created an environment task as you suggested and discovered that the problem was with my IDE rather than Gradle.

The behaviour you describe for Gradle is exactly what I would expect and exactly how Gradle behaves if I invoke it from a terminal window. However, if I invoke Gradle from within my IDE, the PATH is different…

This is because I’m starting my IDE from the Mac OS X Dock rather than from a Unix shell.

In order for my IDE to pick up the PATH from my environment, I need to start it from a terminal window like this:

open -a 'IntelliJ IDEA

This is a known problem - see for example:


In principle, I should be able to fix this by configuring my IDE to run Gradle tasks with a particular PATH, but apparently, IDEA invokes Gradle via the Tool API, which didn’t allow you to specify a PATH prior to Gradle 3.5:


It’s not obvious whether the current version of IDEA provides a way of configuring the PATH for Gradle tasks, so invoking IDEA using open -a seem to be the best solution for now…

I hope this additional information is helpful to someone.

Best wishes,


(Robert Stroud) #4

If you upgrade to Gradle 3.5, you can configure the PATH for Gradle in IDEA via Run / Edit Configurations / Defaults / Gradle