Gradle download dependencies jar

Using Java 11, Gradle 6.7.1.
Having several subprojects inside root project.

I have a task to build a docker image, which would have all source code compiled, all dependencies would be downloaded inside docker image, all dependencies would be resolved.
But I do not want to run build and test during docker build.

I have following configuration in my Dockerfile:
RUN gradle mysubproject:getDeps
RUN gradle mysubproject:resolveDependencies
RUN gradle mysubproject:assemble

I have following gradle tasks in the mysubproject module:
task getDeps(type: Copy) {
from sourceSets.main.runtimeClasspath
into ‘runtime/’

    doFirst {
        ant.delete(dir: 'runtime')
        ant.mkdir(dir: 'runtime')
    }

    doLast {
        ant.delete(dir: 'runtime')
    }
}

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

When docker image is built and inside container I run:
gradle mysubproject:test

But I still see that process of resolving dependencies is doing again.

How to solve it?

This does not exactly solve what you asking but I thought I would share how I do this.

I used the docker compose gradle plugin here: https://github.com/avast/gradle-docker-compose-plugin
This gives you a few tasks such as composeBuild, composeUp, composeDown. For composeBuild, I have it depend on “classes” so only the source is built. I used spring so I also make sure the jar was built using bootJar.

composeBuild {
dependsOn(“assemble”) // create the jar first, with spring also runs bootJar
dependsOn(“bootJar”)
}

composeUp {
dependsOn(“bootJar”)
finalizedBy(“healthCheck”)
}

I setup the plugin like this:
dockerCompose {
projectName = dockerGroupName // Used so nested implementations can all find each other when started
useComposeFiles = listOf("./docker/docker-compose.yml")
removeOrphans = false // false so that it doesn’t remove orphans from other projects
captureContainersOutput = true // if true, prints output of all containers to Gradle output - very useful for debugging; default is false
captureContainersOutputToFile = project.file("$buildDir/docker/containerlog.txt") // sends output of all containers to a log file
captureContainersOutputToFiles = project.file("$buildDir/docker/") // sends output of all services to a dedicated log file in the directory specified, e.g. ‘web.log’ for service named ‘log’
composeLogToFile = project.file("$buildDir/docker/composeUpDownlog.txt")// redirect output of composeUp and composeDown tasks to this file; default is null (ouput is not redirected)
containerLogToDir = project.file("$buildDir/docker/containerlog.txt") // directory where composeLogs task stores output of the containers; default: build/containers-logs
buildAdditionalArgs = listOf("–build-arg", “ENV_NAME=containers,local”)
}