30

How can I list all the repositories configured for a project?

Background: I have a pretty complex gradle build script and cannot get my NetBeans to download the sources for maven dependencies. In that issue-report I was suggested to double check the order in which mavenCentral is being imported.

Alberto
  • 5,021
  • 4
  • 46
  • 69

3 Answers3

56

For anyone interested, here is the code to list the loaded repositories (thanks @kelemen):

task listrepos {
    doLast {
        println "Repositories:"
        project.repositories.each { println "Name: " + it.name + "; url: " + it.url }
   }
}

After adding this code to the build script, execute gradle listrepos and voilà...

Thunderforge
  • 19,637
  • 18
  • 83
  • 130
Alberto
  • 5,021
  • 4
  • 46
  • 69
  • In addition, if you want to, for example, check if credentials are correctly set, you can print `it.credentials.username` and `it.credentials.password` – froblesmartin Oct 13 '20 at 10:38
  • And for those of you who are fatigued from whatever this is intended to troubleshoot, don't forget you need to use gradle like you would for building (e.g. `-b path/to/build.gadle`, `./gradlew` and whatnot. – Sridhar Sarnobat Aug 30 '22 at 21:53
11

If someone comes to this page looking for the Kotlin (build.gradle.kts) equivalent of @Alberto's answer, it would be done as follows:

tasks.register("listrepos") {
    doLast {
        println("Repositories:")
        project.repositories.map{it as MavenArtifactRepository}
            .forEach{
            println("Name: ${it.name}; url: ${it.url}")
        }
    }
}

Just as a heads up, the cast to a MavenArtifactRepository is required in the Kotlin version to get the url property. This could be different for you if you are not adding Maven Repositories.

Bwvolleyball
  • 2,593
  • 2
  • 19
  • 31
1

Trying to use Alberto's task from his answer I was getting the following error as in my case I had a Plugin repository defined:

No such property: url for class: org.gradle.plugin.use.internal.PluginDependencyResolutionServices$PluginArtifactRepository

To avoid this error I have changed the task logic a bit:

task listrepos {
    doLast {
        println "Repositories:"
        project.repositories.each {
            if (it.name == '__plugin_repository__Gradle Central Plugin Repository') {
                println "Name: " + it.name + "; url: " + it.url
            } else {
                println "Name: " + it.displayName
            }
        }
    }
}
froblesmartin
  • 1,527
  • 18
  • 25