Skip to content

Instantly share code, notes, and snippets.

@matthiasbalke
Last active April 1, 2023 21:53
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save matthiasbalke/3c9ecccbea1d460ee4c3fbc5843ede4a to your computer and use it in GitHub Desktop.
Save matthiasbalke/3c9ecccbea1d460ee4c3fbc5843ede4a to your computer and use it in GitHub Desktop.
Gradle resolveDependencies Task
// found here: http://jdpgrailsdev.github.io/blog/2014/10/28/gradle_resolve_all_dependencies.html
task resolveDependencies {
doLast {
project.rootProject.allprojects.each { subProject ->
subProject.buildscript.configurations.each { configuration ->
resolveConfiguration(configuration)
}
subProject.configurations.each { configuration ->
resolveConfiguration(configuration)
}
}
}
}
void resolveConfiguration(configuration) {
if (isResolveableConfiguration(configuration)) {
configuration.resolve()
}
}
boolean isResolveableConfiguration(configuration) {
def nonResolveableConfigurations = ['apiElements', 'implementation',
'runtimeElements', 'runtimeOnly',
'testImplementation', 'testRuntimeOnly',
'generatedImplementation', 'generatedRuntimeOnly']
if (nonResolveableConfigurations.contains(configuration.getName())) {
return false
}
return true
}
@copitz
Copy link

copitz commented Aug 7, 2019

When you have other configurations this won't work - here's a solution working with any configuration using Configuration::isCanBeResolved:

task resolveDependencies(group: "build setup", description: "Resolve and prefetch dependencies") {
    doLast {
        def resolve = {
            ConfigurationContainer configurations ->
                configurations
                    .findAll({ Configuration c -> c.isCanBeResolved() })
                    .each({ c -> c.resolve() })
        }
        project.rootProject.allprojects.each { subProject ->
            resolve(subProject.buildscript.configurations)
            resolve(subProject.configurations)
        }
    }
}

@matthiasbalke
Copy link
Author

Hi @copitz, thx for letting me know. I didn't know about this alternative approach! Looks promising, I'll test it next time I need this. 👍

@dhasilin
Copy link

dhasilin commented Oct 30, 2019

Hi, @matthiasbalke what do you think about this one: https://stackoverflow.com/a/47107135/2906505 ? Any pitfalls?

@matthiasbalke
Copy link
Author

matthiasbalke commented Nov 6, 2019

Hi, @dhasilin in my opinion gradle dependencies does not download all dependencies for all subprojects, but I'm not sure. Maybe I didn't know about that task back in the days.

If you're curious just rename your ~/.gradle directory then use gradle dependencies. Afterwards rename it again and use my script. This way you can compare the outcome of the two methods by comparing the generated ~/.gradle directories.

@dirkbolte
Copy link

FYI: a Kotlin version of the code snippet of @copitz:

tasks.register<Task>(name = "resolveDependencies") {
    group = "Build Setup"
    description = "Resolve and prefetch dependencies"
    doLast {
        rootProject.allprojects.forEach {
            it.buildscript.configurations.filter(Configuration::isCanBeResolved).forEach { it.resolve() }
            it.configurations.filter(Configuration::isCanBeResolved).forEach { it.resolve() }
        }
    }
}

@matthiasbalke
Copy link
Author

@dirkbolte thanks for sharing this! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment