I have trouble understanding the groovy syntax in gradle.
If named parameters (in groovy) are using the :
suffix, then I assume that the code apply plugin: 'java'
means to call the function apply(plugin = 'java')
. This is strange because the function apply
is not even defined. The following gives me an error in my gradle script:
println apply.getClass()
> Could not get unknown property 'apply' for root project 'Simple' of type
org.gradle.api.Project.
So what is apply
and where is it defined? Why doesn't the code above just print the class of the apply
element?
And one other thing that is strange to me is the following:
dependencies {
compile 'org.slf4j:slf4j-api:1.7.12'
testCompile 'junit:junit:4.12'
}
The syntax suggests that the code wrapped in {}
is a closure, but what are the compile
and testCompile
elements? If it was a closure, then the code above would just return 'junit:junit:4.12'
as a string and the rest should fail to compile. It looks like it's more a definition of a map. But again, if the code above is data, then I should be able to enter it at the groovysh
shell.
groovy:000> dependencies {
compile 'org.slf4j:slf4j-api:1.7.12'
testCompile 'junit:junit:4.12'
}
groovy:001> groovy:002> groovy:003> ERROR groovy.lang.MissingMethodException:
No signature of method: groovysh_evaluate.dependencies() is applicable for argument types: (groovysh_evaluate$_run_closure1) values: [groovysh_evaluate$_run_closure1@b7c4869]
This is confusing to me. I thought that gradle scripts are just groovy scripts, but it seems that the gradle DSL adds element to the groovy language. A groovy clojure becomes a map, a function call with named parameters becomes something different.
Can someone enlighten me on this groovy DSL ;)