【问题标题】:Pull Dependency version from Spring-dependency-management plugin从 Spring-dependency-management 插件中提取依赖版本
【发布时间】:2019-02-27 19:31:54
【问题描述】:

我有一个 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 语句在上面打印gav.version 时总是以null 出现。

我发现,这些依赖项的版本都在Spring Dependency Management Plugin 中维护。下面是sn-p

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'

        }
    }

如何在我的自定义任务中获取版本?目前为 null

【问题讨论】:

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


    【解决方案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
                    }
                }
            }
        }
    }
    

    【讨论】:

    • 太棒了!!谢谢
    猜你喜欢
    • 2017-06-11
    • 2021-05-18
    • 2014-07-26
    • 2015-10-13
    • 1970-01-01
    • 2021-10-08
    • 2020-04-04
    • 2019-12-22
    • 2014-11-25
    相关资源
    最近更新 更多