【Azure Container App】构建Java应用镜像时候遇无法编译错误:ERROR [build 10 10] RUN . mvnw.cmd dependency:go‐offline ‐B ‐Dproduction package - LuBu0505/My-Code GitHub Wiki

问题描述

部署Java应用到Azure Container App中,首先需要在本地构建镜像文件后上传到Azure Contaienr Registrier中。

但是,在第一步构建镜像的时候,就遇见错误:

=> ERROR [build 9/9] RUN ./mvnw.cmd dependency:go-offline -B -Dproduction package 0.6s

[build 9/9] RUN ./mvnw.cmd dependency:go-offline -B -Dproduction package: 0.523 /bin/sh: line 1: ./mvnw.cmd: No such file or directory

就单独代码调试可以正常运行。而且,项目文件根目录(与Dockerfile同级)中,已经包含了 mvnw 文件和 mvnw.cmd, 所以,不应该是文件本身确实的原因导致的。

项目目录结构


....>javademo> tree /a /f
Folder PATH listing for volume Local Disk
Volume serial number is 4CDB-764A
C:.
| .gitignore
| CHANGELOG.md
| CONTRIBUTING.md
| Dockerfile
| LICENSE.md
| mvnw
| mvnw.cmd
| pom.xml

...

DockerFile内容


问题解答

以前遇见问题,全网搜索。当前,遇见问题,第一问AI大模型。

我得到的答案是:

我的问题:

RUN ./mvnw dependency:go-offline -B -Dproduction package 这句话在dockerfile中是什么目的呢?如果遇见报错“0.288 /bin/sh: line 1: ./mvnw: cannot execute: required file not found” 如何来解决它

M365 Copilot的回答:

这条命令:

RUN ./mvnw dependency:go-offline -B -Dproduction package

在 Dockerfile 中的作用是:

🧩 命令目的解释:

  1. ./mvnw:使用项目自带的 Maven Wrapper(mvnw),确保使用的是项目指定版本的 Maven,而不是系统安装的 Maven。
  2. dependency:go-offline:提前下载所有依赖项,以便后续构建过程可以在离线环境中进行,提高构建速度和稳定性。
  3. -B:表示 batch 模式,避免 Maven 交互式提示,适合自动化构建。
  4. -Dproduction:传递一个名为 production 的系统属性,可能用于激活某些 profile 或配置。
  5. package:执行 Maven 的 package 阶段,通常会编译代码并打包成 JAR 或 WAR 文件。

🛠 报错分析:

错误信息:

/bin/sh: line 1: ./mvnw: cannot execute: required file not found

说明 Docker 构建过程中找不到 ./mvnw 文件,可能原因如下:

✅ 解决方法:

  1. 确认 mvnw 文件是否存在于 Docker 构建上下文中

    • mvnw 和 mvnw.cmd 应该在项目根目录。
    • 如果你用的是 .dockerignore 文件,确保没有把 mvnw 忽略掉。
  2. 检查 Dockerfile 的工作目录设置

    • 确保你在执行命令前设置了正确的工作目录,例如:

      WORKDIR /app
      COPY . /app
      RUN chmod +x mvnw
      RUN ./mvnw dependency:go-offline -B -Dproduction package
      
      
  3. 添加执行权限

    • 有时 mvnw 没有执行权限,可以加上:

      RUN chmod +x mvnw
      
      

如果你能提供 Dockerfile 的相关部分,我可以帮你更具体地定位问题并优化它。你想我帮你检查一下 Dockerfile 吗?

其实,根据以上三点提示,并没有定位到问题。

进一步思考, mvnw 是用于打包java 应用,而mvnw 和 mvnw.bat文件都是在创建项目是自动生成。

开始怀疑是文件内容由问题导致,所以决定删除并生成新文件。

使用下面的命令可以重新生成 mvnw文件或更新:

mvn -N io.takari:maven:wrapper

image.png

随后,再次执行 docker build 指令( docker build -t myalbum:v1 . )成功。镜像正常生成! image.png

为了找到之前的 mvnw 文件为何会失败,在对比两个文件后发现:"重新生成mvnw文件是最快的解决之道" image.png

参考资料

What is the purpose of mvnw and mvnw.cmd files? https://stackoverflow.com/questions/38723833/what-is-the-purpose-of-mvnw-and-mvnw-cmd-files

These files are from Maven wrapper. It works similarly to the Gradle wrapper.

This allows you to run the Maven project without having Maven installed and present on the path. It downloads the correct Maven version if it's not found (as far as I know by default in your user home directory).

The mvnw file is for Linux (bash) and the mvnw.cmd is for the Windows environment.


To create or update all necessary Maven Wrapper files execute the following command:

mvn -N io.takari:maven:wrapper

To use a different version of maven you can specify the version as follows:

mvn -N io.takari:maven:wrapper -Dmaven=3.3.3

Both commands require maven on PATH (add the path to maven bin to Path on System Variables) if you already have mvnw in your project you can use ./mvnw instead of mvn in the commands.

当在复杂的环境中面临问题,格物之道需:浊而静之徐清,安以动之徐生。 云中,恰是如此!