Fill up Gradles Dependency Cache

In a project we build our applications in ephemeral containers, therefore we lose all the artifacts of the dependencies for each build. Luckily there is a way to reuse the dependency cache by either copying or sharing the cache, as described in the manual here Learning the Basics.
We decided to use the sharing solution.

To keep the dependency cache up to date, as over time new dependencies could be defined, we want to automate filling up the dependency cache automatically, e.g: every night at a specific time.
We’re created our own BOM for the different dependencies.
In an empty Gradle project we just want to define this platform BOM and would like that Gradle will download all the dependency artifacts (including the transitive dependendencies) and fill up the dependency cache.

Is there a way, maybe over the Gradle API, to download all the artifacts and put it into the gradle cache?

Thanks for your help

Almost everything in a build script is using the Gradle API. The way to have them downloaded and put in the cache is to resolve them, just like any other build. Since you have a BOM, you mainly just need to translate the dependencies from the the dependency management section into actual dependencies and since you don’t need to do anything else in this project, force the resolution. Something like this should work as long as you take into account anything that you might need to handle specially for your BOM (repositories, exclusions, etc). You could also omit the version and use the built-in BOM support to handle the versions, if you want.

plugins {
    id 'java'
}

ext.bom = 'BOM COORDINATES HERE'

repositories {
    mavenCentral()
}

tasks.register('resolveDependencies') {
    doLast {
        def bomFile = configurations.detachedConfiguration(dependencies.create("${bom}@pom")).singleFile
        new XmlSlurper().parse(bomFile).dependencyManagement.dependencies.dependency.each { dependency ->
            dependencies {
                implementation "${dependency.groupId}:${dependency.artifactId}:${dependency.version}"
            }
        }
        configurations.runtimeClasspath.resolve()
    }
}


Thank you for your help. This is exactly what we want to achieve, just didn’t know how to do it. :+1:
Just out of interest, where does the special syntax @pom comes from? Is there a list of available options somewhere?

There’s nothing super special about @pom. Gradle supports dependencies as strings using the format:

group:name:version:classifier@extension

The @pom is just the file extension of the file that we want to resolve at those coordinates. For normal dependencies, there’s an artifact (such as the JAR) and the POM file.

In the case of resolving a published BOM to parse ourselves, the POM file is the actual file we want. We don’t want to Gradle to consume the POM and look for another artifact as it would in most cases.

This would work for any file extension that happens to be published in the maven repository structure, even those that typically wouldn’t be in a Maven repository.