Maven 3.6.3+Java 8 Docker构建无法找到settings.xml问题求助
Docker构建Maven项目时settings.xml定位失败的解决方法
错误信息
构建Docker镜像时触发如下错误:
failed to compute cache key: "/settings.xml" not found: not found
使用环境:Maven 3.6.3 + Java SDK 8,原Dockerfile代码如下:
# Custom image from Maven on DockerHub # Language: dockerfile FROM maven:3.6.3-amazoncorretto-8 # Set the working dir RUN mkdir -p /app # Create a non root user ARG USERNAME=jefferson ARG USER_UID=1000 ARG USER_GID=$USER_UID # Add linux dependenciesq RUN yum install wget -y RUN yum install shadow-utils -y ENV MAVEN_CONFIG=/var/maven/.m2 # Create the user RUN groupadd --gid $USER_GID $USERNAME \ && useradd --uid $USER_UID --gid $USER_GID -m $USERNAME \ && yum install sudo -y \ && echo $USERNAME ALL=(root) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME \ && chmod 600 /etc/sudoers.d/$USERNAME \ && sudo groupadd docker \ && sudo usermod -aG docker $USERNAME \ && newgrp docker # Change to the root folder and edit the settings.xml for Maven WORKDIR /var/maven/.m2 RUN rm -rf settings.xml \ && chown $USER_UID:$USER_GID . RUN echo '<settings xmlns="http://maven.apache.org/SETTINGS/0.0.0" \ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" \ xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 \ http://maven.apache.org/xsd/settings-1.0.0.xsd"> \ </settings>' >> settings.xml RUN find -name "settings.xml" COPY settings.xml /root/.m2/settings.xml COPY pom.xml ./pom.xml COPY src ./ ./ COPY . ./ USER $USERNAME # Run the application CMD ["mvn", "clean", "verify", "-Duser.home=/var/maven", "-Pcargo.run", "-X"]
问题根源
错误出在COPY settings.xml /root/.m2/settings.xml这条命令:Docker会尝试从构建上下文的本地当前目录读取settings.xml,但该文件不存在,导致构建失败。而实际上你已经通过echo命令在容器内生成了settings.xml,这条COPY命令完全多余。
解决步骤
移除多余的COPY命令
删除Dockerfile中的COPY settings.xml /root/.m2/settings.xml行,无需从本地复制文件到容器。修正settings.xml生成逻辑
- 将生成命令中的
>>改为>,避免文件不存在时的追加异常; - 生成后立即设置文件权限,确保普通用户
jefferson能读写:RUN echo '<settings xmlns="http://maven.apache.org/SETTINGS/0.0.0" \ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" \ xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 \ http://maven.apache.org/xsd/settings-1.0.0.xsd"> \ </settings>' > settings.xml && chown $USER_UID:$USER_GID settings.xml
- 将生成命令中的
调整项目文件复制路径
当前WORKDIR是/var/maven/.m2,后续复制pom.xml、src会把文件放到Maven配置目录下,不符合项目结构。需要切换到/app目录后再复制:# 切换到项目工作目录 WORKDIR /app COPY pom.xml ./ COPY src ./src COPY . ./可选:验证settings.xml状态
可以添加命令确认文件位置和权限:RUN ls -l /var/maven/.m2/settings.xml
修改后的完整Dockerfile
FROM maven:3.6.3-amazoncorretto-8 # Set the working dir RUN mkdir -p /app # Create a non root user ARG USERNAME=jefferson ARG USER_UID=1000 ARG USER_GID=$USER_UID # Add linux dependencies RUN yum install wget -y RUN yum install shadow-utils -y ENV MAVEN_CONFIG=/var/maven/.m2 # Create the user RUN groupadd --gid $USER_GID $USERNAME \ && useradd --uid $USER_UID --gid $USER_GID -m $USERNAME \ && yum install sudo -y \ && echo $USERNAME ALL=(root) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME \ && chmod 600 /etc/sudoers.d/$USERNAME \ && sudo groupadd docker \ && sudo usermod -aG docker $USERNAME \ && newgrp docker # Generate Maven settings.xml and set permissions WORKDIR /var/maven/.m2 RUN rm -rf settings.xml \ && chown $USER_UID:$USER_GID . RUN echo '<settings xmlns="http://maven.apache.org/SETTINGS/0.0.0" \ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" \ xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 \ http://maven.apache.org/xsd/settings-1.0.0.xsd"> \ </settings>' > settings.xml && chown $USER_UID:$USER_GID settings.xml RUN ls -l /var/maven/.m2/settings.xml # Switch to project directory and copy files WORKDIR /app COPY pom.xml ./ COPY src ./src COPY . ./ USER $USERNAME # Run the application CMD ["mvn", "clean", "verify", "-Duser.home=/var/maven", "-Pcargo.run", "-X"]
内容的提问来源于stack exchange,提问作者Cody Wirth
相关产品推荐
相关产品推荐

