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

Render.com Docker部署Java后端mvn编译报错:无效目标版本17

Docker部署后端Maven编译报错解决

我的Dockerfile

#
# Build stage
#
# Use the official maven/Java 8 image to create a build artifact.
# https://hub.docker.com/_/maven
FROM maven:3.8.2-jdk-11 as build
#
# # Copy local code to the container image.
COPY . .
#
# # Build a release artifact.
RUN mvn clean package


#
# Package stage
#
# It's important to use OpenJDK 8u191 or above that has container support enabled.
# https://hub.docker.com/r/adoptopenjdk/openjdk8
# https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds
FROM openjdk:11-jdk-slim

# Copy the jar to the production image from the builder stage.
COPY --from=build /target/Hostel-Availability-System-0.0.1-SNAPSHOT.jar Hostel-Availability-System.jar

# ENV PORT=8080
EXPOSE 8080

# Run the web service on container startup.
ENTRYPOINT ["java","-jar","Hostel-Availability-System.jar"]

构建报错信息

[build 3/3] RUN mvn clean install:
Apr 24 08:38:02 PM  #11 21.15 [INFO] Total time:  18.592 s
Apr 24 08:38:02 PM  #11 21.15 [INFO] Finished at: 2023-04-24T15:08:02Z
Apr 24 08:38:02 PM  #11 21.15 [INFO] ------------------------------------------------------------------------
Apr 24 08:38:02 PM  #11 21.15 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.10.1:compile (default-compile) on project Hostel-Availability-System: Fatal error compiling: error: invalid target release: 17 -> [Help 1]
Apr 24 08:38:02 PM  #11 21.15 [ERROR]
Apr 24 08:38:02 PM  #11 21.15 [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
Apr 24 08:38:02 PM  #11 21.15 [ERROR] Re-run Maven using the -X switch to enable full debug logging.
Apr 24 08:38:02 PM  #11 21.15 [ERROR]
Apr 24 08:38:02 PM  #11 21.15 [ERROR] For more information about the errors and possible solutions, please read the following articles:
Apr 24 08:38:02 PM  #11 21.15 [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
Apr 24 08:38:02 PM  ------
Apr 24 08:38:02 PM  Dockerfile:12
Apr 24 08:38:02 PM  --------------------
Apr 24 08:38:02 PM    10 |     #
Apr 24 08:38:02 PM    11 |     # # Build a release artifact.
Apr 24 08:38:02 PM    12 | >>> RUN mvn clean install
Apr 24 08:38:02 PM    13 |
Apr 24 08:38:02 PM    14 |
Apr 24 08:38:02 PM  --------------------
Apr 24 08:38:02 PM  error: failed to solve: process "/bin/sh -c mvn clean install" did not complete successfully: exit code: 1
Apr 24 08:38:02 PM  error: exit status 1

解决方法

方法1:升级Docker镜像的JDK版本到17

报错核心是构建阶段用的JDK11,而项目编译目标是JDK17,版本不匹配导致编译失败。修改Dockerfile的两个基础镜像为JDK17版本:

# Build stage
FROM maven:3.8.2-jdk-17 as build
COPY . .
RUN mvn clean package

# Package stage
FROM openjdk:17-jdk-slim
COPY --from=build /target/Hostel-Availability-System-0.0.1-SNAPSHOT.jar Hostel-Availability-System.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","Hostel-Availability-System.jar"]

方法2:修改项目编译目标为JDK11

如果不需要JDK17特性,可调整项目的Maven编译配置,在pom.xml中修改maven-compiler-plugin的source和target为11,保持和Docker镜像的JDK版本一致:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.10.1</version>
            <configuration>
                <source>11</source>
                <target>11</target>
            </configuration>
        </plugin>
    </plugins>
</build>

修改后重新构建即可解决版本不兼容问题。

内容的提问来源于stack exchange,提问作者HARMANPREET SINGH

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 02:07:01