【问题标题】:Simple gradle build file build error简单的gradle构建文件构建错误
【发布时间】:2012-07-08 06:40:52
【问题描述】:

我正在尝试几个 gradle 基础知识。 这是我的 gradle 文件“build.gradle”的样子:

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

这会导致以下错误:

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.

如果我像这样对构建文件进行小修改

[请注意,我已将括号从第二行移至 第一行]

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

我看到了输出

Hello World!

没有问题。

括号在gradle中是个大问题吗?把括号放在第二行是我做错了什么?

【问题讨论】:

    标签: build gradle


    【解决方案1】:

    与使用分号推断的其他语言一样,换行符在 Groovy 中有所不同。第一个 sn-p 被解析为 task hello; { ... },这是不明确的(无法确定第二个语句是块还是闭包),因此 Groovy 语法无效。无论如何,这不是您想要的。您希望闭包与hello 任务相关联。为避免此类意外,我建议遵循 Java 大括号样式。

    【讨论】:

      猜你喜欢
      • 2015-07-02
      • 2016-10-21
      • 1970-01-01
      • 2016-05-08
      • 2015-06-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多