Gradle如何制作配置变量

时间:2017-08-02 06:39:29

标签: gradle configuration

Gradle:将配置foo定义如下:

task fooJar(type: Jar) {
    ...
}

configurations {
    foo.extendsFrom("compile")
}

artifacts {
    foo fooJar
}

如果配置名称是变量,如何写?例如,

['foo', 'bar'].each {

    task "${it}Jar"(type: Jar) {
       ....
    }

    configurations {
        "$it".extendsFrom("compile")
    }

    artifacts {
        "$it" tasks["${it}Jar"]
    }

}

回答后更新:

第二部分:添加神器:

artifacts.add(config, tasks["${it}Jar"])

错误:

> Could not find method add() for arguments [configuration 'foo', task 
'fooJar'] on object of type 
org.gradle.api.internal.artifacts.dsl.DefaultArtifactHandler.

1 个答案:

答案 0 :(得分:1)

ConfigurationContainer

返回的@see Project.getConfigurations()

例如

Configuration config = configurations.create(it)
config.extendsFrom configurations.getByName('xxx')