英文:
Spring Boot: GraalVM Native Image Support
问题
Sure, here's the translated content you requested:
我有一个Spring Boot项目,最初使用的是Java 11的2.7.6版本,当前正在添加[GraalVM原生镜像支持][1],按照[使用Buildpacks构建原生镜像][2]的文档进行操作。我已经将版本更改为Java 17的3.1.0版本,并修改了pom.xml。我还对spring-data-jpa进行了更改,重构了导入语句,现在可以运行该应用程序。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.0</version>
</parent>
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
</plugin>
我对添加GraalVM的理解是它将加快应用程序的启动时间并减小jar文件或应用程序的Docker镜像的构建大小。以下是比较结果。
在GraalVM之前:
应用程序启动时间约为7秒
运行 mvn clean install
的时间约为15秒
Jar文件大小约为61MB
在GraalVM之后:
应用程序启动时间约为2秒
运行 mvn clean install -Pnative
的时间约为12秒
Jar文件大小约为77MB
问题如下:
-
我可以看到应用程序启动时间的性能提升,这很好,但是jar文件的大小已经增加,应该小于约61MB。
-
我是否使用了正确的GraalVM构建命令,即使用
mvn clean install -Pnative
? -
在文档中有如下说明,我是否需要在pom.xml中添加任何内容?
用于镜像的构建器是paketobuildpacks/builder:tiny。它具有小的足迹和减小的攻击面,但如果需要,您也可以使用paketobuildpacks/builder:base或paketobuildpacks/builder:full,以在镜像中获得更多工具。
如果我在spring-boot-maven-plugin的配置中添加以下内容,我会收到共享的错误消息:
<image>
<buildpacks>
<buildpack>paketobuildpacks/builder:full</buildpack>
</buildpacks>
<env>
<BP_NATIVE_IMAGE>true</BP_NATIVE_IMAGE>
</env>
</image>
class org.springframework.boot.maven.Image无法转换为class org.springframework.boot.maven.FilterableDependency(org.springframework.boot.maven.Image和org.springframework.boot.maven.FilterableDependency位于加载器org.codehaus.plexus.classworlds.realm.ClassRealm @6228d0bd的未命名模块中)
更新:
当我运行 mvn -Pnative native:compile
时,我收到以下错误。
错误:应该在运行时初始化的类在图像构建期间被初始化:
在构建时意外初始化了org.apache.commons.logging.LogFactory。要查看为什么初始化了org.apache.commons.logging.LogFactory,请使用--trace-class-initialization=org.apache.commons.logging.LogFactory
在构建时意外初始化了org.apache.commons.logging.LogFactoryService。要查看为什么初始化了org.apache.commons.logging.LogFactoryService,请使用--trace-class-initialization=org.apache.commons.logging.LogFactoryService
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.1.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.xyz</groupId>
<artifactId>abc</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>abc</name>
<description>Application</description>
<properties>
<java.version>17</java.version>
<springdoc-openapi-ui.version>1.6.13</springdoc-openapi-ui.version>
<problem.version>0.26.0</problem.version>
<org.json.version>20220320</org.json.version>
<apache.commons.lang3.version>3.12.0</apache.commons.lang3.version>
<checkstyle-plugin.version>3.2.0</checkstyle-plugin.version>
<checkstyle.version>10.5.0</checkstyle.version>
</properties>
<dependencies>
<!-- Your dependencies here -->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<image>
<buildpacks>
<buildpack>paketobuildpacks/builder:full</buildpack>
</buildpacks>
<env>
<BP_NATIVE_IMAGE>true</BP_NATIVE_IMAGE>
</env>
</image>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
<executions>
<execution>
<goals>
<goal>build-info</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
</plugin>
<!-- Other plugins -->
</plugins>
</build>
</project>
控制台中还会看到以下日志:
<details>
<summary>英文:</summary>
I have a Spring Boot project which was initially using version 2.7.6 with Java 11 and currently, I am adding the [GraalVM Native Image Support][1] by following the documentation with [Building a Native Image Using Buildpacks][2] option. I have changed the version to 3.1.0 with Java 17 and modified the pom.xml. I have also done the changes regarding spring-data-jpa and refactored the import statements and now I am able to run the application.
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.0</version>
</parent>
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
</plugin>
My understanding of adding the GraalVM is that it will make the application start-up time faster and reduce the build size of jar file or the docker image of an application. Below is the comparison.
**Before GraalVM:** <br>
Application start in ~7s <br>
Time to run `mvn clean install` ~15s <br>
Jar file size ~61MB
**After GraalVM:** <br>
Application start in : ~2s <br>
Time to run `mvn clean install -Pnative` ~12s <br>
Jar file size ~77MB
**Questions are:**
1. I can see the performance on the application startup time which is good, however jar file size has increased which is supposed to be less than ~61MB
2. Does I am using the correct command for build with GraalVM by using `mvn clean install -Pnative`
3. In the documentation there is note as below, do I need to add anything in the pom.xml?
> The builder used for the images is paketobuildpacks/builder:tiny. It
> has small footprint and reduced attack surface, but you can also use
> paketobuildpacks/builder:base or paketobuildpacks/builder:full to have
> more tools available in the image if required.
If I do add below in the configurations of spring-boot-maven-plugin, I am getting the shared error message
<image>
<buildpacks>
<buildpack>paketobuildpacks/builder:full</buildpack>
</buildpacks>
<env>
<BP_NATIVE_IMAGE>true</BP_NATIVE_IMAGE>
</env>
</image>
**class org.springframework.boot.maven.Image cannot be cast to class org.springframework.boot.maven.FilterableDependency (org.springframework.boot.maven.Image and org.springframework.boot.maven.FilterableDependency are in unnamed module of loader org.codehaus.plexus.classworlds.realm.ClassRealm @6228d0bd)**
----------
**UPDATE:**
When I do run `mvn -Pnative native:compile` I am getting the below error.
**Error: Classes that should be initialized at run time got initialized during image building:
org.apache.commons.logging.LogFactory was unintentionally initialized at build time. To see why org.apache.commons.logging.LogFactory got initialized use --trace-class-initialization=org.apache.commons.logging.LogFactory
org.apache.commons.logging.LogFactoryService was unintentionally initialized at build time. To see why org.apache.commons.logging.LogFactoryService got initialized use --trace-class-initialization=org.apache.commons.logging.LogFactoryService**
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.1.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.xyz</groupId>
<artifactId>abc</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>abc</name>
<description>Application</description>
<properties>
<java.version>17</java.version>
<springdoc-openapi-ui.version>1.6.13</springdoc-openapi-ui.version>
<problem.version>0.26.0</problem.version>
<org.json.version>20220320</org.json.version>
<apache.commons.lang3.version>3.12.0</apache.commons.lang3.version>
<checkstyle-plugin.version>3.2.0</checkstyle-plugin.version>
<checkstyle.version>10.5.0</checkstyle.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>${org.json.version}</version>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${apache.commons.lang3.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.4</version>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>org.zalando</groupId>
<artifactId>problem-spring-web</artifactId>
<version>${problem.version}</version>
</dependency>
<dependency>
<groupId>org.zalando</groupId>
<artifactId>jackson-datatype-problem</artifactId>
<version>${problem.version}</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>1.5.3.Final</version>
</dependency>
<dependency>
<groupId>com.github.xeroapi</groupId>
<artifactId>xero-java</artifactId>
<version>4.26.0</version>
</dependency>
<dependency>
<groupId>com.google.http-client</groupId>
<artifactId>google-http-client</artifactId>
<version>1.43.1</version>
</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>
<image>
<buildpacks>
<buildpack>paketobuildpacks/builder:full</buildpack>
</buildpacks>
<env>
<BP_NATIVE_IMAGE>true</BP_NATIVE_IMAGE>
</env>
</image>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
<executions>
<execution>
<goals>
<goal>build-info</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
<version>3.3.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<source>17</source>
<target>17</target>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.5.3.Final</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.26</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-mapstruct-binding</artifactId>
<version>0.2.0</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin>
<groupId>pl.project13.maven</groupId>
<artifactId>git-commit-id-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>${checkstyle-plugin.version}</version>
<dependencies>
<dependency>
<groupId>com.puppycrawl.tools</groupId>
<artifactId>checkstyle</artifactId>
<version>${checkstyle.version}</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>checkstyle</id>
<phase>validate</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
<configuration>
<configLocation>checkstyle.xml</configLocation>
<excludes>**/module-info.java,**/target/**/*,**/protocols/**/*</excludes>
<consoleOutput>true</consoleOutput>
<failsOnError>true</failsOnError>
<violationSeverity>warning</violationSeverity>
<failOnViolation>true</failOnViolation>
<linkXRef>false</linkXRef>
</configuration>
</plugin>
</plugins>
</build>
</project>
In console I can also see the below logs
Warning: Could not resolve org.jboss.logmanager.LogManager for reflection configuration. Reason: java.lang.ClassNotFoundException: org.jboss.logmanager.LogManager.
Warning: Could not resolve com.sun.el.ExpressionFactoryImpl for reflection configuration. Reason: java.lang.ClassNotFoundException: com.sun.el.ExpressionFactoryImpl.
Warning: Could not resolve groovy.grape.GrabAnnotationTransformation for reflection configuration. Reason: java.lang.ClassNotFoundException: groovy.grape.GrabAnnotationTransformation.
Warning: Could not resolve groovy.lang.Script for reflection configuration. Reason: java.lang.ClassNotFoundException: groovy.lang.Script.
Warning: Could not resolve groovy.lang.Script for reflection configuration. Reason: java.lang.ClassNotFoundException: groovy.lang.Script.
Warning: Could not resolve groovyjarjarantlr.CommonToken for reflection configuration. Reason: java.lang.ClassNotFoundException: groovyjarjarantlr.CommonToken.
Warning: Could not resolve javafx.beans.value.ObservableValue for reflection configuration. Reason: java.lang.ClassNotFoundException: javafx.beans.value.ObservableValue.
Warning: Could not resolve javax.money.MonetaryAmount for reflection configuration. Reason: java.lang.ClassNotFoundException: javax.money.MonetaryAmount.
Warning: Could not resolve org.codehaus.groovy.antlr.GroovySourceAST for reflection configuration. Reason: java.lang.ClassNotFoundException: org.codehaus.groovy.antlr.GroovySourceAST.
Warning: Could not resolve org.codehaus.groovy.ast.builder.AstBuilderTransformation for reflection configuration. Reason: java.lang.ClassNotFoundException: org.codehaus.groovy.ast.builder.AstBuilderTransformation.
Warning: Could not resolve org.codehaus.groovy.runtime.ScriptBytecodeAdapter for reflection configuration. Reason: java.lang.ClassNotFoundException: org.codehaus.groovy.runtime.ScriptBytecodeAdapter.
Warning: Could not resolve org.glassfish.expressly.ValueExpressionImpl for reflection configuration. Reason: java.lang.ClassNotFoundException: org.glassfish.expressly.ValueExpressionImpl.
Warning: Could not resolve org.glassfish.expressly.parser.AstValue for reflection configuration. Reason: java.lang.ClassNotFoundException: org.glassfish.expressly.parser.AstValue.
Warning: Could not resolve org.joda.time.ReadableInstant for reflection configuration. Reason: java.lang.ClassNotFoundException: org.joda.time.ReadableInstant.
Warning: Could not register method org.hibernate.validator.internal.constraintvalidators.bv.time.future.FutureValidatorForReadablePartial.<init>() for reflection. Reason: java.lang.NoClassDefFoundError: org/joda/time/ReadableInstant.
Warning: Could not register method org.hibernate.validator.internal.constraintvalidators.bv.time.futureorpresent.FutureOrPresentValidatorForReadablePartial.<init>() for reflection. Reason: java.lang.NoClassDefFoundError: org/joda/time/ReadableInstant.
Warning: Could not register method org.hibernate.validator.internal.constraintvalidators.bv.time.past.PastValidatorForReadablePartial.<init>() for reflection. Reason: java.lang.NoClassDefFoundError: org/joda/time/ReadableInstant.
Warning: Could not register method org.hibernate.validator.internal.constraintvalidators.bv.time.pastorpresent.PastOrPresentValidatorForReadablePartial.<init>() for reflection. Reason: java.lang.NoClassDefFoundError: org/joda/time/ReadableInstant.
Warning: Method com.zaxxer.hikari.HikariConfig.getScheduledExecutorService() not found.
Warning: Method com.zaxxer.hikari.HikariConfig.isInitializationFailFast() not found.
Warning: Method com.zaxxer.hikari.HikariConfig.isJdbc4ConnectionTest() not found.
Warning: Method com.zaxxer.hikari.HikariConfig.setInitializationFailFast(boolean) not found.
Warning: Method com.zaxxer.hikari.HikariConfig.setJdbc4ConnectionTest(boolean) not found.
Warning: Method com.zaxxer.hikari.HikariConfig.setScheduledExecutorService(ScheduledThreadPoolExecutor) not found.
Warning: Could not resolve oracle.jdbc.OracleConnection for reflection configuration. Reason: java.lang.ClassNotFoundException: oracle.jdbc.OracleConnection.
Warning: Could not resolve org.h2.Driver for reflection configuration. Reason: java.lang.ClassNotFoundException: org.h2.Driver.
Warning: Could not resolve org.postgresql.util.PGobject for reflection configuration. Reason: java.lang.ClassNotFoundException: org.postgresql.util.PGobject.
Warning: Method com.zaxxer.hikari.HikariConfig.getScheduledExecutorService() not found.
Warning: Method com.zaxxer.hikari.HikariConfig.isInitializationFailFast() not found.
Warning: Method com.zaxxer.hikari.HikariConfig.isJdbc4ConnectionTest() not found.
Warning: Method com.zaxxer.hikari.HikariConfig.setInitializationFailFast(boolean) not found.
Warning: Method com.zaxxer.hikari.HikariConfig.setJdbc4ConnectionTest(boolean) not found.
Warning: Method com.zaxxer.hikari.HikariConfig.setScheduledExecutorService(ScheduledThreadPoolExecutor) not found.
Warning: Could not resolve org.hibernate.service.jta.platform.internal.NoJtaPlatform for reflection configuration. Reason: java.lang.ClassNotFoundException: org.hibernate.service.jta.platform.internal.NoJtaPlatform.
Warning: Could not resolve oracle.jdbc.OracleConnection for reflection configuration. Reason: java.lang.ClassNotFoundException: oracle.jdbc.OracleConnection.
Warning: Could not resolve org.h2.Driver for reflection configuration. Reason: java.lang.ClassNotFoundException: org.h2.Driver.
Warning: Could not resolve org.postgresql.util.PGobject for reflection configuration. Reason: java.lang.ClassNotFoundException: org.postgresql.util.PGobject.
Warning: Could not resolve org.jboss.logmanager.LogManager for reflection configuration. Reason: java.lang.ClassNotFoundException: org.jboss.logmanager.LogManager.
Warning: Could not resolve com.sun.el.ExpressionFactoryImpl for reflection configuration. Reason: java.lang.ClassNotFoundException: com.sun.el.ExpressionFactoryImpl.
Warning: Could not resolve groovy.grape.GrabAnnotationTransformation for reflection configuration. Reason: java.lang.ClassNotFoundException: groovy.grape.GrabAnnotationTransformation.
Warning: Could not resolve groovy.lang.Script for reflection configuration. Reason: java.lang.ClassNotFoundException: groovy.lang.Script.
Warning: Could not resolve groovy.lang.Script for reflection configuration. Reason: java.lang.ClassNotFoundException: groovy.lang.Script.
Warning: Could not resolve groovyjarjarantlr.CommonToken for reflection configuration. Reason: java.lang.ClassNotFoundException: groovyjarjarantlr.CommonToken.
Warning: Could not resolve javafx.beans.value.ObservableValue for reflection configuration. Reason: java.lang.ClassNotFoundException: javafx.beans.value.ObservableValue.
Warning: Could not resolve javax.money.MonetaryAmount for reflection configuration. Reason: java.lang.ClassNotFoundException: javax.money.MonetaryAmount.
Warning: Could not resolve org.codehaus.groovy.antlr.GroovySourceAST for reflection configuration. Reason: java.lang.ClassNotFoundException: org.codehaus.groovy.antlr.GroovySourceAST.
Warning: Could not resolve org.codehaus.groovy.ast.builder.AstBuilderTransformation for reflection configuration. Reason: java.lang.ClassNotFoundException: org.codehaus.groovy.ast.builder.AstBuilderTransformation.
Warning: Could not resolve org.codehaus.groovy.runtime.ScriptBytecodeAdapter for reflection configuration. Reason: java.lang.ClassNotFoundException: org.codehaus.groovy.runtime.ScriptBytecodeAdapter.
Warning: Could not resolve org.glassfish.expressly.ValueExpressionImpl for reflection configuration. Reason: java.lang.ClassNotFoundException: org.glassfish.expressly.ValueExpressionImpl.
Warning: Could not resolve org.glassfish.expressly.parser.AstValue for reflection configuration. Reason: java.lang.ClassNotFoundException: org.glassfish.expressly.parser.AstValue.
Warning: Could not resolve org.joda.time.ReadableInstant for reflection configuration. Reason: java.lang.ClassNotFoundException: org.joda.time.ReadableInstant.
Warning: Could not register method org.hibernate.validator.internal.constraintvalidators.bv.time.future.FutureValidatorForReadablePartial.<init>() for reflection. Reason: java.lang.NoClassDefFoundError: org/joda/time/ReadableInstant.
Warning: Could not register method org.hibernate.validator.internal.constraintvalidators.bv.time.futureorpresent.FutureOrPresentValidatorForReadablePartial.<init>() for reflection. Reason: java.lang.NoClassDefFoundError: org/joda/time/ReadableInstant.
Warning: Could not register method org.hibernate.validator.internal.constraintvalidators.bv.time.past.PastValidatorForReadablePartial.<init>() for reflection. Reason: java.lang.NoClassDefFoundError: org/joda/time/ReadableInstant.
Warning: Could not register method org.hibernate.validator.internal.constraintvalidators.bv.time.pastorpresent.PastOrPresentValidatorForReadablePartial.<init>() for reflection. Reason: java.lang.NoClassDefFoundError: org/joda/time/ReadableInstant.
[1]: https://docs.spring.io/spring-boot/docs/current/reference/html/native-image.html
[2]: https://docs.spring.io/spring-boot/docs/current/reference/html/native-image.html#native-image.developing-your-first-application.buildpacks
</details>
# 答案1
**得分**: 1
I had the same problem of your update and I fixed it by adding this args configuration in native-maven-plugin.
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
<configuration>
<buildArgs>
<arg>
--initialize-at-build-time=org.apache.commons.logging.LogFactory,org.apache.commons.logging.LogFactoryService,
</arg>
</buildArgs>
</configuration>
</plugin>
<details>
<summary>英文:</summary>
I hade the same problem of your update and I fixed it by adding this args configuration in native-maven-plugin.
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
<configuration>
<buildArgs>
<arg>
--initialize-at-build-time=org.apache.commons.logging.LogFactory,org.apache.commons.logging.LogFactoryService,
</arg>
</buildArgs>
</configuration>
</plugin>
</details>
# 答案2
**得分**: 0
我可以看到应用程序启动时间的性能很好,但是jar文件大小已经增加,应该小于~61MB。
本地镜像是一个特定平台的二进制文件,可以在没有JVM的情况下执行。jar文件的大小并不重要。
我是否正在使用正确的命令来使用GraalVM构建,使用mvn clean install -Pnative?
如果你想要使用构建包来创建一个包含本地可执行文件的容器镜像,你应该运行 `mvn -Pnative spring-boot:build-image`。
如果你想在你的机器上直接构建本地可执行文件,你应该运行 `mvn -Pnative native:compile`。你还需要添加本地构建工具Maven插件:
```xml
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
</plugin>
如果我在spring-boot-maven-plugin的配置中添加以下内容,我会得到共享的错误消息。
只看到配置片段并没有堆栈跟踪,很难确定,但我怀疑你将配置放在了错误的标签中。它应该类似于这样:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<image>
<buildpacks>
<buildpack>paketobuildpacks/builder:full</buildpack>
</buildpacks>
<env>
<BP_NATIVE_IMAGE>true</BP_NATIVE_IMAGE>
</env>
</image>
</configuration>
</plugin>
英文:
> I can see the performance on the application startup time which is good, however jar file size has increased which is supposed to be less than ~61MB
A native image is a platform-specific binary that can be executed without a JVM. The size of the jar file isn't relevant.
> Does I am using the correct command for build with GraalVM by using mvn clean install -Pnative
If you want to use buildpacks to create a container image that contains a native executable you should run mvn -Pnative spring-boot:build-image
.
If you want to build a native executable directly on your machine, you should run mvn -Pnative native:compile
. You also need to add the Native Build Tools Maven plugin:
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
</plugin>
> If I do add below in the configurations of spring-boot-maven-plugin, I am getting the shared error message
It's hard to tell with only a snippet of configuration and no stack trace but I suspect you've placed the configuration within the wrong tags. It should look something like this:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<image>
<buildpacks>
<buildpack>paketobuildpacks/builder:full</buildpack>
</buildpacks>
<env>
<BP_NATIVE_IMAGE>true</BP_NATIVE_IMAGE>
</env>
</image>
</configuration>
</plugin>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论