如何在build.gradle中通过编程方式从具有动态版本的依赖项中获取解析版本(静态版本)?

ssgvzors  于 2022-11-24  发布在  其他
关注(0)|答案(2)|浏览(87)

使用gradle时,我使用动态版本声明了一个依赖项

ext {
    fooLibVersion = [1.0.0, 1.0.999]   
}
dependencies {
    implementation "org.examble:foo-lib:$fooLibVersion"
}

现在,我想创建项目的javadoc,其中包含指向lib-foo javadoc的链接,为此,

javadoc {
    …
    options {
        links = [
            "https://example.org/foo-lib/releases/$fooLibVersion/javadoc/"
        ]
    }
}

然而,这当然会失败

javadoc: error - Malformed URL: https://example.org/foo-lib/releases/[1.0.0, 1.0.999]/javadoc/

如何通过编程方式获得foo-lib(使用动态版本声明)的解析版本IINM(也称为“静态版本”),并在javadoc链接参数的变量部分使用它?

lp0sw83n

lp0sw83n1#

Gradle似乎并没有提供一种现成的方法来实现这一点。例如(KotlinDSL),获取对实际依赖项的引用并不会产生解析的版本:

val commonsLangVersion by extra { listOf("3.9", "3.10") }

dependencies {
    implementation("org.apache.commons:commons-lang3:$commonsLangVersion")
}

configurations.compileClasspath.get().allDependencies.find { it.name == "commons-lang3" }?.let {
    println(it)
}

结果:

$ ./gradlew build

> Configure project :
DefaultExternalModuleDependency{group='org.apache.commons', name='commons-lang3', version='[3.9, 3.10]', configuration='default'}

因此,获取实际版本的唯一方法是解析配置,然后根据文件名执行一些子字符串操作:

val commonsLangVersion by extra { listOf("3.9", "3.10") }

dependencies {
    implementation("org.apache.commons:commons-lang3:$commonsLangVersion")
}

val resolvedVersion = configurations.compileClasspath.get().resolve()
        .filter { it.name.startsWith("commons-lang3") }[0] // commons-lang3-3.10.jar
        .name.substringAfterLast("-") // 3.10.jar
        .substringBeforeLast(".") // 3.10

println(resolvedVersion)

tasks.javadoc {
    options {
        (this as StandardJavadocDocletOptions)
        links = listOf(
                "https://commons.apache.org/proper/commons-lang/javadocs/api-$resolvedVersion/"
        )
    }
}
vnzz0bqm

vnzz0bqm2#

你应该解决工件,看下面的代码:

task myDepList {
    doLast {
        subprojects.each { subproject ->
            println "Project : ${subproject.name}"
            subproject.configurations.each { configuration -> 
                println "Configuration : ${subproject.name}#${configuration.name}"
                    if (configuration.canBeResolved) {
                        configuration.resolvedConfiguration.resolvedArtifacts.each { ra -> 
                            println "Resolved : ${subproject.name}#${ra.moduleVersion.id.group}:${ra.moduleVersion.id.name}:${ra.moduleVersion.id.version}"
                        }
                    }
                    configuration.allDependencies.each { dependency -> 
                        def type = []
                        if (dependency instanceof ExternalDependency) {
                            type.add "ExternalDependency"
                        }
                        if (dependency instanceof ClientModule) {
                            type.add "ClientModule"
                        }
                        if (dependency instanceof ExternalModuleDependency) {
                            type.add "ExternalModuleDependency"
                        }
                        if (dependency instanceof FileCollectionDependency) {
                            type.add "FileCollectionDependency"
                        }
                        if (dependency instanceof ModuleDependency) {
                            type.add "ModuleDependency"
                        }
                        if (dependency instanceof ProjectDependency) {
                            type.add "ProjectDependency"
                        }
                        if (dependency instanceof SelfResolvingDependency) {
                            type.add "SelfResolvingDependency"
                        }
                        println "Dependency ${type}: ${subproject.name}#${configuration.name}#${dependency.group}:${dependency.name}:${dependency.version}"
                        if (dependency instanceof ModuleDependency) {
                            dependency.artifacts.each { artifact -> 
                                println "        Artifact ${artifact.name}:${artifact.type}:${artifact.classifier}:${artifact.extension} => ${artifact.url}"
                            }
                        }
                    }
            }
        }
    }
}

相关问题