1

Below are two tasks hello & printProperties in build.gradle:

task('hello', {
            description("Hey student! Run this one :D")
            group("Our demo")
            doLast({println("Hello World!")})
            }
     )

plugins({
    id('java')
})

ext({
    springVersion = "3.1.0.RELEASE"
    emailNotification = "build@master.org"
})

sourceSets.all({ ext.purpose = null })

sourceSets({
    main({
        purpose = "production"
    })
    test({
        purpose = "test"
    })
    plugin({
        purpose = "production"
    })
})

task('printProperties', {
    doLast({
        println(springVersion)
        println(emailNotification)
        sourceSets.matching({ it.purpose == "production" }.each({ println it.name }))
    })
})

that gives error:

> startup failed:
  build file '/../../build.gradle': 8:
only buildscript {} and other plugins {} script blocks are allowed before plugins {} blocks, no other statements are allowed

Why plugins({id('java')}) give groovy scripted syntax error?

overexchange
  • 15,768
  • 30
  • 152
  • 347
  • 1
    Define the task after the plugins block? – tim_yates Dec 27 '19 at 23:54
  • @tim_yates why developers use declarative syntax? it is not readable...I could not understand single line of groovy code with declarative syntax. Really painful. Ya got resolved the above error. – overexchange Dec 27 '19 at 23:58
  • I don't understand what you're saying – tim_yates Dec 28 '19 at 00:15
  • I think what he is saying is that he finds the gradle dsl with parens and commas removed hard to read (vs the question where they are included). IMHO this is a function of how much time you have spent with a DSL. When you first start out you want all your commas and parens. Once you've lived with it for a few years, the less non-essential, not-contributing-to-the-information-content chars I have to parse mentally, the better. I'm also a clojure programmer. In clojure, everybody starts out hating the parens. Five years later I have not heard a single person dislike them. Just my 2c. – Matias Bjarland Dec 28 '19 at 10:21
  • @MatiasBjarland Spending time with DSL is same as to go far from regular programming languages (C/Python/java/GoLang/TyoeScript) that follow another kind of syntax that looks a bit close to scripted syntax of groovy. – overexchange Dec 28 '19 at 15:51

1 Answers1

1

It's answered here : https://stackoverflow.com/a/48611069/1250435

Whenever you write a build.gradle script and use the new plugins script block, you need to put it as first block in the file. The only exceptions from this rule are other plugins blocks or the special buildScript block, which always must go first.