This message was deleted.
# community-support
s
This message was deleted.
e
buildscript
,
initscript
,
plugins
, and
pluginManagement
are all special blocks that extracted and run separately, before any other part of the script. this is necessary because Gradle needs to use them to set up the build environment for the rest of the script. you cannot use anything defined outside of the block, in these blocks.
a
Thanks
How then can I define a method within the initscript?
in order to reduce code duplication
e
you cannot share anything between the special blocks as they run in a separate context
a
i'm talking about within the initscript alone
something like
Copy code
initscript{
    def hi() {
        println "hi"
    }
    hi()
    hi()
}
does not work
i get the following error:
Copy code
FAILURE: Build failed with an exception.

* Where:
Initialization script '/Users/aloneitan/.gradle/init.gradle' line: 1

* What went wrong:
Could not compile initialization script '/Users/aloneitan/.gradle/init.gradle'.
> startup failed:
  initialization script '/Users/aloneitan/.gradle/init.gradle': 1: Unexpected input: '{' @ line 1, column 11.
     initscript{
               ^
  
  1 error
I don't need to share a single method between, say
initscript
and
plugins
. just keep it within
initiscript
e
try adding a space? I'm not sure, might be a quirk of the Groovy DSL (seems to work fine for me in Kotlin)
a
add a space where?
could this then be a bug in Groovy?
e
initscript{
versus
initscript {
a
Copy code
FAILURE: Build failed with an exception.

* Where:
Initialization script '/Users/aloneitan/.gradle/init.gradle' line: 2

* What went wrong:
Could not compile initialization script '/Users/aloneitan/.gradle/init.gradle'.
> startup failed:
  initialization script '/Users/aloneitan/.gradle/init.gradle': 2: Method definition not expected here. Please define the method at an appropriate place or perhaps try using a block/Closure instead. at line: 2 column: 5. File: _BuildScript_ @ line 2, column 5.
         def hi() {
         ^
  
  1 error
e
Gradle's pre-parsing is a bit of a heuristic and you shouldn't do anything out of the ordinary
I guess it doesn't work in Groovy then
a
🤦
well, thanks anyway