Gradle 查找配置的所有子配置
在使用Gradle 查找配置的所有子配置,gradle,Gradle,在使用beforesolve添加依赖项时,它仅添加到指定的配置中,而不会添加到将该配置作为超级配置的配置中 /** * Get all Configurations that extend the provided configuration, including * the one that is specified. * * @param config the configuration to get all sub configurations for * @param p t
beforesolve
添加依赖项时,它仅添加到指定的配置中,而不会添加到将该配置作为超级配置的配置中
/**
* Get all Configurations that extend the provided configuration, including
* the one that is specified.
*
* @param config the configuration to get all sub configurations for
* @param p the project to get the configurations from
*
* @return set of all unique sub configurations of the provided configuration
*/
private static Set<String> getSubConfigs(final Configuration config, final Project p)
{
final Set<String> subConfs = new HashSet<>()
subConfs.add(config.name)
p.configurations.each {
if (getSuperConfigs(it, p).contains(config.name))
{
subConfs.add(it.name)
}
}
return subConfs
}
/**
* Get all super configurations for a given Configuration.
*
* @param config the configuration to get all super configurations for
* @param p the project to get the configurations from
*
* @return set of all unique super configurations of the provided configuration
*/
private static Set<String> getSuperConfigs(final Configuration config, final Project p)
{
final Set<String> superConfs = new HashSet<>()
superConfs.add(config.name)
config.extendsFrom.each {
superConfs.addAll(getSuperConfigs(p.configurations[it.name], p))
}
return superConfs
}
也就是说,当使用java
插件时,您有默认配置compile
、runtime
、testCompile
和testRuntime
。配置继承如下:
返回给定配置的超级配置集。您可以将给定配置的超级配置列表打印为compile
但是,它没有列出超级配置的超级配置(即递归列表)。但是,围绕这一点编写一段代码就可以给出完整的列表task printConfig { configurations.testRuntime.extendsFrom.each { println it.name } }
ext.configList = new HashMap<String, String>() def getSuperConfigs(config) { config.extendsFrom.each { configList.put(it.name, '1') getSuperConfigs(configurations[it.name]) } } task printConfig { getSuperConfigs(configurations['testRuntime']) configList.each { println it.key } }
我在寻找另一个方向(我想我的超级/次级术语是反向的)。使用这个代码示例,我能够构建我需要的东西。ext.configList=new HashMap() def getSuperConfigs(配置){ config.extendsFrom.each{ configList.put(it.name,'1') getSuperConfigs(配置[it.name]) } } 任务打印配置{ getSuperConfigs(配置['testRuntime']) configList.each{ println it.key } }