Gradle 解析多项目依赖项中的 ext 属性

Gradle resolve ext properties in depedencies for multi-project

我有以下多项目结构(只显示重要部分):

- project
    - build.gradle (file) 
    - settings.gradle (file)
    + module1 (dir)
    + module2 (dir)

build.gradle:

ext {
    groovyMajorVersion = 2.4
    groovyMinorVersion = 3
}
subprojects {
    dependencies {
        testCompile 'org.codehaus.groovy:groovy-all:${groovyMajorVersion}.${groovyMinorVersion}'
        testCompile 'org.spockframework:spock-core:1.0-groovy-${groovyMajorVersion}'
    }
}

settings.gradle:

include 'module1', 'module2'

当我从模块 1 运行 gradle dependencies 时,出现以下错误:

testCompile - Compile classpath for source set 'test'.
+--- org.codehaus.groovy:groovy-all:${groovyMajorVersion}.${groovyMinorVersion} FAILED
\--- org.spockframework:spock-core:1.0-groovy-${groovyMajorVersion} FAILED

这是因为在 dependencies 块中,您用撇号 (') 而不是引号 (") 包装依赖项,因此依赖项中的参数不会得到解析。要修复,只需将撇号替换为引号,即:

    testCompile "org.codehaus.groovy:groovy-all:${groovyMajorVersion}.${groovyMinorVersion}"
    testCompile "org.spockframework:spock-core:1.0-groovy-${groovyMajorVersion}"