Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle - get URL of dependency artifact

Tags:

gradle

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.

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

like image 971
Hans Avatar asked Jun 08 '16 08:06

Hans


2 Answers

use gson for a example:

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

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) {
                }
            }
        }
    }
}

run ./gradlew 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

PS:the result dependency your project's

repositories {
    jcenter()
    mavenCentral()
}

so, the result maybe:

Task :getURLofDependencyArtifact

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

like image 123
andforce Avatar answered Oct 20 '22 17:10

andforce


using Gradle version 6.0 or above, another way of outputting the URLs is to mix --refresh-dependencies with --info

// bash/terminal
./gradlew --info --refresh-dependencies
// cmd
gradlew --info --refresh-dependencies

or output to file

// bash/terminal
./gradlew --info --refresh-dependencies > urls.txt
// cmd
gradlew --info --refresh-dependencies > urls.txt

note on --refresh-dependencies

It’s a common misconception to think that using --refresh-dependencies will force download of dependencies. This is not the case: Gradle will only perform what is strictly required to refresh the dynamic dependencies. This may involve downloading new listing or metadata files, or even artifacts, but if nothing changed, the impact is minimal.

source: https://docs.gradle.org/current/userguide/dependency_management.html

see also: How can I force gradle to redownload dependencies?

like image 20
MikeM Avatar answered Oct 20 '22 17:10

MikeM