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

Spring Boot应用部署fly.io报错:无效或损坏的jar文件/app.jar

Spring Boot部署fly.io失败:Invalid or corrupt jarfile /app.jar

问题现象

  • 部署至fly.io时,v45、v46版本均因资源分配不健康失败,日志核心错误为Error: Invalid or corrupt jarfile /app.jar
  • 本地构建显示BUILD SUCCESS,但伴随警告:请求的配置文件"pom.xml"无法激活,因为该文件不存在

部署日志

2022-12-27T18:13:45Z   [info]Preparing to run: `java -jar /app.jar` as root
2022-12-27T18:13:45Z   [info]2022/12/27 18:13:45 listening on [fdaa:1:12ef:a7b:a0:3feb:41aa:2]:22 (DNS: [fdaa::3]:53)
2022-12-27T18:13:45Z   [info]Error: Invalid or corrupt jarfile /app.jar
2022-12-27T18:13:46Z   [info]Starting clean up.
--> v45 failed - Failed due to unhealthy allocations - not rolling back to stable job version 45 as current job has same specification and deploying as v46 

--> Troubleshooting guide at https://fly.io/docs/getting-started/troubleshooting/
Error abort

Dockerfile

FROM ibm-semeru-runtimes:open-17-jre-focal
EXPOSE 8080
ARG JAR_FILE= ./target/*.jar
COPY ${JAR_FILE} /app.jar
ENTRYPOINT ["java", "-jar", "/app.jar"]

pom.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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.7</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.tasf</groupId>
    <artifactId>prueba</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>prueba</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        ...     
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

解决方案

1. 修复Dockerfile参数错误

Dockerfile中ARG JAR_FILE= ./target/*.jar的等号后存在空格,会导致参数解析异常,COPY无法正确获取jar包。修改为:

ARG JAR_FILE=./target/*.jar

2. 确保本地生成有效可执行jar

执行mvn clean package后,检查target目录下是否存在prueba-0.0.1-SNAPSHOT.jar(对应pom中的artifactId和version),并通过java -jar target/prueba-0.0.1-SNAPSHOT.jar验证jar可正常启动,排除本地构建产物损坏问题。

3. 消除本地构建警告

该警告是因为构建命令错误指定了profile参数(比如误执行mvn -P pom.xml clean package),直接执行mvn clean package即可解决。

4. 重新部署

修复上述问题后,重新执行fly.io部署流程,确保镜像打包和推送过程无异常。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 15:50:38