【问题标题】:Mounting Maven Repository to Docker将 Maven 存储库挂载到 Docker
【发布时间】:2018-01-21 11:32:35
【问题描述】:

我正在尝试构建一个 Java 应用程序并使用 docker 制作一个包。这个构建需要一个我不想包含在图像中的 Maven 存储库,因为它非常大。我想尝试使用卷并将我的本地 maven 存储库安装到映像中的 maven 存储库。我使用apt-get install -y maven 以使maven 可用,但在图像$HOME 中找不到目录.m2

我使用ls -la $HOMEls -lals -la /root找到了maven home,但是那里没有.m2目录。

编辑 1:

我在Dockerfile 中有这些行:

FROM ubuntu
MAINTAINER Zeinab Abbasimazar
# Install and configure required packages
RUN apt-get update; \
    apt-get install -y --no-install-recommends apt-utils; \
    apt-get install -y dialog; \
    apt-get install -y wget unzip curl maven; \
    mkdir $HOME/.m2/; \
    ls -la /usr/share/maven/conf/; \
    echo \
    "<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>/root/.m2/repository</localRepository> \
        <interactiveMode>true</interactiveMode> \
        <usePluginRegistry>false</usePluginRegistry> \
        <offline>false</offline> \
    </settings>" \
    > /usr/share/maven/conf/settings.xml
VOLUME ["/home/zeinab/.m2/", "/root/.m2/"]
# Build
RUN mvn  -X clean install -pl components -P profile

它将本地存储库配置放入映像的 maven 配置文件中,将我的本地 maven 存储库安装到映像中的目录并最终执行构建。正如我在 Maven 构建日志中看到的那样,它正在使用我期望的本地存储库路径:

[DEBUG] Reading global settings from /usr/share/maven/conf/settings.xml
[DEBUG] Reading user settings from /root/.m2/settings.xml
[DEBUG] Using local repository at /root/.m2/repository

但仍然无法检测到依赖关系。

【问题讨论】:

  • 为什么不在 docker 容器之外构建应用程序,然后将结果复制到容器中?
  • 能否请您发布完整的错误日志。
  • 我同意@khmarbaise - 在 Docker 之外构建应用程序会容易得多。
  • @khmarbaise,我想过这个;但我更喜欢在同一个镜像中构建和打包,以便进行集成。
  • @Akash,没有错误。我说我在搜索.m2目录,但是找不到。

标签: java maven docker dockerfile docker-build


【解决方案1】:

我终于找到了在 docker 中安装本地 maven 存储库的解决方案。我改变了我的解决方案;我将它安装在run 阶段而不是build 阶段。这是我的Dockerfile

FROM ubuntu
MAINTAINER Zeinab Abbasimazar
ADD gwr $HOME
RUN apt-get update; \
    apt-get install -y --no-install-recommends apt-utils; \
    apt-get install -y wget unzip curl maven git; \
    echo \
    "<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>/root/.m2/repository</localRepository> \
        <interactiveMode>true</interactiveMode> \
        <usePluginRegistry>false</usePluginRegistry> \
        <offline>false</offline> \
    </settings>" \
    > /usr/share/maven/conf/settings.xml; \
    mkdir /root/.m2/; \
    echo \
    "<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>/root/.m2/repository</localRepository> \
        <interactiveMode>true</interactiveMode> \
        <usePluginRegistry>false</usePluginRegistry> \
        <offline>false</offline> \
    </settings>" \
    > /root/.m2/settings.xml
WORKDIR .
CMD mvn  -X clean install -pl components -P profile

首先,我使用上面的Dockerfile构建图像:

sudo docker build -t imageName:imageTag .

然后,我运行一个容器如下:

sudo docker run -d -v /home/zeinab/.m2/:/root/.m2/ --name containerName imageName:imageTag

【讨论】:

    【解决方案2】:

    您找不到~/.m2 目录,因为它仅在需要时创建,即当您将库存储在本地存储库中或添加配置文件时。

    你可以自己创建~/.m2目录,在里面创建你自己的settings.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>/path/to/local/repo/</localRepository>
      ...
    </settings>
    

    阅读documentation了解更多详情。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-20
      • 1970-01-01
      • 1970-01-01
      • 2018-05-20
      • 2014-01-01
      • 1970-01-01
      • 2013-06-06
      • 1970-01-01
      相关资源
      最近更新 更多