You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

执行mvn spring-boot:build-image时archiveFile空指针异常原因

问题:执行mvn spring-boot:build-image构建Spring Boot镜像时出现Cannot invoke "java.io.File.exists()" because "archiveFile" is null错误

错误日志

执行mvn spring-boot:build-image的输出:

$ mvn spring-boot:build-image
[INFO] 扫描项目中...
[INFO] 
[INFO] ---------------------< example:springboot >----------------------
[INFO] 构建Spring Boot 1.0.0-SNAPSHOT
[INFO]   来自pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
...
[INFO] --- spring-boot:3.3.1:build-image (default-cli) @ springboot ---
[INFO] ------------------------------------------------------------------------
[INFO] 构建失败
[INFO] ------------------------------------------------------------------------
[INFO] 总耗时:  6.893 s
[INFO] 完成时间: 2024-12-28T09:48:54Z
[INFO] ------------------------------------------------------------------------
[ERROR] 无法在项目springboot上执行目标org.springframework.boot:spring-boot-maven-plugin:3.3.1:build-image (default-cli): 执行default-cli目标org.springframework.boot:spring-boot-maven-plugin:3.3.1:build-image失败: Cannot invoke "java.io.File.exists()" because "archiveFile" is null -> [帮助 1]
[ERROR] 
[ERROR] 要查看完整错误堆栈跟踪,请使用-e参数重新运行Maven。
[ERROR] 使用-X参数重新运行Maven以启用完整调试日志。
[ERROR] 
[ERROR] 有关错误及可能解决方案的更多信息,请阅读以下文章:
[ERROR] [帮助 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginExecutionException

添加-e参数后的堆栈跟踪:

$ mvn spring-boot:build-image -e
[INFO] 已开启错误堆栈跟踪。
[INFO] 扫描项目中...
[INFO] 
[INFO] ---------------------< example:springboot >----------------------
[INFO] 构建Spring Boot 1.0.0-SNAPSHOT
[INFO]   来自pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
...
[INFO] --- spring-boot:3.3.1:build-image (default-cli) @ springboot ---
[INFO] ------------------------------------------------------------------------
[INFO] 构建失败
[INFO] ------------------------------------------------------------------------
[INFO] 总耗时:  6.474 s
[INFO] 完成时间: 2024-12-28T09:48:33Z
[INFO] ------------------------------------------------------------------------
[ERROR] 无法在项目springboot上执行目标org.springframework.boot:spring-boot-maven-plugin:3.3.1:build-image (default-cli): 执行default-cli目标org.springframework.boot:spring-boot-maven-plugin:3.3.1:build-image失败: Cannot invoke "java.io.File.exists()" because "archiveFile" is null -> [帮助 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:3.3.1:build-image (default-cli) on project springboot: Execution default-cli of goal org.springframework.boot:spring-boot-maven-plugin:3.3.1:build-image failed: Cannot invoke "java.io.File.exists()" because "archiveFile" is null
    at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute2 (MojoExecutor.java:333)
    at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute (MojoExecutor.java:316)
...
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:348)
Caused by: org.apache.maven.plugin.PluginExecutionException: Execution default-cli of goal org.springframework.boot:spring-boot-maven-plugin:3.3.1:build-image failed: Cannot invoke "java.io.File.exists()" because "archiveFile" is null
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:133)
...
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:348)
Caused by: java.lang.NullPointerException: Cannot invoke "java.io.File.exists()" because "archiveFile" is null
    at org.springframework.boot.maven.BuildImageMojo.getArchiveFile (BuildImageMojo.java:306)
    at org.springframework.boot.maven.BuildImageMojo.getBuildRequest (BuildImageMojo.java:261)
    at org.springframework.boot.maven.BuildImageMojo.buildImage (BuildImageMojo.java:251)
...
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:348)
[ERROR] 
[ERROR] 使用-X参数重新运行Maven以启用完整调试日志。
[ERROR] 
[ERROR] 有关错误及可能解决方案的更多信息,请阅读以下文章:
[ERROR] [帮助 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginExecutionException

原因及解决办法

核心原因

这个错误是因为Spring Boot Maven插件在构建镜像时,找不到项目编译打包后生成的可执行jar/war文件(archiveFile变量为空),导致调用exists()方法时触发空指针异常。

常见诱因及解决

  1. 未执行打包阶段
    spring-boot:build-image依赖项目先完成package阶段,生成可运行的归档文件。如果直接单独执行build-image,插件会找不到目标文件。
    解决:执行命令时先触发打包,再构建镜像:
# 先打包再构建镜像
mvn package spring-boot:build-image
# 或者让Maven自动执行前置生命周期阶段
mvn clean spring-boot:build-image
  1. 打包阶段失败
    如果之前的package阶段因为编译错误、依赖缺失、配置问题等原因未成功生成jar/war文件,插件自然找不到目标文件。
    解决:单独执行mvn package,查看控制台输出是否有BUILD SUCCESS,如果失败,先修复编译、依赖等问题,确保target目录下生成了对应的归档文件。

  2. 插件配置异常
    如果在pom.xml中自定义了spring-boot-maven-plugin的配置(比如修改了<finalName>归档文件名、<outputDirectory>输出路径),可能导致插件无法正确定位到归档文件。
    解决:检查插件配置,确保配置的路径和文件名正确,与实际生成的文件一致。


内容的提问来源于stack exchange,提问作者Stewart

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 06:24:53