How to know whether you are running inside a gradle daemon

拜拜、爱过 提交于 2019-12-10 03:10:05

问题


We have a situation where we take up a Jetty instance inside the VM that runs gradle.

However, this fails pretty badly when we are running inside a gradle daemon: We don't get rid of the Jetty instance totally, so it have to die with the gradle process itself. (However, that is not really of a big concern, since we do not want the gradle daemon in this CI integration tests case anyway).

So, we would like to know whether the current task is running inside a gradle daemon, or not - so that we can throw an exception or otherwise inform the user that this is the wrong approach, please run this un-daemonized.


回答1:


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" };



回答2:


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




回答3:


I wanted to know this from the context of a gradle plugin. After checking out the gradle source I eventually found the answer using:

val daemonScanInfo: DaemonScanInfo? = (project as DefaultProject).services.get(DaemonScanInfo::class.java)
val runningAsDaemon = !daemonScanInfo.isSingleUse

This had the benefit of being able to detect --no-daemon aswell as defining org.gradle.daemon=true|false. Doing project.findProperty("org.gradle.jvmargs") didn't catch --no-daemon on the command line.



来源:https://stackoverflow.com/questions/23265217/how-to-know-whether-you-are-running-inside-a-gradle-daemon

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!