【问题标题】:Is there a way to get the version of a dependency from within build.gradle?有没有办法从 build.gradle 中获取依赖项的版本?
【发布时间】:2021-02-07 09:52:30
【问题描述】:

这是我的问题,我正在根据项目所在的区域提取 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,但我特别需要在任务中自动执行此操作。

【问题讨论】:

    标签: gradle build dependencies build.gradle gradlew


    【解决方案1】:

    看起来您只想找到已解析 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
    

    【讨论】:

    • 这应该足以让我开始,非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-08
    • 2020-12-03
    • 1970-01-01
    • 2017-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多