由于 Java 版本不匹配,Maven 无法构建 jar 文件

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

当我尝试从 Spring Boot 项目构建 jar 文件时,出现了此问题。首先,我在终端中输入以下命令:

./mvnw clean package

然后我看到以下日志消息:

/JayathilakaPosBackedWithPrinter$ ./mvnw clean package
[INFO] Scanning for projects...
[INFO] 
[INFO] ----< com.jayathilakaRiceProducts:JayathilakaPosBackedWithPrinter >-----
[INFO] Building JayathilakaPosBackedWithPrinter 0.0.1-SNAPSHOT
[INFO]   from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- clean:3.3.2:clean (default-clean) @ JayathilakaPosBackedWithPrinter ---
[INFO] Deleting /home/rashmika/Paid Projects/JayathilakaRiceProducts_With printer/PaysheetPrinter_JayathilakaRiceProducts/JayathilakaPosBackedWithPrinter/target
[INFO] 
[INFO] --- resources:3.3.1:resources (default-resources) @ JayathilakaPosBackedWithPrinter ---
[INFO] Copying 1 resource from src/main/resources to target/classes
[INFO] Copying 2 resources from src/main/resources to target/classes
[INFO] 
[INFO] --- compiler:3.11.0:compile (default-compile) @ JayathilakaPosBackedWithPrinter ---
[INFO] Changes detected - recompiling the module! :source
[INFO] Compiling 17 source files with javac [debug release 21] to target/classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  4.641 s
[INFO] Finished at: 2024-11-03T19:19:55+05:30
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.11.0:compile (default-compile) on project JayathilakaPosBackedWithPrinter: Fatal error compiling: error: release version 21 not supported -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
rashmika@rashmika-Aspire-A515-53G:~/Paid Projects/JayathilakaRiceProducts_With printer/PaysheetPrinter_JayathilakaRiceProducts/JayathilakaPosBackedWithPrinter$ ./mvnw clean package
[INFO] Scanning for projects...
[INFO] 
[INFO] ----< com.jayathilakaRiceProducts:JayathilakaPosBackedWithPrinter >-----
[INFO] Building JayathilakaPosBackedWithPrinter 0.0.1-SNAPSHOT
[INFO]   from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- clean:3.3.2:clean (default-clean) @ JayathilakaPosBackedWithPrinter ---
[INFO] Deleting /home/rashmika/Paid Projects/JayathilakaRiceProducts_With printer/PaysheetPrinter_JayathilakaRiceProducts/JayathilakaPosBackedWithPrinter/target
[INFO] 
[INFO] --- resources:3.3.1:resources (default-resources) @ JayathilakaPosBackedWithPrinter ---
[INFO] Copying 1 resource from src/main/resources to target/classes
[INFO] Copying 2 resources from src/main/resources to target/classes
[INFO] 
[INFO] --- compiler:3.11.0:compile (default-compile) @ JayathilakaPosBackedWithPrinter ---
[INFO] Changes detected - recompiling the module! :source
[INFO] Compiling 17 source files with javac [debug release 21] to target/classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  5.231 s
[INFO] Finished at: 2024-11-03T19:20:44+05:30
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.11.0:compile (default-compile) on project JayathilakaPosBackedWithPrinter: Fatal error compiling: error: release version 21 not supported -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

我的

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.3.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.jayathilakaRiceProducts</groupId>
    <artifactId>JayathilakaPosBackedWithPrinter</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>JayathilakaPosBackedWithPrinter</name>
    <description>jayathilaka pos backend with printer</description>
    <url/>
    <licenses>
        <license/>
    </licenses>
    <developers>
        <developer/>
    </developers>
    <scm>
        <connection/>
        <developerConnection/>
        <tag/>
        <url/>
    </scm>
    <properties>
        <java.version>21</java.version>
    </properties>
    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

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

        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </dependency>

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

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.jpos/jpos -->
        <dependency>
            <groupId>org.jpos</groupId>
            <artifactId>jpos</artifactId>
            <version>2.1.4</version>
        </dependency>



<!--        <dependency>-->
<!--            <groupId>com.itextpdf</groupId>-->
<!--            <artifactId>itext7-core</artifactId>-->
<!--            <version>7.1.12</version>-->
<!--        </dependency>-->


        <!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf -->
<!--        <dependency>-->
<!--            <groupId>com.itextpdf</groupId>-->
<!--            <artifactId>itextpdf</artifactId>-->
<!--            <version>5.5.13.3</version>-->
<!--        </dependency>-->


<!--        <dependency>-->
<!--            <groupId>javax.print</groupId>-->
<!--            <artifactId>javax.print-api</artifactId>-->
<!--            <version>1.0</version>-->
<!--        </dependency>-->

        <dependency>
            <groupId>org.usb4java</groupId>
            <artifactId>usb4java-javax</artifactId>
            <version>1.3.0</version>
        </dependency>
<!--        <dependency>-->
<!--            <groupId>javax.usb</groupId>-->
<!--            <artifactId>javax-usb-api</artifactId>-->
<!--            <version>1.0.2</version>-->
<!--        </dependency>-->
        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>pdfbox</artifactId>
            <version>2.0.24</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <executable>true</executable>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.11.0</version>
                <configuration>
                    <source>21</source>
                    <target>21</target>
                    <compilerArgs>
                        <arg>--enable-preview</arg>
                    </compilerArgs>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

我可以看到这是因为某种 Java 版本不匹配。但我的系统 Java 版本与

pom.xml

匹配
java spring-boot maven java-21
1个回答
0
投票

我想通了。我不得不指示 Maven 使用 Java 21。出于某种原因,Maven 仍然使用 Java 17,这是我的第一个 java 安装版本。 这些是我遵循的步骤。 (我的操作系统是Ubuntu 22)

  1. 在 Spring Boot 项目中打开终端。
  2. 输入以下代码查看系统java版本
$ java --version
openjdk 21.0.4 2024-07-16
OpenJDK Runtime Environment (build 21.0.4+7-Ubuntu-1ubuntu222.04)
OpenJDK 64-Bit Server VM (build 21.0.4+7-Ubuntu-1ubuntu222.04, mixed mode, sharing)
  1. 运行以下命令检查maven java版本
$ ./mvnw -v
Apache Maven 3.9.7 (8b094c9513efc1b9ce2d952b3b9c8eaedaf8cbf0)
Maven home: /home/rashmika/.m2/wrapper/dists/apache-maven-3.9.7/2a4cb831
Java version: 17.0.6, vendor: Oracle Corporation, runtime: /usr/lib/jvm/java-17-oracle
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "6.8.0-47-generic", arch: "amd64", family: "unix"

正如你在这里看到的,我有两个不同的 java 版本用于 Maven 和系统。 为了解决这个问题,我们需要将这两个 java 版本合二为一。 这就是我们可以做到的。

  1. 找到java安装位置。
sudo update-alternatives --config java

这是我该命令的输出

  Selection    Path                                         Priority   Status
------------------------------------------------------------
  0            /usr/lib/jvm/java-21-openjdk-amd64/bin/java   2111      auto mode
  1            /usr/lib/jvm/java-11-openjdk-amd64/bin/java   1111      manual mode
  2            /usr/lib/jvm/java-17-oracle/bin/java          1091      manual mode
* 3            /usr/lib/jvm/java-21-openjdk-amd64/bin/java   2111      manual mode

从这里开始,我只需要

/usr/lib/jvm/java-21-openjdk-amd64
部分。 2.设置
$JAVA_HOME
变量

 export JAVA_HOME=/usr/lib/jvm/java-21-openjdk-amd64

3.确认更改

echo $JAVA_HOME

4.现在问题应该解决了。我们可以构建 jar 文件

./mvnw clean package
© www.soinside.com 2019 - 2024. All rights reserved.