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

Docker构建自动化测试镜像时遇Invalid or corrupt jarfile问题求助

问题排查:Docker构建时Invalid or corrupt jarfile错误

我在为基于Rest Assured、TestNG、JUnit的自动化测试项目构建Docker镜像时,持续报错:Error: Invalid or corrupt jarfile javarestassuredextentreport.jar。另外有个疑问:生成的jar文件默认存放在Artifacts文件夹,我手动将其拖到了target目录,这个操作是否正确?当前使用spotify的dockerfile-maven-plugin构建镜像,配置代码如下:

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>javarestassuredextentreport</artifactId>
    <version>1.0-SNAPSHOT</version>
    <build>
    <plugins>
        <plugin>
            <groupId>com.spotify</groupId>
            <artifactId>dockerfile-maven-plugin</artifactId>
            <version>1.4.13</version>
            <executions>
                <execution>
                    <id>default</id>
                    <goals>
                        <goal>build</goal>
                        <goal>push</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <repository>maven-4-openjdk-17</repository>
                <tag>${project.version}</tag>
                <buildArgs>
                    <JAR_FILE>${project.build.finalName}.jar</JAR_FILE>
                </buildArgs>
            </configuration>
        </plugin>
    </plugins>
    </build>
    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>RELEASE</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>RELEASE</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>RELEASE</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.rest-assured/rest-assured -->
        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>rest-assured</artifactId>
            <version>5.3.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>rest-assured</artifactId>
            <version>5.3.2</version>
            <scope>compile</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.json/json -->
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20230618</version>
        </dependency>
        <dependency>
            <groupId>com.aventstack</groupId>
            <artifactId>extentreports</artifactId>
            <version>5.0.9</version>
        </dependency>
        <dependency>
            <groupId>com.aventstack</groupId>
            <artifactId>extentreports-testng-adapter</artifactId>
            <version>1.2.2</version>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>7.8.0</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.24</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.2.2</version>
            <type>maven-plugin</type>
        </dependency>
    </dependencies>
</project>

Dockerfile

FROM openjdk:17
RUN mkdir app
ARG JAR_FILE
ADD /target/${JAR_FILE} /app/javarestassuredextentreport.jar
WORKDIR /app
ENTRYPOINT java -jar javarestassuredextentreport.jar

解决方案

1. 手动移动Jar包的操作问题

手动把Artifacts里的Jar拖到target目录不正确,原因:

  • Maven构建的标准输出目录是target,手动移动可能导致Jar包拷贝不完整、损坏,或与Maven后续构建操作冲突
  • 应通过Maven插件配置,让Jar包直接生成在target目录,无需手动干预

2. Jar包损坏的核心原因及修复

核心原因

  • 未生成包含所有依赖的可执行Jar:当前pom.xml缺少打包插件,生成的是普通Jar,Docker运行时无法找到依赖,被判定为损坏
  • Jar路径/名称不匹配:插件传递的JAR_FILE变量与实际生成的Jar名称不一致,导致Docker构建时拷贝错误文件

修复步骤

步骤1:添加Maven插件生成可执行Jar

在pom.xml的<build><plugins>中添加maven-assembly-plugin,生成包含所有依赖的可执行Jar:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>3.6.0</version>
    <configuration>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
        <archive>
            <manifest>
                <!-- 替换为你的测试入口类(需包含main方法) -->
                <mainClass>com.yourpackage.TestRunner</mainClass>
            </manifest>
        </archive>
    </configuration>
    <executions>
        <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>

步骤2:调整Dockerfile

用COPY替代ADD(拷贝Jar文件时COPY更稳妥,避免自动解压),并规范命令格式:

FROM openjdk:17
RUN mkdir -p /app
ARG JAR_FILE
COPY target/${JAR_FILE} /app/javarestassuredextentreport.jar
WORKDIR /app
ENTRYPOINT ["java", "-jar", "javarestassuredextentreport.jar"]

步骤3:修正dockerfile-maven-plugin配置

修改JAR_FILE参数,匹配生成的可执行Jar名称:

<buildArgs>
    <JAR_FILE>${project.build.finalName}-jar-with-dependencies.jar</JAR_FILE>
</buildArgs>

步骤4:执行标准Maven构建命令

清理并重新构建,确保生成正确的可执行Jar:

mvn clean package

3. 额外优化:清理pom.xml冗余依赖

当前pom.xml存在重复依赖(如junit、testng、rest-assured均重复声明),建议删除重复项,避免依赖冲突。


内容的提问来源于stack exchange,提问作者Bruno Cesar Rodrigues

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.04 22:14:52