【问题标题】:dockerBuild fails resulting in an unsupported class file major version 61 errordockerBuild 失败导致不支持的类文件主要版本 61 错误
【发布时间】:2021-12-16 07:06:40
【问题描述】:

我正在尝试构建一个 docker 映像,但我收到一条错误消息,告诉我 jib-maven-plugin 失败。导致不支持的类文件主要版本 61 错误。

起初我认为这与我使用的 Java 版本(Java 17)有关。所以我从我的机器上卸载了它并安装了 Java 15 但没有成功。

我正在尝试运行的命令:

./mvnw compile jib:dockerBuild -Djib.to.image=fullstack:v1

我得到的错误响应:

Failed to execute goal com.google.cloud.tools:jib-maven-plugin:2.5.2:dockerBuild (default-cli) on project demo: Execution default-cli of goal com.google.cloud.tools:jib-maven-plugin:2.5.2:dockerBuild failed: Unsupported class file major version 61 -> [Help 1]

我的 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>2.4.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>15</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
            <!-- The plugin below is to make a docker image using Jib -->
            <plugin>
                <groupId>com.google.cloud.tools</groupId>
                <artifactId>jib-maven-plugin</artifactId>
                <version>2.5.2</version>
                <configuration>
                    <from>
                        <image>openjdk:15</image>
                    </from>
                    <container>
                        <ports>
                            <port>8080</port>
                        </ports>
                        <format>OCI</format>
                    </container>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <!-- The code below is for packaging the frontend with the backend using maven -->
    <profiles>
        <profile>
            <id>build-frontend</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <build>
                <plugins>

                    <plugin>
                        <groupId>com.github.eirslett</groupId>
                        <artifactId>frontend-maven-plugin</artifactId>
                        <!-- Use the latest released version:
                                  https://repo1.maven.org/maven2/com/github/eirslett/frontend-maven-plugin/ -->
                        <version>1.11.2</version>
                        <configuration>
                            <nodeVersion>v4.6.0</nodeVersion>
                            <workingDirectory>src/frontend</workingDirectory>
                        </configuration>
                        <executions>
                            <execution>
                                <id>install node and npm</id>
                                <goals>
                                    <goal>install-node-and-npm</goal>
                                </goals>
                                <configuration>
                                    <nodeVersion>v15.4.0</nodeVersion>
                                    <npmVersion>7.3.0</npmVersion>
                                </configuration>
                            </execution>
                            <execution>
                                <id>npm install</id>
                                <goals>
                                    <goal>npm</goal>
                                </goals>
                                <configuration>
                                    <arguments>install</arguments>
                                </configuration>
                            </execution>
                            <execution>
                                <id>npm run build</id>
                                <goals>
                                    <goal>npm</goal>
                                </goals>
                                <configuration>
                                    <arguments>run build</arguments>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                    <!-- The plugin below is for copying the build folder into the target static folder (maven) -->
                    <plugin>
                        <artifactId>maven-resources-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>copy-build-folder</id>
                                <phase>process-classes</phase>
                                <goals>
                                    <goal>copy-resources</goal>
                                </goals>
                                <configuration>
                                    <resources>
                                        <resource>
                                            <directory>src/frontend/build</directory>
                                        </resource>

                                    </resources>
                                    <outputDirectory>${basedir}/target/classes/static</outputDirectory>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

</project>

【问题讨论】:

  • 我想知道这是否是因为 Jib 用于自动推断主类的 ASM 库; ASM 库可能尚不支持 Java 17。如果您通过 &lt;container&gt;&lt;mainClass&gt; 手动指定 main 怎么办?另外,我建议将 Jib 升级到 3.1.4。
  • 另外,运行 mvn -X ... 以获取完整的堆栈跟踪并使用它更新问题。
  • 错误信息 unsupported 61 表示 JDK 16...
  • @ChanseokOh 将&lt;container&gt;&lt;mainClass&gt; 添加到我的 pom.xml 确实解决了问题,谢谢!如果您愿意,可以将其添加为答案。
  • @khmarbaise 这实际上不是真的。版本 61 表示 JDK 17。Source

标签: java docker maven docker-build jib


【解决方案1】:

UPDATE(01/20/2022):已发布带有错误修复的新 Jib 版本。升级 Jib 即可解决。

但是,几年后,当您使用 Java 18+ 左右时,您可能会再次遇到此问题。即便如此,也有一种解决方法。


基本上,这是 Jib 中的 bug

Jib 使用 ASM 库检查编译后的 Java 字节码以自动推断出一个主类(即定义 public static void main() 的类)。这样,如果您只有一个这样的类,Jib 可以自动推断并使用该类作为图像入口点。

这种情况的原因是,Jib 当前没有使用能够识别和理解较新 Java 版本的字节码的最新 ASM 库。 Jib 团队需要升级库并发布新的 Jib。

解决方法:为防止 Jib 进行自动推理,您可以通过 &lt;container&gt;&lt;mainClass&gt; 手动设置所需的主类,例如 &lt;container&gt;&lt;mainClass&gt;com.example.your.Main&lt;/mainClass&gt;。与其他 Jib 参数一样,这可以通过属性或命令行设置,例如,-Dcontainer.mainClass=...


注意,虽然在这种情况下错误是由 ASM 引起的,但当然也有可能由于其他原因而遇到此错误。您可能希望使用 -e-X 运行 Maven,以获取完整的堆栈跟踪以查看错误来自何处。

【讨论】:

  • 解释得很好,这确实解决了我的问题!
  • 谢谢 Chanseok,它帮助了我!
猜你喜欢
  • 2023-03-03
  • 2022-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-24
  • 2019-05-04
  • 1970-01-01
相关资源
最近更新 更多