尝试 Dockerize Maven 应用程序,但收到“不支持发布版本 21”

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

Java 和 Docker 新手,尝试使用以下方法对 this(我的第一个 Spring Boot 应用程序)进行 dockerize:

Dockerfile


FROM maven:3.8.4-jdk-11 AS build

COPY src /app/src
COPY pom.xml /app

WORKDIR /app
RUN mvn clean install -U

FROM openjdk:8-jre-alpine
COPY --from=build /app/target/mal-randomizer-0.0.1-SNAPSHOT.jar /app/app.jar

WORKDIR /app

EXPOSE 8080

CMD ["java", "-jar", "app.jar"]

昨晚多次出现此错误:

20.09 [INFO] Compiling 8 source files with javac [debug release 21] to target/classes
20.18 [INFO] ------------------------------------------------------------------------
20.18 [INFO] BUILD FAILURE
20.18 [INFO] ------------------------------------------------------------------------
20.18 [INFO] Total time:  19.160 s
20.18 [INFO] Finished at: 2024-04-22T14:49:00Z
20.18 [INFO] ------------------------------------------------------------------------
20.18 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.11.0:compile (default-compile) on project mal-randomizer: Fatal error compiling: error: release version 21 not supported -> [Help 1]
20.18 [ERROR] 
20.18 [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
20.18 [ERROR] Re-run Maven using the -X switch to enable full debug logging.
20.18 [ERROR] 
20.18 [ERROR] For more information about the errors and possible solutions, please read the following articles:
20.18 [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

我尝试了多种解决方案,例如:

  • 重新定义我的
    JAVA_HOME
    环境
  • 换了图
  • 更改了
    pom.xml
    Java 版本、maven 编译器目标和源*

*如果我将 Java 版本降级太多(低于 Java 16),我的代码将无法运行,因为记录已在 16 中引入。

我相信解决方案在于

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>3.2.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.victorfernandesneto</groupId>
    <artifactId>mal-randomizer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>mal-randomizer</name>
    <description>MyAnimeList&apos;s Unofficial List Randomizer</description>
    <properties>
        <java.version>21</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.10.1</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
java spring spring-boot docker dockerfile
1个回答
0
投票

有两件事并不真正匹配:dockerfile 中的 maven:3.8.4-jdk-11 和 pom.xml 中的 21。这意味着您正在尝试使用 Java 21 构建应用程序,但您使用的是“仅”Java 11 的 Maven 映像,这是行不通的。

错误消息证明了这一点

error: release version 21 not supported -> [Help 1]

此外,您用来运行构建的应用程序的映像显然使用更低的 Java 版本:

openjdk:8-jre-alpine

这也行不通……

pom.xml 中的 Java 版本必须得到构建映像和稍后运行应用程序的映像的支持,因此请使用

maven:3.8.4-jdk-21
(或不同的 Maven 版本)进行构建,并使用
openjdk:21-jre-alpine
进行运行.

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