28 October 2014

I had a challenge recently that involved being able to run a set of Gradle tasks in a Docker container that did not have access to my artifact repository. The obvious solution is to use the --offline switch provided by Gradle, but this only works if you have all the artifacts need to run cached locally. When Docker builds its container, you can run any number of commands to populate it with whatever you need. I decided to create a simple Gradle task to walk through each project in the source that I had copied into the Docker container that would force resolve each declared dependency:

task resolveDependencies {
    doLast {
        project.rootProject.allprojects.each { subProject ->
            subProject.buildscript.configurations.each { configuration ->
                configuration.resolve()
            }
            subProject.configurations.each { configuration ->
                configuration.resolve()
            }
        }
    }
}

The Gradle task above walks through each configuration defined in each sub-project in the Gradle project and forces Gradle to "resolve" (download) the artifacts. Because it uses the rootProject, this task can be defined in any of the sub-projects. After running this task, the Docker container now has a primed Gradle cache that allows me to run the task in the container in offline mode!

comments powered by Disqus