3

Is a build.gradle file a syntactically valid Groovy script? If the right classes are in the class path, will it compile? For instance, suppose you have task hello{}. If I understand correctly, this creates a variable of type Task whose name is hello. But surely this is impossible in Groovy? Variables are declared with def. Why is this not failing with due to an undeclared identifier?

Mark VY
  • 1,489
  • 16
  • 31
  • can you provide an example for your question? and in groovy script you can assign to undeclared variables. – daggett Aug 02 '17 at 14:59
  • I did provide an example. `task hello{}`. Is this valid Groovy syntax? If so, how? – Mark VY Aug 02 '17 at 15:02
  • 4
    Possible duplicate of [Understanding the groovy syntax in a gradle task definition](https://stackoverflow.com/questions/27584463/understanding-the-groovy-syntax-in-a-gradle-task-definition) – tkruse Jan 29 '18 at 05:53
  • Hmm, yes, I suppose this was a duplicate after all. – Mark VY Jan 29 '18 at 19:47

1 Answers1

7

No, Gradle scripts are not valid Groovy scripts. Gradle is using a DSL that is based on Groovy. This among other things means there are AST transformers provided by Gradle that transform the provided DSL to valid Groovy code that is then compiled and executed.

Vampire
  • 35,631
  • 4
  • 76
  • 102
  • Ah nice. So the literal answer is no, but this kind of magic was part of Groovy's intended use case and they provided tools to make it easier than it would have normally been. – Mark VY Aug 02 '17 at 15:30
  • Exactly. Look at the linked resource, you find more information about DSLs and AST transformations there. – Vampire Aug 02 '17 at 15:31
  • Aha, nice! So Gradle probably loads an AST transformer that turns `task hello{}` into something more like `Task hello = new Task();`, or something. – Mark VY Aug 02 '17 at 15:34
  • Actually I think the produced result is something like `project.task('hello') { ... }`, but yes. – Vampire Aug 02 '17 at 15:41
  • Oh, that makes even more sense. Thanks! – Mark VY Aug 02 '17 at 15:41