Adding local dependencies to contenerized application built by gradlew

22 Views Asked by At

I' working on an application that I run locally via Docker. Most important part of the Dockerfile is:

FROM eclipse-temurin:17 as source
COPY . /data
WORKDIR /data

FROM source as build
RUN ./gradlew clean build --info -x test

As you can see above jar is built by gradle wrapper that is present in the main application directory. Until now I worked only with central maven depndencies, so in my build.gradle.kts I had part like:

repositories {
    mavenCentral()
}

My current goal is to add some local dependencies. I added mavenLocal() to the above configuration and specify the dependency in dependencies part. It is possible for IntelliJ to add the jar to the External Libraries, but I have no idea how to build the application in the container. I tried to mount my local .m2/repository in docker-compose.yml as a volume like this:

volumes:
  - ./config:/config
  - ~/.m2/repository:/root/.m2/repository

And I can see that relevant jar is present in the container, but the ouput of the build process is still like this:

47.23 FAILURE: Build failed with an exception.
47.23 
47.23 * What went wrong:
47.23 Execution failed for task ':compileJava'.
47.23 > Could not resolve all files for configuration ':compileClasspath'.
47.23    > Could not find com.x:y:0.0.0-main-SNAPSHOT.
47.23      Required by:
47.23          project :

What is the part that I'm missing? Do I need to add something more to gradle settings to inform gradlew that it should look for the files in some specific place? I tried to replace mavenLocal() with maven(url = "/root/.m2/repository") (or maven(url = "~/.m2/repository") but it didn't work either. I run the build also with --info flag and the only logs relevant to this dependency were:

50.36 Resource missing. [HTTP GET: https://repo.maven.apache.org/maven2/com/x/y/0.0.0-main-SNAPSHOT/maven-metadata.xml]
50.36 Resource missing. [HTTP GET: https://repo.maven.apache.org/maven2/com/x/y/0.0.0-main-SNAPSHOT/y-0.0.0-main-SNAPSHOT.pom]

How can I deliver dependency from local maven repository to the build process?

1

There are 1 best solutions below

0
szczyzanski On

Problem was related not to gradle itself, but to Docker - volumes are mounted too late in the process, so gradle wasn't able to resolve the dependency, because .m2 was not there yet. I copied repositories to the main application directory and modified Dockerfile like this:

FROM eclipse-temurin:17 as source
COPY . /data
COPY ./repository /root/.m2/repository
WORKDIR /data

FROM source as build
COPY --from=source /root/.m2/repository /root/.m2/repository
RUN ./gradlew clean build -x test

and everything is working as expected.