有没有办法从 build.gradle 中获取依赖项的版本?

Is there a way to get the version of a dependency from within build.gradle?

这是我的问题,我正在根据项目所在的区域提取 latest.release:

if (System.getenv()["namespace"].contains("dev")) {
       compile("com.foo.bar.dev:library:latest.release")
   } else if (System.getenv()["namespace"].contains("test")) {
       compile("com.foo.bar.test:library:latest.release")
   } else {
       compile("com.foo.bar.other:library:latest.release")
   }

有没有一种方法可以解决以后专门要获取 jar 版本的行的这种依赖关系?

def zipFile = file("build/libs/foobar/library-${projectVersion}.jar")

感谢您的帮助,我发现最好的是命令行的 dependencyInsight,但我特别需要在任务中自动执行此操作。

您似乎只想找到已解析 JAR 的路径。如果我理解正确,那么你可以解析配置并过滤你想要的依赖项。例如,使用 commons-lang3:

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

val commonsLang3Jar = configurations.compileClasspath.get().filter { it.name.startsWith("commons-lang3") }.singleFile

println(commonsLang3Jar.absolutePath)

这会导致打印以下行:

~/.gradle/caches/modules-2/files-2.1/org.apache.commons/commons-lang3/3.11/68e9a6adf7cf8eb7e9d31bbc554c7c75eeaac568/commons-lang3-3.11.jar