Can a plugin detect if the wrapper is being used?

In an infomational message for a Gradle plugin, I’d like to show the user the tasks they need to run. I’d like to be able to say:

Run the 'abc' task to do x y z:
./gradlew abc

However, I’d also like to switch that message between ./gradlew or gradle depending on whether the wrapper is being used or not.

Is there a way for plugins to detect that the current build is using the wrapper?

I don’t think there is.
But actually imho any build not using the Gradle wrapper has a bug anyway.
So I’d recommend to just advise with gradlew and let the user figure out the correct startable.
You could also use some placeholder if you are concerned, like <gradle start script> or similar.
Actually, you would also need to differentiate where and how the executable is started which is most probably also not possible, because from Bash or similar (also on Windows e. g. when using Cygwin, WSL, or Git Bash) it has to be ./gradlew, from CMD it has to be gradlew, from PowerShell (also on Linux) it has to b .\gradlew.

Thanks @Vampire - I am inclined to agree with your encouragement towards using the wrapper via the messaging. I was planning a simple switch between ./gradlew and .\gradlew.bat based on operating system, but hadn’t considered the other environments like Bash on Windows or PowerShell on Linux.

I guess I was hoping for the Gradle equivalent of Bash’s $0 or $BASH_SOURCE, but on reflection, I think I’ll keep it simple.

1 Like