'Gitlab Registry dependency not resolved during mvn when using mounted cache?

I'm trying to optimize the download of maven dependencies in my gitlab CI/CD pipelines with docker.

Problem: when using the dependency:go-offline feature that is designed for that purpose, my dependencies from gitlab repository are not properly resolved anymore (all public dependencies are ok).

Dockerfile:

FROM maven:3.8.4-eclipse-temurin-11 as dependencies
COPY pom.xml .
COPY ci_settings.xml .
RUN --mount=type=cache,target=/root/.m2 mvn -B dependency:go-offline -s ci_settings.xml
#this works instead:
#RUN mvn -B dependency:resolve -s ci_settings.xml

ci_settings.xml simply sets the following property:

<property>
    <name>Job-Token</name>
    <value>${CI_JOB_TOKEN}</value>
</property>
            

.gitlab-ci.yml:

image: docker:20
variables:
  DOCKER_BUILDKIT: 1
services:
  - docker:dind
build:
  script:
    - docker build --build-arg CI_JOB_TOKEN=$CI_JOB_TOKEN .

Result:

#15 3.284 [INFO] --- maven-dependency-plugin:3.2.0:resolve (default-cli) @ hello-docker ---
#15 4.122 [INFO] 
#15 4.122 [INFO] The following files have been resolved:
#15 4.122 [INFO]    com.exmaple:hello-commons:jar:1.0.0:compile -- module hello.commons (auto)

#18 5.663 [INFO] Downloading from gitlab-maven: https://git.my-company.com/api/v4/projects/295/packages/maven/com/example/hello-commons/1.0.0/hello-commons-1.0.0.pom
#18 5.721 [INFO] Downloading from central: https://repo.maven.apache.org/maven2/com/example/hello-commons/1.0.0/hello-commons-1.0.0.pom
#18 5.741 [WARNING] The POM for com.example:hello-commons:jar:1.0.0 is missing, no dependency information available

#18 14.17 [INFO] BUILD FAILURE
#18 14.17 [INFO] ------------------------------------------------------------------------
#18 14.18 [INFO] Total time:  12.894 s
#18 14.18 [INFO] Finished at: 2022-03-29T16:36:44Z
#18 14.18 [INFO] ------------------------------------------------------------------------
#18 14.18 [ERROR] Failed to execute goal on project hello-docker: Could not resolve dependencies for project com.example:hello-docker:jar:1.0.0: Could not find artifact com.example:hello-commons:jar:1.0.0 in gitlab-maven (https://git.my-company.com/api/v4/projects/295/packages/maven) -> [Help 1]

The result is strange, because I even can download the *.jar and *.pom at the exact same path that maven is not able to resolve here.

What could be the problem?



Solution 1:[1]

--build-arg must be "instantiated" in the Dockerfile using ARG:

FROM maven:3.8.4-eclipse-temurin-11 as dependencies
ARG CI_JOB_TOKEN #this was missing!
COPY pom.xml .

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 membersound