gradle 任务配置

gradle configurations for tasks

我有 gradle 使用插件构建脚本:groovyjacocomaven

在 jars 旁边的依赖项是:

testCompile group: 'org.spockframework', name: 'spock-core', version: '0.7-groovy-2.0'

现在当我创建 task integTest(Type: Test) 和 :

configurations{
    integTestCompile {
        extendsFrom testCompile
    }
    integTestRuntime {
        extendsFrom integTestCompile
    }
}

一切正常,但我想添加一些模块化,现在所有 integTest 任务都是这样创建的:

task "${module}_${suite}" (type: Test)

当我尝试在配置中实施相同的更改时,出现错误:

Could not find method ModuleName_Suite1Runtime() for arguments [build_55s7pmm2c6k8n8e2n1i59t3b5b$_run_closure5_closure28_closure29_closure31@3394214b] on root project 'projectName'.

configurations {
            "${module}_${suite}Compile"{
                extendsFrom testCompile
            }
            "${module}_${suite}Runtime"{
                extendsFrom "${module}_${suite}Compile"
            }
 }

另一个配置不同的错误:

No signature of method: java.lang.String.extendsFrom() is applicable for argument types: (org.gradle.api.internal.artifacts.configurations.DefaultConfiguration_Decorated) values: [configuration ':testCompile']

configurations{
   "${module}_${suite}Compile".extendsFrom(testCompile)
   "${module}_${suite}Runtime".extendsFrom("${module}_${suite}Compile")
}   

没有“taskName+Compile”和“taskName+Runtime”,我得到了 Spock 规范的 ClassNotFound 异常。所以我确定我需要像以前版本一样的东西。

我很确定这很简单,但我在 google 中找不到任何提示。 任何帮助将不胜感激。

configurations {...} 块中使用的语法是通过一些 Groovy 魔术实现的,不幸的是,当您使用字符串文字时不会调用该魔术。相反,如果您想创建一个具有动态名称的配置,您需要在 ConfigurationsContainer.

上调用 create() 方法
configurations.create("${module}_${suite}Compile") {
    extendsFrom configurations.testCompile
}