无法从 payara-micro-maven-plugin 启动 Payara Micro,获取 "unsupported JDK"

Can't launch Payara Micro from payara-micro-maven-plugin, getting "unsupported JDK"

我整理了一个示例项目来演示我遇到的问题。

https://github.com/johnmanko/payara-micro-plugin-group

基本上,我在开发期间尝试通过 payara micro 的 maven 插件启动我的应用程序。这是 pom.xml 配置:

<plugin>
                <groupId>fish.payara.maven.plugins</groupId>
                <artifactId>payara-micro-maven-plugin</artifactId>
                <version>1.4.0</version>
                <configuration>
                    <useUberJar>false</useUberJar>
                    <contextRoot>/myapp</contextRoot>
                    <payaraVersion>${version.payara}</payaraVersion>
                    <deployWar>false</deployWar>
                    <artifactItem>
                        <groupId>fish.payara.extras</groupId>
                        <artifactId>payara-micro</artifactId>
                        <version>${version.payara.micro}</version>
                    </artifactItem>
                    <javaCommandLineOptions>
                        <option>
                            <value>-Xdebug</value>
                        </option>
                    </javaCommandLineOptions>
                    <commandLineOptions>
                        <option>
                            <key>--autoBindHttp</key>
                        </option>
                        <option>
                            <key>--nocluster</key>
                        </option>
                        <option>
                            <key>--port</key>
                            <value>8095</value>
                        </option>
                        <option>
                            <key>--prebootcommandfile</key>
                            <value>${project.basedir}/src/main/resources/pre-boot-commands.txt</value>
                        </option>
                        <option>
                            <key>--postbootcommandfile</key>
                            <value>${project.basedir}/src/main/resources/post-boot-commands.txt</value>
                        </option>
                        <option>
                            <key>--deploy</key>
                            <value>${project.build.directory}/${project.build.finalName}</value>
                        </option>
                    </commandLineOptions>
                </configuration>
            </plugin>

结果如下:

[2022-05-11T09:54:58.362-0400] [] [SEVERE] [] [org.eclipse.persistence.session./file:/path/to/payara-micro-plugin-group/payara-micro-plugin-example/target/payara-micro-plugin-example-1.0.0-SNAPSHOT/WEB-INF/classes/_MyAppPU.metadata] [tid: _ThreadID=1 _ThreadName=main] [timeMillis: 1652277298362] [levelValue: 1000] [[
  The java.lang.Object class was compiled with an unsupported JDK. Report this error to the EclipseLink open source project.
java.lang.ArrayIndexOutOfBoundsException: Index 8 out of bounds for length 0
    at org.eclipse.persistence.internal.libraries.asm.ClassReader.readUnsignedShort(ClassReader.java:3573)


[2022-05-11T09:54:58.364-0400] [] [WARNING] [] [org.eclipse.persistence.session./file:/path/to/payara-micro-plugin-group/payara-micro-plugin-example/target/payara-micro-plugin-example-1.0.0-SNAPSHOT/WEB-INF/classes/_MyAppPU.metadata] [tid: _ThreadID=1 _ThreadName=main] [timeMillis: 1652277298364] [levelValue: 900] [[
  The java.lang.String class was compiled with an unsupported JDK. Report this error to the EclipseLink open source project.
java.lang.IllegalArgumentException: Unsupported class file major version 62
    at org.eclipse.persistence.internal.libraries.asm.ClassReader.<init>(ClassReader.java:196)


[2022-05-11T09:54:58.372-0400] [] [WARNING] [] [org.eclipse.persistence.session./file:/path/to/payara-micro-plugin-group/payara-micro-plugin-example/target/payara-micro-plugin-example-1.0.0-SNAPSHOT/WEB-INF/classes/_MyAppPU.metadata] [tid: _ThreadID=1 _ThreadName=main] [timeMillis: 1652277298372] [levelValue: 900] [[
  The java.lang.Long class was compiled with an unsupported JDK. Report this error to the EclipseLink open source project.
java.lang.IllegalArgumentException: Unsupported class file major version 62
    at org.eclipse.persistence.internal.libraries.asm.ClassReader.<init>(ClassReader.java:196)


[2022-05-11T09:54:58.379-0400] [] [WARNING] [] [org.eclipse.persistence.session./file:/path/to/payara-micro-plugin-group/payara-micro-plugin-example/target/payara-micro-plugin-example-1.0.0-SNAPSHOT/WEB-INF/classes/_MyAppPU.metadata] [tid: _ThreadID=1 _ThreadName=main] [timeMillis: 1652277298379] [levelValue: 900] [[
  The java.lang.Number class was compiled with an unsupported JDK. Report this error to the EclipseLink open source project.
java.lang.IllegalArgumentException: Unsupported class file major version 62
    at org.eclipse.persistence.internal.libraries.asm.ClassReader.<init>(ClassReader.java:196)

该项目是用 JDK 1.8 构建的,maven 是通过将 JAVA_HOME 设置为 JDK 1.8:

来启动的
DATABASE_USER=user \
DATABASE_PASS=password \
DATABASE_NAME=MY_DB_NAME \
DATABASE_SERVER=localhost \
DATABASE_SERVER_PORT=1234 \
JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-amd64 \
M2_HOME=/usr/share/maven \
/usr/share/maven/bin/mvn payara-micro:start

系统默认值是 Java 18,但我什至看不出这有什么用。所有包含的应用程序依赖项都是使用 JDK 1.8 或更早版本编译的(我检查了每一个)。

为什么它会选择主要版本 62?

我找到了原因。该插件使用 Apache Toolchain 来定位 java,它找到了错误的(即系统的)。

一种解决方案是添加插件 <javaPath> 选项:

                <configuration>
                    <useUberJar>false</useUberJar>
                    <contextRoot>/myapp</contextRoot>
                    <payaraVersion>${version.payara}</payaraVersion>
                    <deployWar>false</deployWar>                    
                    <javaPath>/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/bin/java</javaPath>

当然,这不适用于其他系统上的开发人员,例如 Mac 的 Windows。

正确的方法是配置一个 ~/.m2/toolchains.xml 文件并在 pom 中设置 Toolchain 以使用它。

~/.m2/toolchains.xml:

<?xml version="1.0" encoding="UTF-8"?>
<toolchains>
    <!-- JDK toolchains -->
    <toolchain>
        <type>jdk</type>
        <provides>
            <version>1.8</version>
            <vendor>openjdk</vendor>
        </provides>
        <configuration>
            <jdkHome>/usr/lib/jvm/java-1.8.0-openjdk-amd64</jdkHome>
        </configuration>
    </toolchain>
    <toolchain>
        <type>jdk</type>
        <provides>
            <version>11</version>
            <vendor>openjdk</vendor>
        </provides>
        <configuration>
            <jdkHome>/usr/lib/jvm/java-11-openjdk-amd64</jdkHome>
        </configuration>
    </toolchain>
    <toolchain>
        <type>jdk</type>
        <provides>
            <version>18</version>
            <vendor>openjdk</vendor>
        </provides>
        <configuration>
            <jdkHome>/usr/lib/jvm/java-18-openjdk-amd64</jdkHome>
        </configuration>
    </toolchain>
</toolchains>

pom.xml


            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.10.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-toolchains-plugin</artifactId>
                <version>1.1</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>toolchain</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <toolchains>
                        <jdk>
                            <version>1.8</version>
                            <vendor>openjdk</vendor>
                        </jdk>
                    </toolchains>
                </configuration>
            </plugin>
            <plugin>
                <groupId>fish.payara.maven.plugins</groupId>
                <artifactId>payara-micro-maven-plugin</artifactId>
                <version>1.4.0</version>
                <configuration>
                    <useUberJar>false</useUberJar>
                    <contextRoot>/myapp</contextRoot>
                    <payaraVersion>${version.payara}</payaraVersion>
                    <deployWar>false</deployWar>                    
                    <artifactItem>
                        <groupId>fish.payara.extras</groupId>
                        <artifactId>payara-micro</artifactId>
                        <version>${version.payara.micro}</version>
                    </artifactItem>
                    <javaCommandLineOptions>
                        <option>
                            <value>-Xdebug</value>
                        </option>
                    </javaCommandLineOptions>
                    <commandLineOptions>
                        <option>
                            <key>--nocluster</key>
                        </option>
                        <option>
                            <key>--port</key>
                            <value>8095</value>
                        </option>
                        <option>
                            <key>--postbootcommandfile</key>
                            <value>${project.basedir}/src/main/resources/post-boot-commands.txt</value>
                        </option>
                        <option>
                            <key>--deploy</key>
                            <value>${project.build.directory}/${project.build.finalName}</value>
                        </option>
                    </commandLineOptions>
                </configuration>
            </plugin>

使用 nbactions.xml 启动:

<?xml version="1.0" encoding="UTF-8"?>
<actions>
    <action>
        <actionName>CUSTOM-toolchains:toolchain payara-micro:start (with DB)</actionName>
        <displayName>toolchains:toolchain payara-micro:start (with DB)</displayName>
        <goals>
            <goal>toolchains:toolchain</goal>
            <goal>payara-micro:start</goal>
        </goals>
        <properties>
            <Env.DATABASE_USER>user</Env.DATABASE_USER>
            <Env.DATABASE_PASS>password</Env.DATABASE_PASS>
            <Env.DATABASE_NAME>DATABASE</Env.DATABASE_NAME>
            <Env.DATABASE_SERVER>localhost</Env.DATABASE_SERVER>
            <Env.DATABASE_SERVER_PORT>1234</Env.DATABASE_SERVER_PORT>
        </properties>
    </action>
</actions>

使用命令行启动:

DATABASE_USER=user \
DATABASE_PASS=password \
DATABASE_NAME=MY_DB_NAME \
DATABASE_SERVER=localhost \
DATABASE_SERVER_PORT=1234 \
JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-amd64 \
M2_HOME=/usr/share/maven \
/usr/share/maven/bin/mvn toolchains:toolchain payara-micro:start