The statement isn't to be taken literally. A Gradle script cannot be run with plain Groovy, just like a Javascript snippet on a web page cannot be run in a plain JavaScript engine outside the browser.
GroovyConsole running build.gradle
-
30-06-2022 - |
Question
GradleWare's book - Building and Testing with Gradle - states the following in section 1.1: "Every Gradle build file is an executable Groovy script."
However, when I run a build.gradle file in GroovyConsole, it gives me this error:
groovy.lang.MissingMethodException: No signature of method: build.apply() is applicable for argument types: (java.util.LinkedHashMap) values: [[plugin:android-library]] Possible solutions: any(), any(groovy.lang.Closure), every(), every(groovy.lang.Closure), split(groovy.lang.Closure) at build.run(build.gradle:1)
It refers to line 1:
apply plugin: 'android-library'
In the build script:
apply plugin: 'android-library'
dependencies {
compile 'com.android.support:support-v4:18.0.+'
}
android {
compileSdkVersion 14
buildToolsVersion '17.0.0'
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
res.srcDirs = ['res']
}
}
}
So can you please clarify whether the book's statement is correct or wrong??
Thank you, Igor Ganapolsky
Solution
OTHER TIPS
It may be possible to add a task to a gradle build file to launch a Groovy Console, though, as described here:
http://piraguaconsulting.blogspot.com.br/2012/02/gradle-groovy-console.html
The relevant code snippet is:
task(console, dependsOn: 'classes', type: JavaExec) {
main = 'groovy.ui.Console'
classpath = sourceSets.main.runtimeClasspath
}
A gradle build file is strictly spoken not groovy. It is a scripting language, aka a DSL (domain specific language) written in groovy. Hence you need the gradle distribution to run any gradle file, see: https://gradle.org/install/