Gangmax Blog

Introspect Gradle Script

It’s difficult to figure out how a Gradle script works as a pure Groovy script. Such as, where are the “buildscript/task/project/allprojects/subprojects/dependencies” functions defined, if they are function calls really?

From here I know: by default the following classes are imported into a Gradle build script:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import org.gradle.*
import org.gradle.api.*
import org.gradle.api.artifacts.*
import org.gradle.api.artifacts.cache.*
import org.gradle.api.artifacts.component.*
import org.gradle.api.artifacts.dsl.*
import org.gradle.api.artifacts.ivy.*
import org.gradle.api.artifacts.maven.*
import org.gradle.api.artifacts.query.*
import org.gradle.api.artifacts.repositories.*
import org.gradle.api.artifacts.result.*
import org.gradle.api.artifacts.transform.*
import org.gradle.api.attributes.*
import org.gradle.api.component.*
import org.gradle.api.credentials.*
import org.gradle.api.distribution.*
import org.gradle.api.distribution.plugins.*
import org.gradle.api.dsl.*
import org.gradle.api.execution.*
import org.gradle.api.file.*
import org.gradle.api.initialization.*
import org.gradle.api.initialization.dsl.*
import org.gradle.api.invocation.*
import org.gradle.api.java.archives.*
import org.gradle.api.logging.*
import org.gradle.api.logging.configuration.*
import org.gradle.api.plugins.*
import org.gradle.api.plugins.announce.*
import org.gradle.api.plugins.antlr.*
import org.gradle.api.plugins.buildcomparison.gradle.*
import org.gradle.api.plugins.jetty.*
import org.gradle.api.plugins.osgi.*
import org.gradle.api.plugins.quality.*
import org.gradle.api.plugins.scala.*
import org.gradle.api.publish.*
import org.gradle.api.publish.ivy.*
import org.gradle.api.publish.ivy.plugins.*
import org.gradle.api.publish.ivy.tasks.*
import org.gradle.api.publish.maven.*
import org.gradle.api.publish.maven.plugins.*
import org.gradle.api.publish.maven.tasks.*
import org.gradle.api.publish.plugins.*
import org.gradle.api.reporting.*
import org.gradle.api.reporting.components.*
import org.gradle.api.reporting.dependencies.*
import org.gradle.api.reporting.dependents.*
import org.gradle.api.reporting.model.*
import org.gradle.api.reporting.plugins.*
import org.gradle.api.resources.*
import org.gradle.api.specs.*
import org.gradle.api.tasks.*
import org.gradle.api.tasks.ant.*
import org.gradle.api.tasks.application.*
import org.gradle.api.tasks.bundling.*
import org.gradle.api.tasks.compile.*
import org.gradle.api.tasks.diagnostics.*
import org.gradle.api.tasks.incremental.*
import org.gradle.api.tasks.javadoc.*
import org.gradle.api.tasks.scala.*
import org.gradle.api.tasks.testing.*
import org.gradle.api.tasks.testing.junit.*
import org.gradle.api.tasks.testing.testng.*
import org.gradle.api.tasks.util.*
import org.gradle.api.tasks.wrapper.*
import org.gradle.authentication.*
import org.gradle.authentication.aws.*
import org.gradle.authentication.http.*
import org.gradle.buildinit.plugins.*
import org.gradle.buildinit.tasks.*
import org.gradle.caching.*
import org.gradle.external.javadoc.*
import org.gradle.ide.visualstudio.*
import org.gradle.ide.visualstudio.plugins.*
import org.gradle.ide.visualstudio.tasks.*
import org.gradle.ivy.*
import org.gradle.jvm.*
import org.gradle.jvm.application.scripts.*
import org.gradle.jvm.application.tasks.*
import org.gradle.jvm.platform.*
import org.gradle.jvm.plugins.*
import org.gradle.jvm.tasks.*
import org.gradle.jvm.tasks.api.*
import org.gradle.jvm.test.*
import org.gradle.jvm.toolchain.*
import org.gradle.language.assembler.*
import org.gradle.language.assembler.plugins.*
import org.gradle.language.assembler.tasks.*
import org.gradle.language.base.*
import org.gradle.language.base.artifact.*
import org.gradle.language.base.plugins.*
import org.gradle.language.base.sources.*
import org.gradle.language.c.*
import org.gradle.language.c.plugins.*
import org.gradle.language.c.tasks.*
import org.gradle.language.coffeescript.*
import org.gradle.language.cpp.*
import org.gradle.language.cpp.plugins.*
import org.gradle.language.cpp.tasks.*
import org.gradle.language.java.*
import org.gradle.language.java.artifact.*
import org.gradle.language.java.plugins.*
import org.gradle.language.java.tasks.*
import org.gradle.language.javascript.*
import org.gradle.language.jvm.*
import org.gradle.language.jvm.plugins.*
import org.gradle.language.jvm.tasks.*
import org.gradle.language.nativeplatform.*
import org.gradle.language.nativeplatform.tasks.*
import org.gradle.language.objectivec.*
import org.gradle.language.objectivec.plugins.*
import org.gradle.language.objectivec.tasks.*
import org.gradle.language.objectivecpp.*
import org.gradle.language.objectivecpp.plugins.*
import org.gradle.language.objectivecpp.tasks.*
import org.gradle.language.rc.*
import org.gradle.language.rc.plugins.*
import org.gradle.language.rc.tasks.*
import org.gradle.language.routes.*
import org.gradle.language.scala.*
import org.gradle.language.scala.plugins.*
import org.gradle.language.scala.tasks.*
import org.gradle.language.scala.toolchain.*
import org.gradle.language.twirl.*
import org.gradle.maven.*
import org.gradle.model.*
import org.gradle.nativeplatform.*
import org.gradle.nativeplatform.platform.*
import org.gradle.nativeplatform.plugins.*
import org.gradle.nativeplatform.tasks.*
import org.gradle.nativeplatform.test.*
import org.gradle.nativeplatform.test.cunit.*
import org.gradle.nativeplatform.test.cunit.plugins.*
import org.gradle.nativeplatform.test.cunit.tasks.*
import org.gradle.nativeplatform.test.googletest.*
import org.gradle.nativeplatform.test.googletest.plugins.*
import org.gradle.nativeplatform.test.plugins.*
import org.gradle.nativeplatform.test.tasks.*
import org.gradle.nativeplatform.toolchain.*
import org.gradle.nativeplatform.toolchain.plugins.*
import org.gradle.platform.base.*
import org.gradle.platform.base.binary.*
import org.gradle.platform.base.component.*
import org.gradle.platform.base.plugins.*
import org.gradle.play.*
import org.gradle.play.distribution.*
import org.gradle.play.platform.*
import org.gradle.play.plugins.*
import org.gradle.play.plugins.ide.*
import org.gradle.play.tasks.*
import org.gradle.play.toolchain.*
import org.gradle.plugin.devel.*
import org.gradle.plugin.devel.plugins.*
import org.gradle.plugin.devel.tasks.*
import org.gradle.plugin.repository.*
import org.gradle.plugin.use.*
import org.gradle.plugins.ear.*
import org.gradle.plugins.ear.descriptor.*
import org.gradle.plugins.ide.api.*
import org.gradle.plugins.ide.eclipse.*
import org.gradle.plugins.ide.idea.*
import org.gradle.plugins.javascript.base.*
import org.gradle.plugins.javascript.coffeescript.*
import org.gradle.plugins.javascript.envjs.*
import org.gradle.plugins.javascript.envjs.browser.*
import org.gradle.plugins.javascript.envjs.http.*
import org.gradle.plugins.javascript.envjs.http.simple.*
import org.gradle.plugins.javascript.jshint.*
import org.gradle.plugins.javascript.rhino.*
import org.gradle.plugins.javascript.rhino.worker.*
import org.gradle.plugins.signing.*
import org.gradle.plugins.signing.signatory.*
import org.gradle.plugins.signing.signatory.pgp.*
import org.gradle.plugins.signing.type.*
import org.gradle.plugins.signing.type.pgp.*
import org.gradle.process.*
import org.gradle.process.daemon.*
import org.gradle.testing.base.*
import org.gradle.testing.base.plugins.*
import org.gradle.testing.jacoco.plugins.*
import org.gradle.testing.jacoco.tasks.*
import org.gradle.testkit.runner.*
import org.gradle.util.*

The document says:

To make build scripts more concise, Gradle automatically adds a set of import statements to the Gradle scripts.

Not sure if they hope anyone understand how “build.gradle” really works.

However, I find the following script can introspect the instances inside the Gradle script:

Refer here, here and here.

build.gradle
1
2
3
4
5
6
7
8
task introspect() {
println "task class: ${introspect.class}"
println introspect.dump().class
println introspect.dump()
for(method in introspect.metaClass.methods) {
println method
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# Run the "build.gradle" script:
~/temp> gradle -q introspect
task class: class org.gradle.api.DefaultTask_Decorated
class java.lang.String
<org.gradle.api.DefaultTask_Decorated@63259e68 __meta_class__=groovy.lang.MetaClassImpl@68c159ff[class org.gradle.api.DefaultTask_Decorated] project=root project 'temp' name=introspect actions=[] path=:introspect enabled=true dependencies=org.gradle.api.internal.tasks.DefaultTaskDependency@6143523b mustRunAfter=org.gradle.api.internal.tasks.DefaultTaskDependency@6842d1a3 finalizedBy=org.gradle.api.internal.tasks.DefaultTaskDependency@275bd41d shouldRunAfter=org.gradle.api.internal.tasks.DefaultTaskDependency@605d6181 extensibleDynamicObject=DynamicObject for task ':introspect' description=null group=null onlyIfSpec=org.gradle.api.specs.AndSpec@c3ad0576 executer=null services=ProjectScopeServices state=org.gradle.api.internal.tasks.TaskStateInternal@6545302f validators=[] taskMutator=org.gradle.api.internal.tasks.TaskMutator@1cc7eba6 observableActionList=[] impliesSubProjects=false hasCustomActions=false toStringValue=task ':introspect' identityPath=:introspect taskInputs=org.gradle.api.internal.tasks.DefaultTaskInputs@179dc0d6 taskOutputs=org.gradle.api.internal.tasks.DefaultTaskOutputs@2d7af2e3 publicType=class org.gradle.api.DefaultTask loggingManager=null>
public boolean java.lang.Object.equals(java.lang.Object)
public final native java.lang.Class java.lang.Object.getClass()
public native int java.lang.Object.hashCode()
public final native void java.lang.Object.notify()
public final native void java.lang.Object.notifyAll()
public java.lang.String java.lang.Object.toString()
public final void java.lang.Object.wait() throws java.lang.InterruptedException
public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
public void org.gradle.api.internal.AbstractTask.addValidator(org.gradle.api.internal.tasks.execution.TaskValidator)
public void org.gradle.api.internal.AbstractTask.appendParallelSafeAction(org.gradle.api.Action)
public int org.gradle.api.internal.AbstractTask.compareTo(org.gradle.api.Task)
public org.gradle.api.Task org.gradle.api.internal.AbstractTask.configure(groovy.lang.Closure)
public org.gradle.api.Task org.gradle.api.internal.AbstractTask.deleteAllActions()
public org.gradle.api.Task org.gradle.api.internal.AbstractTask.dependsOn(java.lang.Object[])
public boolean org.gradle.api.internal.AbstractTask.dependsOnTaskDidWork()
public org.gradle.api.Task org.gradle.api.internal.AbstractTask.doFirst(groovy.lang.Closure)
public org.gradle.api.Task org.gradle.api.internal.AbstractTask.doFirst(org.gradle.api.Action)
public org.gradle.api.Task org.gradle.api.internal.AbstractTask.doLast(groovy.lang.Closure)
public org.gradle.api.Task org.gradle.api.internal.AbstractTask.doLast(org.gradle.api.Action)
public final void org.gradle.api.internal.AbstractTask.execute()
public org.gradle.api.Task org.gradle.api.internal.AbstractTask.finalizedBy(java.lang.Object[])
public java.util.Set org.gradle.api.internal.AbstractTask.getActionClassLoaders()
public java.util.List org.gradle.api.internal.AbstractTask.getActions()
public org.gradle.api.AntBuilder org.gradle.api.internal.AbstractTask.getAnt()
public org.gradle.internal.metaobject.DynamicObject org.gradle.api.internal.AbstractTask.getAsDynamicObject()
public org.gradle.api.plugins.Convention org.gradle.api.internal.AbstractTask.getConvention()
public java.util.Set org.gradle.api.internal.AbstractTask.getDependsOn()
public java.lang.String org.gradle.api.internal.AbstractTask.getDescription()
public boolean org.gradle.api.internal.AbstractTask.getDidWork()
public boolean org.gradle.api.internal.AbstractTask.getEnabled()
public org.gradle.api.internal.tasks.TaskExecuter org.gradle.api.internal.AbstractTask.getExecuter()
public org.gradle.api.plugins.ExtensionContainer org.gradle.api.internal.AbstractTask.getExtensions()
public org.gradle.api.tasks.TaskDependency org.gradle.api.internal.AbstractTask.getFinalizedBy()
public java.lang.String org.gradle.api.internal.AbstractTask.getGroup()
public org.gradle.util.Path org.gradle.api.internal.AbstractTask.getIdentityPath()
public boolean org.gradle.api.internal.AbstractTask.getImpliesSubProjects()
public org.gradle.api.internal.TaskInputsInternal org.gradle.api.internal.AbstractTask.getInputs()
public org.gradle.api.logging.Logger org.gradle.api.internal.AbstractTask.getLogger()
public org.gradle.logging.LoggingManagerInternal org.gradle.api.internal.AbstractTask.getLogging()
public org.gradle.api.tasks.TaskDependency org.gradle.api.internal.AbstractTask.getMustRunAfter()
public java.lang.String org.gradle.api.internal.AbstractTask.getName()
public org.gradle.api.specs.Spec org.gradle.api.internal.AbstractTask.getOnlyIf()
public org.gradle.api.internal.TaskOutputsInternal org.gradle.api.internal.AbstractTask.getOutputs()
public java.lang.String org.gradle.api.internal.AbstractTask.getPath()
public org.gradle.api.Project org.gradle.api.internal.AbstractTask.getProject()
public org.gradle.api.tasks.TaskDependency org.gradle.api.internal.AbstractTask.getShouldRunAfter()
public org.gradle.logging.StandardOutputCapture org.gradle.api.internal.AbstractTask.getStandardOutputCapture()
public org.gradle.api.internal.tasks.TaskStateInternal org.gradle.api.internal.AbstractTask.getState()
public java.util.List org.gradle.api.internal.AbstractTask.getTaskActions()
public org.gradle.api.internal.tasks.TaskDependencyInternal org.gradle.api.internal.AbstractTask.getTaskDependencies()
public java.io.File org.gradle.api.internal.AbstractTask.getTemporaryDir()
public org.gradle.internal.Factory org.gradle.api.internal.AbstractTask.getTemporaryDirFactory()
public java.util.List org.gradle.api.internal.AbstractTask.getValidators()
public boolean org.gradle.api.internal.AbstractTask.hasProperty(java.lang.String)
public static org.gradle.api.Task org.gradle.api.internal.AbstractTask.injectIntoNewInstance(org.gradle.api.internal.project.ProjectInternal,java.lang.String,java.lang.Class,java.util.concurrent.Callable)
public boolean org.gradle.api.internal.AbstractTask.isEnabled()
public boolean org.gradle.api.internal.AbstractTask.isHasCustomActions()
public org.gradle.api.Task org.gradle.api.internal.AbstractTask.leftShift(groovy.lang.Closure)
public org.gradle.api.Task org.gradle.api.internal.AbstractTask.mustRunAfter(java.lang.Object[])
public void org.gradle.api.internal.AbstractTask.onlyIf(groovy.lang.Closure)
public void org.gradle.api.internal.AbstractTask.onlyIf(org.gradle.api.specs.Spec)
public void org.gradle.api.internal.AbstractTask.prependParallelSafeAction(org.gradle.api.Action)
public java.lang.Object org.gradle.api.internal.AbstractTask.property(java.lang.String) throws groovy.lang.MissingPropertyException
public void org.gradle.api.internal.AbstractTask.setActions(java.util.List)
public void org.gradle.api.internal.AbstractTask.setDependsOn(java.lang.Iterable)
public void org.gradle.api.internal.AbstractTask.setDescription(java.lang.String)
public void org.gradle.api.internal.AbstractTask.setDidWork(boolean)
public void org.gradle.api.internal.AbstractTask.setEnabled(boolean)
public void org.gradle.api.internal.AbstractTask.setExecuter(org.gradle.api.internal.tasks.TaskExecuter)
public void org.gradle.api.internal.AbstractTask.setFinalizedBy(java.lang.Iterable)
public void org.gradle.api.internal.AbstractTask.setGroup(java.lang.String)
public void org.gradle.api.internal.AbstractTask.setImpliesSubProjects(boolean)
public void org.gradle.api.internal.AbstractTask.setMustRunAfter(java.lang.Iterable)
public void org.gradle.api.internal.AbstractTask.setOnlyIf(groovy.lang.Closure)
public void org.gradle.api.internal.AbstractTask.setOnlyIf(org.gradle.api.specs.Spec)
public void org.gradle.api.internal.AbstractTask.setProperty(java.lang.String,java.lang.Object)
public void org.gradle.api.internal.AbstractTask.setShouldRunAfter(java.lang.Iterable)
public org.gradle.api.tasks.TaskDependency org.gradle.api.internal.AbstractTask.shouldRunAfter(java.lang.Object[])
public java.lang.String org.gradle.api.internal.AbstractTask.toString()
public void org.gradle.api.DefaultTask_Decorated.appendParallelSafeAction(groovy.lang.Closure)
public void org.gradle.api.DefaultTask_Decorated.description(java.lang.String)
public void org.gradle.api.DefaultTask_Decorated.didWork(boolean)
public void org.gradle.api.DefaultTask_Decorated.enabled(boolean)
public void org.gradle.api.DefaultTask_Decorated.executer(org.gradle.api.internal.tasks.TaskExecuter)
public groovy.lang.MetaClass org.gradle.api.DefaultTask_Decorated.getMetaClass()
public java.lang.Object org.gradle.api.DefaultTask_Decorated.getProperty(java.lang.String)
public void org.gradle.api.DefaultTask_Decorated.group(java.lang.String)
public boolean org.gradle.api.DefaultTask_Decorated.hasProperty(java.lang.String)
public void org.gradle.api.DefaultTask_Decorated.impliesSubProjects(boolean)
public java.lang.Object org.gradle.api.DefaultTask_Decorated.invokeMethod(java.lang.String,java.lang.Object)
public void org.gradle.api.DefaultTask_Decorated.prependParallelSafeAction(groovy.lang.Closure)
public void org.gradle.api.DefaultTask_Decorated.setMetaClass(groovy.lang.MetaClass)
public void org.gradle.api.DefaultTask_Decorated.setProperty(java.lang.String,java.lang.Object

Now at least I know something inside.

Comments