我有一个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'
}
}
如何在自定义任务中获取版本?当前为空
答案 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
}
}
}
}
}