Home > other >  Maven inside docker container ignoring local repository
Maven inside docker container ignoring local repository

Time:12-16

I have a maven project with a few custom dependencies that reside on a private repository. I'm attempting to create a docker image based on one of the maven images with these dependencies pre-loaded into the local maven repository.

Dockerfile

FROM maven:3.8.6-openjdk-11-slim

COPY settings-docker.xml /usr/share/maven/ref/
COPY bom.xml /tmp

RUN mvn -B -f /tmp/bom.xml -s /usr/share/maven/ref/settings-docker.xml dependency:resolve

settings-docker.xml

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
                      https://maven.apache.org/xsd/settings-1.0.0.xsd">
    <localRepository>/usr/share/maven/ref/repository</localRepository>

    <mirrors>
        <mirror>
            <id>Mirror of Private Repo</id>
            <mirrorOf>Private Repo</mirrorOf>
            <name>allows http</name>
            <url>http://here.it.is/repository/</url>
        </mirror>
    </mirrors>
</settings>

bom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>org.myproject</groupId>
    <artifactId>bom</artifactId>
    <packaging>pom</packaging>
    <version>1.0</version>

    <repositories>
        <repository>
            <id>Private Repo</id>
            <url>http://here.it.is/repository/</url>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>codec</groupId>
            <artifactId>codec</artifactId>
            <version>1.10.0.</version>
        </dependency>
    </dependencies>

</project>

I followed the instructions from the official maven image on using /usr/share/maven/ref/repository for preloaded dependencies. The image builds successfully and when I start it I can see my dependency in both /usr/share/maven/ref/repository and in /root/.m2/repository.

Despite this when I run maven, it will always attempt to connect to my private repository. Is this some maven behavior I don't know about or is it ignoring my repository?

CodePudding user response:

I believe you have faced with "enhanced local repository manager" feature:

Enhanced local repository manager is built upon the classical Maven 2.0 local repository structure but additionally keeps track of from what repositories a cached artifact was resolved. Resolution of locally cached artifacts will be rejected in case the current resolution request does not match the known source repositories of an artifact, thereby emulating physically separated artifact caches per remote repository.

For example:

% cat ~/.m2/repository/org/springframework/spring-core/5.3.9/_remote.repositories 
#NOTE: This is a Maven Resolver internal implementation file, its format can be changed without prior notice.
#Wed Mar 16 08:49:28 AEDT 2022
spring-core-5.3.9.pom>internal-repository=
spring-core-5.3.9.pom>central=
spring-core-5.3.9.jar>central=
spring-core-5.3.9.jar>internal-repository=

You may either disable that feature via specifying -llr in maven opts or investigate how force maven to use the same "repository id" in different scenarios.

  • Related