刚接触Java和Docker,try 使用以下方法对this(我的第一个Spring Boot应用程序)进行dockered:

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

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

  • 重新定义我的JAVA_HOME个环境
  • 改变了形象
  • 更改了pom.xml Java版本、专家编译器目标和源 *

* 如果我将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>

推荐答案

有两件事并不真正匹配:dockerfile中的maven:3.8.4-jdk-11和pom.html中的<java.version>21</java.version>.这意味着您正在try 使用Java 21构建应用程序,但您使用的是支持最高版本11的Java的maven镜像,但该镜像将无法工作.

That's proof by the error message
error: release version 21 not supported … (by an image with JDK 11).

In addition, the image you are using to run the built application obviously uses an even lower Java version: openjdk:8-jre-alpine.
That will not work either… You're going get a similar error message as soon as the build is done with the correct Java version.

pom.html中的Java版本必须得到构建镜像和后来运行应用程序的镜像的支持,因此在构建中使用maven:3.8.4-jdk-21(或不同的maven版本),在运行中使用openjdk:21-jre-alpine.

Java相关问答推荐

Java -使用空比较或instanceof?

计算战舰沉船/船只的问题(Java)

如何让TaskView总是添加特定的列来进行排序?

JPackage-results已安装-如何添加系统属性?

如何使用解析器组合子解析Java数组类型签名?

Jooq外键关系

Spring和可编辑";where";@Query

在Java 17中使用两个十进制数字分析时间时出错,但在Java 8中成功

Spring安全令牌刷新和JWT签名与本地计算的签名不匹配

对从Spring Boot 3.1.5升级到3.2.0的方法的查询验证失败

JFree Chart从图表中删除边框

如何在Record Java中使用isRecord()和RecordComponent[]?

Java HashMap保留所有时间复杂性

通过/失败的参数化junit测试方法执行数

在Spring Boot中使用咖啡因进行缓存-根据输出控制缓存

Intellij 2023 IDE:始终在顶部显示菜单栏

简化每个元素本身都是 map 列表的列表

始终使用Spring Boot连接mongodb上的测试数据库

转换为JSON字符串时,日期按天递减-Java

Java 8 中 ByteBuffer 和 BitSet 的奇怪行为