Hey everyone. I'm trying to write a task in for Gr...
# community-support
j
Hey everyone. I'm trying to write a task in for Gradle 6.4.1 using Groovy 2.5.10. I'm trying a simple task:
Copy code
task hello {
    doLast {
        println "Hello world!"
    }
}
but it's failing with the following error:
Copy code
Could not find method task() for arguments [hello, test_36j2h73i78oarkgx47smjygs2$_run_closure1@6c6f8529] on build of type org.gradle.invocation.DefaultGradle.
What's going on? Won't Gradle 6.4.1 recognize closures? Any help or clue would be most appreciated. Thanks!
t
build of type org.gradle.invocation.DefaultGradle
looks strange here. Wouldn't the message be about
Project
in a
build.gradle
file? Are you trying this in a
settings.gradle
? (disclaimer: I haven't used the Groovy DSL for years)
j
@Thomas Broyer I'm trying this in an init script, it's the only thing I have on that script
The wrapper I'm using is using Groovy 2.5.10
t
Ah yes, init script indeed (settings script would be of type
Settings
) You cannot create a task from there, the
task()
method is only available on a `Project`: https://docs.gradle.org/6.4.1/dsl/org.gradle.api.Project.html#org.gradle.api.Project:task(java.lang.String,%20groovy.lang.Closure)
j
Ahh... Gotcha, thank you very much!
v
You can, but not just on top-level. The question more whether you should, but that depends on the situation.
j
I'm trying to create something to download project sources without having to modify the project build.gradle file. I have a plugin working, but only for newer gradle versions. I need it to work for older gradle versions too, so I'll need to come up with something
v
Well, as the docs for init scripts say, the context you have is https://docs.gradle.org/current/dsl/org.gradle.api.invocation.Gradle.html, there you find the things you could use, like
allrojects { ... }
if you need the task on all projects of the build and so on
j
I see, thank you @Vampire, it's pretty basic stuff, I'm not familiar with Gradle at all. Thanks
It makes sense