How to know whether you are running inside a gradle daemon

Gradle names one of its thread "Daemon thread" so if you allow a hack, this could work:

def isDaemon = Thread.allStackTraces.keySet.any { it.name.contains "Daemon" };

Another solution would be to read the "sun.java.command" property.

If you are in the daemon the value for gradle 2.5 is

org.gradle.launcher.daemon.bootstrap.GradleDaemon 2.5

and if you are not the value is

org.gradle.launcher.GradleMain taskName

so a simple

if (System.properties.'sun.java.command'.contains('launcher.daemon')) {
  println 'Daemon is true'
} else {
  println 'Daemon is false'
}

would do the trick too