2

我正在尝试做一些我觉得应该相对简单的事情,但互联网上似乎没有什么能给我我想要的东西。

基本上,我想从我的 compile/testCompile 依赖项中获取build.gradle(我不想要任何子依赖项 - 就像它们在文件中一样),并且对于每个我想用组名做某事的人,名称和版本。假设我想打印它们。

所以这是我的build.gradle

dependencies {
    compile 'org.spring.framework.cloud:spring-cloud-example:1.0.6'
    compile 'org.spring.framework.cloud:spring-cloud-other-example:1.1.6'
    testCompile 'org.spring.framework.cloud:spring-cloud-example-test:3.1.2'
}

task printDependencies {
    //some code in here to get results such as...
    // org.spring.framework.cloud  spring-cloud-other-example  1.1.6
}

谢谢大家。

4

1 回答 1

2

要遍历所有依赖项,您可以遍历所有配置和所有配置依赖项。像这样:

task printDependencies {
    project.configurations.each { conf ->
        conf.dependencies.each { dep ->
            println "${dep.group}:${dep.name}:${dep.version}"
        }
    }
}

如果您需要确切的配置依赖项,您可以单独获取它们:

task printDependencies {
    project.configurations.getByName('compile') { conf ->
        conf.dependencies.each { dep ->
            println "${dep.group}:${dep.name}:${dep.version}"
        }
    }

    project.configurations.getByName('testCompile') { conf ->
        conf.dependencies.each { dep ->
            println "${dep.group}:${dep.name}:${dep.version}"
        }
    }
}

或者修改第一个例子,通过添加条件来检查conf.name

于 2016-07-01T14:10:59.947 回答