无效或损坏的 jar 文件 - Docker

问题描述 投票:0回答:1

我正在为我的自动化测试项目创建一个 docker 映像,但我总是收到以下错误

你能帮我吗?

错误:jar 文件 javarestassuredextentreport.jar 无效或损坏

Obs:当我生成jar文件时,它会执行文件夹Artifacts,bug我拖到目标,这是正确的吗?

我的项目基于 Rest Assured、TestNG、JUnit,为了创建此映像,我使用来自 Spotity.com 的 dockerfile-maven-plugin

这是我的代码和路径enter image description here

这是我的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
java docker rest dockerfile dockerhub
1个回答
0
投票

您需要有一个可执行的 jar。您的 Maven pom.xml 当前未创建它。

要获取可执行 jar,存档需要包含 META-INF/MANIFEST.MF,声明 jar 的哪个类是主类。虽然您可以自己创建指令来完成任务,但 Maven 具有易于使用的功能来实现相同的目的。将此插件添加到您的 pom.xml 的插件中:

org.apache.maven.plugins maven-jar-插件 真的 com.mycompany.app.App

当然,您需要列出您的主类,而不是示例中给出的类。 要验证您的罐子是否已准备好放入容器内,只需运行

java -jar target/javarestassuredextentreport.jar

并期望您的应用程序以有意义的方式启动。然后才继续将其包裹到容器中。

© www.soinside.com 2019 - 2024. All rights reserved.