从Spring依赖管理插件中提取依赖版本

时间:2018-09-24 09:56:21

标签: spring spring-boot gradle build.gradle dependency-management

我有一个Gradle Task来从项目中提取依赖项并使用该数据。以下是我的Gradle Task

task gavValidation() {

    doLast {
        project(':app').configurations.each { 
            configurationType ->
            println "configurationType >>> "+configurationType.name
            configurationType.allDependencies.each {
                gav ->
                println gav.group+" : "+gav.name+" : "+gav.version
            }
        }
    }

}

print语句在上面打印null时总是以gav.version的形式出现。

我发现的是,这些依赖项的版本在Spring Dependency Management插件中维护。下面是代码段

apply plugin: 'io.spring.dependency-management'
    dependencyManagement {
        imports {
            mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Edgware.RELEASE'
            mavenBom 'io.pivotal.spring.cloud:spring-cloud-services-dependencies:1.5.0.RELEASE'
            mavenBom 'org.springframework.boot:spring-boot-dependencies:1.5.13.RELEASE'

        }
        dependencies {
            dependency 'io.springfox:springfox-swagger2:2.8.0'
            dependency 'io.springfox:springfox-swagger-ui:2.8.0'

        }
    }

如何在自定义任务中获取版本?当前为空

1 个答案:

答案 0 :(得分:1)

Working with Dependencies中所述,方法Configuration.getDependencies()Configuration.getAllDependencies()仅返回声明的依赖关系,而不会触发依赖关系解析。因此,对于来自Spring BOM的依赖关系,尚不知道该版本。

您可以改为使用Configuration.getResolvedConfiguration()方法,如下所示:

task gavValidation() {
    doLast {
        configurations.each { configurationType ->
            println " ***************** configurationType >>> " + configurationType.name
            if (configurationType.canBeResolved) {
                configurationType.getResolvedConfiguration().getResolvedArtifacts().each { artefact ->
                    ModuleVersionIdentifier id = artefact.getModuleVersion().getId()
                    println id.group + " : " + id.name + " : " + id.version
                }
            }
        }
    }
}
相关问题