Simple gradle file build error

I am testing a few basics of gradle. This is what my gradle file "build.gradle" looked like:

task hello { doLast { println 'Hello World!' } } 

This results in the following error:

 D:\DevAreas\learn-gradle>gradle -q hello FAILURE: Build failed with an exception. * Where: Build file 'D:\DevAreas\learn-gradle\build.gradle' line: 2 * What went wrong: Could not compile build file 'D:\DevAreas\learn-gradle\build.gradle'. > startup failed: build file 'D:\DevAreas\learn-gradle\build.gradle': 2: Ambiguous expression could be a parameterle ss closure expression, an isolated open code block, or it may continue a previous statement; solution: Add an explicit parameter list, eg {it -> ...}, or force it to be treated as an open block by giving it a label, eg L:{...}, and also either remove the previous newline, or add an exp licit semicolon ';' @ line 2, column 1. { ^ 1 error * Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more l og output. 

If I make a small modification to the assembly file, for example,

[Please note that I have moved the bracket from the second line to the first line]

 task hello{ doLast { println 'Hello World!' } } 

I see the conclusion

 Hello World! 

no problem.

Is brace such a big problem in gradle? What did I do wrong by putting the brackets on the second line?

+4
source share
1 answer

Like other languages ​​that use semicolon output, the new line makes a difference in Groovy. The first fragment is parsed as task hello; { ... } task hello; { ... } which is ambiguous (cannot decide whether the second statement is a block or closure) and therefore the Groovy syntax is invalid. This is not what you want; you want the closure to be associated with the hello task. To avoid such surprises, I recommend following the Java binding style.

+5
source

All Articles