Gradle-获取依赖工件的URL [英] Gradle - get URL of dependency artifact

查看:259
本文介绍了Gradle-获取依赖工件的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Gradle提供所有可用的依赖工件后,在将来手动下载依赖工件,因此我想获得Gradle用于下载这些工件的URL.

I want to download the dependency artifacts manually in the future after Gradle has all the dependency artifacts available, hence I would like to get the URLs which Gradle used to download those artifacts.

是否可以获取Gradle已下载工件的依赖项的URL?

Is there a way to get the URL of dependencies which artifacts have been downloaded by Gradle?

推荐答案

以gson为例:

dependencies {
    // https://mvnrepository.com/artifact/com.google.code.gson/gson
    compile 'com.google.code.gson:gson:2.8.6'
}

创建任务以打印url:

create a task to print url:

task getURLofDependencyArtifact() {
    doFirst {
        project.configurations.compile.dependencies.each { dependency ->
            for (ArtifactRepository repository : project.repositories.asList()) {
                def url = repository.properties.get('url')
                //https://repo.maven.apache.org/maven2/com/google/code/gson/gson/2.8.6/gson-2.8.6.jar
                def jarUrl = String.format("%s%s/%s/%s/%s-%s.jar", url.toString(),
                        dependency.group.replace('.', '/'), dependency.name, dependency.version,
                        dependency.name, dependency.version)
                try {
                    def jarfile = new URL(jarUrl)
                    def inStream = jarfile.openStream();
                    if (inStream != null) {
                        println(String.format("%s:%s:%s", dependency.group, dependency.name, dependency.version)
                                + " -> " + jarUrl)
                        return
                    }
                } catch (Exception ignored) {
                }
            }
        }
    }
}

运行./gradlew getURLofDependencyArtifact

任务:getURLofDependencyArtifact

Task :getURLofDependencyArtifact

com.google.code.gson:gson:2.8.6-> https://jcenter.bintray.com/com/google/code/gson/gson/2.8.6/gson-2.8.6.jar

com.google.code.gson:gson:2.8.6 -> https://jcenter.bintray.com/com/google/code/gson/gson/2.8.6/gson-2.8.6.jar

PS:结果依赖项目的

PS:the result dependency your project's

repositories {
    jcenter()
    mavenCentral()
}

所以,结果可能是:

任务:getURLofDependencyArtifact

Task :getURLofDependencyArtifact

com.google.code.gson:gson:2.8.6->

com.google.code.gson:gson:2.8.6 -> https://repo.maven.apache.org/maven2/com/google/code/gson/gson/2.8.6/gson-2.8.6.jar

这篇关于Gradle-获取依赖工件的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆