我正在尝试构建docker映像,但我收到一个错误,告诉我jib-maven-plugin失败。导致不受支持的类文件主要版本61错误。
起初我认为这和我使用的java版本有关(Java17)。所以我从我的机器上卸载了它,安装Java15,但没有成功。
我尝试运行的命令:
./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>
更新(2022年1月20日):发布了带有bug修复的新Jib版本。升级Jib将修复它。
然而,几年后当你使用Java18岁左右时,你可能会再次遇到这个问题。即便如此,还是有一个解决方法。
基本上,这是Jib中的bug。
Jib使用ASM库来检查编译Java字节码,以自动推断出一个主类(即,定义public静态void main()
的类)。这样,如果您只有一个这样的类,Jib可以自动推断并将该类用于图像入口点。
这种情况下的原因是,Jib目前没有使用能够识别和理解较新Java版本字节码的最新ASM库。Jib团队需要升级该库并制作新的Jib版本。
解决方法:为了防止Jib进行自动推理,您可以通过手动设置所需的主类
请注意,虽然在这种情况下错误是由于ASM造成的,但当然也有可能由于其他原因导致此错误。您可能希望使用-e
或-X
运行Maven以获取完整的堆栈跟踪以查看错误来自何处。
我能够通过将我的jib-maven-plugin升级到其最新版本(目前为3.2.1)来修复它。因此,在您的pom. xml
文件中更新它,并通过右键单击pom.xml
文件重新加载项目,选择“Maven”和“Reload project”。
您的jib-maven-plugin
应如下所示:
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
<version>3.2.1</version>
<configuration>
<container>
<ports>
<port>8080</port>
</ports>
<format>OCI</format>
</container>
</configuration>
</plugin>
希望这会有所帮助。