简单gradle这个build文件的生成错误 [英] Simple gradle build file build error

查看:1672
本文介绍了简单gradle这个build文件的生成错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了几个基本的gradle。
这是我的文件的gradle的build.gradle的样子:

 任务招呼
{
    doLast
    {
        的println世界,你好!
    }
}

这导致以下错误:

  D:\\ DevAreas \\学习-gradle这个> -q的gradle打招呼故障:建立失败,一个例外。*其中:
建立文件'D:\\ DevAreas \\学习,gradle这个\\的build.gradle'行:2* 什么地方出了错:
无法编译生成文件'D:\\ DevAreas \\学习,gradle这个\\的build.gradle。
>启动失败:
  构建文件'D:\\ DevAreas \\学习,gradle这个\\的build.gradle':2:暧昧的前pression可能是一个parameterle
SS关闭前pression,一个孤立的开放code座,也可以继续一个previous声明;
   解决方法:添加一个明确的参数列表,例如{它 - > ...},或强迫它被视为开放
通过给它的标签,例如方框L:{...},也或者删除previous换行,或添加EXP
合法分号; @ 2号线,1列。
     {
     ^  1个错误
*尝试:
与--stacktrace选项获取堆栈跟踪运行。与--info或--debug选项来获得更多的L RUN
OG输出。

如果我做一个小的修改,像这样构建文件


  

[请注意,我已经从第二行括号
  第一行]


 任务你好{
    doLast
    {
        的println世界,你好!
    }
}

我看到的输出

 世界,你好!

与出的问题。

时在括号的gradle这么大的问题?什么是我通过将括号中的第二行这样做不对吗?


解决方案

与使用分号推断其他语言,换行使得在Groovy的差异。第一个片段被解析为任务你好; {...} ,这是不明确的(不能决定是否第二条语句是块或闭包),因此无效的Groovy语法。这不是你想要的呢;你想关闭与你好任务有关。为了避免这样的惊喜,我建议遵循Java括号的风格。

I am trying out several gradle basics. Here is how my gradle file "build.gradle" looked:

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

This causes 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, e.g. {it -> ...}, or force it to be treated as an open
block by giving it a label, e.g. 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 minor modification to the build file like so

[Please NOTE that I have moved the parenthesis from second line to first line]

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

I see the output

Hello World!

with out issues.

Is parenthesis such a big problem in gradle? What was I doing so wrong by placing the parenthesis in the second line?

解决方案

As with other languages that use semicolon inference, the newline makes a difference in Groovy. The first snippet gets parsed as task hello; { ... }, which is ambiguous (can't decide if the second statement is a block or a closure) and hence invalid Groovy syntax. It's not what you want anyway; you want the closure to be associated with the hello task. To avoid such surprises, I recommend to follow the Java braces style.

这篇关于简单gradle这个build文件的生成错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆