我在使用 Docker 构建 Maven 项目时遇到问题。尽管尝试了各种解决方案,但我仍无法解决它。相关信息如下:
POM.xml 文件:
<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/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>com.tericcabrel</groupId>
<artifactId>bmi</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<java.version>21</java.version>
<spring.boot.version>3.3.0</spring.boot.version>
<lombok.version>1.18.26</lombok.version>
</properties>
<dependencies>
<!-- Spring Boot Dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring.boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>${spring.boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<version>${spring.boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<version>${spring.boot.version}</version>
</dependency>
<!-- Lombok Dependency -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Maven Compiler Plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.12.0</version>
<configuration>
<source>21</source>
<target>21</target>
</configuration>
</plugin>
<!-- Spring Boot Maven Plugin -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring.boot.version}</version>
</plugin>
</plugins>
</build>
</project>
Dockerfile:
# Use an official Maven image as the base image
FROM maven:3.8.4-openjdk-11
# Create a working directory inside the container
WORKDIR /app
# Copy the pom.xml file to the container
COPY pom.xml .
# Download the project dependencies
RUN mvn dependency:go-offline
# Copy the rest of the application source code to the container
COPY src ./src
# Build the application
RUN mvn package -X
# Define the command to run your application
CMD ["java", "-jar", "target/bmi-1.0-SNAPSHOT.jar"]
# EXPOSE port
EXPOSE 8000
错误:
[错误] 无法在项目 bmi 上执行目标 org.apache.maven.plugins:maven-compiler-plugin:3.12.0:compile (default-compile): 致命错误编译: 错误: 无效目标版本: 21 -> [帮助1]
采取的步骤:
尝试将 maven-compiler-plugin 版本更改为 11 和 20。 尝试直接从 src 文件夹进行编译。 尝试了不同的版本并进行了各种配置更改。
问题:
为什么 Maven 失败并出现错误 invalid target release: 21? 使用 Docker 时如何解决此问题? 谢谢您的帮助!
您可以更新 Dockerfile 中的基础映像以包含更新的 JDK。
例如,要使用 Eclipse Temurin 21 进行构建,请更改此行:
FROM maven:3.8.4-openjdk-11
至:
FROM maven:3-eclipse-temurin-21
请参阅此处获取可用 Maven 基础映像的列表:https://hub.docker.com/_/maven/tags