简体   繁体   English

docker 容器中的 Maven 忽略本地存储库

[英]Maven inside docker container ignoring local repository

I have a maven project with a few custom dependencies that reside on a private repository.我有一个 Maven 项目,其中包含一些驻留在私有存储库中的自定义依赖项。 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.我正在尝试基于其中一个 maven 图像创建一个 docker 图像,并将这些依赖项预加载到本地 maven 存储库中。

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设置-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.我按照官方 maven 图像中关于使用/usr/share/maven/ref/repository预加载依赖项的说明进行操作。 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 .图像构建成功,当我启动它时,我可以在/usr/share/maven/ref/repository/root/.m2/repository中看到我的依赖项。

Despite this when I run maven, it will always attempt to connect to my private repository.尽管如此,当我运行 maven 时,它总是会尝试连接到我的私有存储库。 Is this some maven behavior I don't know about or is it ignoring my repository?这是一些我不知道的 Maven 行为还是忽略了我的存储库?

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.增强的本地存储库管理器建立在经典的 Maven 2.0 本地存储库结构之上,但另外还跟踪从哪些存储库中解析了缓存的工件。 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.您可以通过在 maven opts 中指定-llr来禁用该功能,或者研究如何强制 maven 在不同的场景中使用相同的“存储库 ID”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM