在gradle文件中指定多个依赖项

时间:2018-03-05 13:51:02

标签: gradle build.gradle

基于以下参考: https://www.petrikainulainen.net/programming/gradle/getting-started-with-gradle-dependency-management/

我相信我可以在依赖配置下指定多个依赖项,如下所示:

Multiple Dependencies under one dependency configuration

但是,当我尝试运行gradle build时,我看到以下错误。 enter image description here

这不是在gradle文件中提供多个依赖项的正确方法吗?

D:\TestGradle>gradle build
FAILURE: Build failed with an exception.
* Where:
Build file 'D:\TestGradle\build-ProblemTemplate.gradle' line: 116
* What went wrong:
Could not compile build file 'D:\TestGradle\build-ProblemTemplate.gradle'.
> startup failed:
  build file 'D:\TestGradle\build-ProblemTemplate.gradle': 116: expecting ')', found ',' @ line 116, column 68.
     toolVersions.mockitoVersion}'],
                                   ^
  1 error
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 1s
D:\TestGradle>

还尝试删除testCompile配置中的[],如下面的JakubWójcik的回答中所述。但仍然会得到同样的错误。

更新 令人惊讶的是,在依赖配置名称为我工作后删除换行符。 它与[]括号一起工作正常。仍然不知道为什么这很重要,但到目前为止用我的发现更新了帖子。

Working gradle dependency file.

2 个答案:

答案 0 :(得分:2)

您可以尝试使用每个属性的配置指定依赖项。我认为这是最常见和最常用的方式。

dependecies {
    compile 'dep1'
    compile 'dep2'
}

或者如果你真的想传入逗号分隔的args来编译闭包。

dependecies {
    compile (
        'dep1',
        'dep2'
    )
}

PS。使用$variables时,您需要使用GString "(双引号)。

PPS。您可以将vararg或数组传递给它并不重要的配置。

适用于新行

Gradle使用groovy作为其脚本,这实际上不是故意的错误,因为(){}可能被解释为编译器的单独块。请参阅Context-sensitive_grammar

在一天结束时它全部都是Groovy解析器:)

答案 1 :(得分:-1)

当testCompile子句具有多个依赖项时,请尝试删除[]括号。另请注意,第118行还有一个额外的"