Spring Boot: GraalVM原生镜像支持

huangapple go评论82阅读模式
英文:

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

问题如下:

  1. 我可以看到应用程序启动时间的性能提升,这很好,但是jar文件的大小已经增加,应该小于约61MB。

  2. 我是否使用了正确的GraalVM构建命令,即使用 mvn clean install -Pnative

  3. 在文档中有如下说明,我是否需要在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.  

    &lt;parent&gt;
        &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
        &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
        &lt;version&gt;3.1.0&lt;/version&gt;
    &lt;/parent&gt;
    
    &lt;plugin&gt;
        &lt;groupId&gt;org.graalvm.buildtools&lt;/groupId&gt;
        &lt;artifactId&gt;native-maven-plugin&lt;/artifactId&gt;
    &lt;/plugin&gt;


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:**  &lt;br&gt;
Application start in ~7s &lt;br&gt;
Time to run `mvn clean install` ~15s  &lt;br&gt;
Jar file size ~61MB 

**After GraalVM:**  &lt;br&gt;
Application start in : ~2s  &lt;br&gt;
Time to run `mvn clean install -Pnative` ~12s  &lt;br&gt;
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? 

&gt; The builder used for the images is paketobuildpacks/builder:tiny. It
&gt; has small footprint and reduced attack surface, but you can also use
&gt; paketobuildpacks/builder:base or paketobuildpacks/builder:full to have
&gt; 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

					&lt;image&gt;
						&lt;buildpacks&gt;
							&lt;buildpack&gt;paketobuildpacks/builder:full&lt;/buildpack&gt;
						&lt;/buildpacks&gt;
						&lt;env&gt;
							&lt;BP_NATIVE_IMAGE&gt;true&lt;/BP_NATIVE_IMAGE&gt;
						&lt;/env&gt;
					&lt;/image&gt;

 
**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

    &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
    &lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
    	xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
    	&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
    	&lt;parent&gt;
    		&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    		&lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
    		&lt;version&gt;3.1.0&lt;/version&gt;
    		&lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
    	&lt;/parent&gt;
    	&lt;groupId&gt;com.xyz&lt;/groupId&gt;
    	&lt;artifactId&gt;abc&lt;/artifactId&gt;
    	&lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
    	&lt;name&gt;abc&lt;/name&gt;
    	&lt;description&gt;Application&lt;/description&gt;
    	&lt;properties&gt;
    		&lt;java.version&gt;17&lt;/java.version&gt;
    		&lt;springdoc-openapi-ui.version&gt;1.6.13&lt;/springdoc-openapi-ui.version&gt;
    		&lt;problem.version&gt;0.26.0&lt;/problem.version&gt;
    		&lt;org.json.version&gt;20220320&lt;/org.json.version&gt;
    		&lt;apache.commons.lang3.version&gt;3.12.0&lt;/apache.commons.lang3.version&gt;
    		&lt;checkstyle-plugin.version&gt;3.2.0&lt;/checkstyle-plugin.version&gt;
    		&lt;checkstyle.version&gt;10.5.0&lt;/checkstyle.version&gt;
    	&lt;/properties&gt;
    	&lt;dependencies&gt;
    		&lt;dependency&gt;
    			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    			&lt;artifactId&gt;spring-boot-starter-actuator&lt;/artifactId&gt;
    		&lt;/dependency&gt;
    		&lt;dependency&gt;
    			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    			&lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
    		&lt;/dependency&gt;
    		&lt;dependency&gt;
    			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    			&lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
    		&lt;/dependency&gt;
    		&lt;dependency&gt;
    			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    			&lt;artifactId&gt;spring-boot-starter-validation&lt;/artifactId&gt;
    		&lt;/dependency&gt;
    		&lt;dependency&gt;
    			&lt;groupId&gt;org.json&lt;/groupId&gt;
    			&lt;artifactId&gt;json&lt;/artifactId&gt;
    			&lt;version&gt;${org.json.version}&lt;/version&gt;
    		&lt;/dependency&gt;
    		&lt;dependency&gt;
    			&lt;groupId&gt;com.microsoft.sqlserver&lt;/groupId&gt;
    			&lt;artifactId&gt;mssql-jdbc&lt;/artifactId&gt;
    		&lt;/dependency&gt;
    		&lt;dependency&gt;
    			&lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
    			&lt;artifactId&gt;lombok&lt;/artifactId&gt;
    			&lt;optional&gt;true&lt;/optional&gt;
    		&lt;/dependency&gt;
    		&lt;dependency&gt;
    			&lt;groupId&gt;org.apache.commons&lt;/groupId&gt;
    			&lt;artifactId&gt;commons-lang3&lt;/artifactId&gt;
    			&lt;version&gt;${apache.commons.lang3.version}&lt;/version&gt;
    		&lt;/dependency&gt;
    		&lt;dependency&gt;
    			&lt;groupId&gt;org.apache.commons&lt;/groupId&gt;
    			&lt;artifactId&gt;commons-collections4&lt;/artifactId&gt;
    			&lt;version&gt;4.4&lt;/version&gt;
    		&lt;/dependency&gt;
    		&lt;dependency&gt;
    			&lt;groupId&gt;org.springdoc&lt;/groupId&gt;
    			&lt;artifactId&gt;springdoc-openapi-starter-webmvc-ui&lt;/artifactId&gt;
    			&lt;version&gt;2.0.2&lt;/version&gt;
    		&lt;/dependency&gt;
    		&lt;dependency&gt;
    			&lt;groupId&gt;org.zalando&lt;/groupId&gt;
    			&lt;artifactId&gt;problem-spring-web&lt;/artifactId&gt;
    			&lt;version&gt;${problem.version}&lt;/version&gt;
    		&lt;/dependency&gt;
    		&lt;dependency&gt;
    			&lt;groupId&gt;org.zalando&lt;/groupId&gt;
    			&lt;artifactId&gt;jackson-datatype-problem&lt;/artifactId&gt;
    			&lt;version&gt;${problem.version}&lt;/version&gt;
    		&lt;/dependency&gt;
    		&lt;dependency&gt;
    			&lt;groupId&gt;org.mapstruct&lt;/groupId&gt;
    			&lt;artifactId&gt;mapstruct&lt;/artifactId&gt;
    			&lt;version&gt;1.5.3.Final&lt;/version&gt;
    		&lt;/dependency&gt;
    		&lt;dependency&gt;
    			&lt;groupId&gt;com.github.xeroapi&lt;/groupId&gt;
    			&lt;artifactId&gt;xero-java&lt;/artifactId&gt;
    			&lt;version&gt;4.26.0&lt;/version&gt;
    		&lt;/dependency&gt;
    		&lt;dependency&gt;
    			&lt;groupId&gt;com.google.http-client&lt;/groupId&gt;
    			&lt;artifactId&gt;google-http-client&lt;/artifactId&gt;
    			&lt;version&gt;1.43.1&lt;/version&gt;
    		&lt;/dependency&gt;
    		&lt;dependency&gt;
    			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    			&lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
    			&lt;scope&gt;test&lt;/scope&gt;
    		&lt;/dependency&gt;
    	&lt;/dependencies&gt;
    
    	&lt;build&gt;
    		&lt;plugins&gt;
    			&lt;plugin&gt;
    				&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    				&lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
    				&lt;configuration&gt;
    					&lt;image&gt;
    						&lt;buildpacks&gt;
    							&lt;buildpack&gt;paketobuildpacks/builder:full&lt;/buildpack&gt;
    						&lt;/buildpacks&gt;
    						&lt;env&gt;
    							&lt;BP_NATIVE_IMAGE&gt;true&lt;/BP_NATIVE_IMAGE&gt;
    						&lt;/env&gt;
    					&lt;/image&gt;
    					&lt;excludes&gt;
    						&lt;exclude&gt;
    							&lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
    							&lt;artifactId&gt;lombok&lt;/artifactId&gt;
    						&lt;/exclude&gt;
    					&lt;/excludes&gt;
    				&lt;/configuration&gt;
    				&lt;executions&gt;
    					&lt;execution&gt;
    						&lt;goals&gt;
    							&lt;goal&gt;build-info&lt;/goal&gt;
    						&lt;/goals&gt;
    					&lt;/execution&gt;
    				&lt;/executions&gt;
    			&lt;/plugin&gt;
    			&lt;plugin&gt;
    				&lt;groupId&gt;org.graalvm.buildtools&lt;/groupId&gt;
    				&lt;artifactId&gt;native-maven-plugin&lt;/artifactId&gt;
    			&lt;/plugin&gt;
    			&lt;plugin&gt;
    				&lt;groupId&gt;com.google.cloud.tools&lt;/groupId&gt;
    				&lt;artifactId&gt;jib-maven-plugin&lt;/artifactId&gt;
    				&lt;version&gt;3.3.1&lt;/version&gt;
    			&lt;/plugin&gt;
    			&lt;plugin&gt;
    				&lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
    				&lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
    				&lt;version&gt;3.10.1&lt;/version&gt;
    				&lt;configuration&gt;
    					&lt;source&gt;17&lt;/source&gt;
    					&lt;target&gt;17&lt;/target&gt;
    					&lt;annotationProcessorPaths&gt;
    						&lt;path&gt;
    							&lt;groupId&gt;org.mapstruct&lt;/groupId&gt;
    							&lt;artifactId&gt;mapstruct-processor&lt;/artifactId&gt;
    							&lt;version&gt;1.5.3.Final&lt;/version&gt;
    						&lt;/path&gt;
    						&lt;path&gt;
    							&lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
    							&lt;artifactId&gt;lombok&lt;/artifactId&gt;
    							&lt;version&gt;1.18.26&lt;/version&gt;
    						&lt;/path&gt;
    						&lt;path&gt;
    							&lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
    							&lt;artifactId&gt;lombok-mapstruct-binding&lt;/artifactId&gt;
    							&lt;version&gt;0.2.0&lt;/version&gt;
    						&lt;/path&gt;
    					&lt;/annotationProcessorPaths&gt;
    				&lt;/configuration&gt;
    			&lt;/plugin&gt;
    			&lt;plugin&gt;
    				&lt;groupId&gt;pl.project13.maven&lt;/groupId&gt;
    				&lt;artifactId&gt;git-commit-id-plugin&lt;/artifactId&gt;
    			&lt;/plugin&gt;
    			&lt;plugin&gt;
    				&lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
    				&lt;artifactId&gt;maven-checkstyle-plugin&lt;/artifactId&gt;
    				&lt;version&gt;${checkstyle-plugin.version}&lt;/version&gt;
    				&lt;dependencies&gt;
    					&lt;dependency&gt;
    						&lt;groupId&gt;com.puppycrawl.tools&lt;/groupId&gt;
    						&lt;artifactId&gt;checkstyle&lt;/artifactId&gt;
    						&lt;version&gt;${checkstyle.version}&lt;/version&gt;
    					&lt;/dependency&gt;
    				&lt;/dependencies&gt;
    				&lt;executions&gt;
    					&lt;execution&gt;
    						&lt;id&gt;checkstyle&lt;/id&gt;
    						&lt;phase&gt;validate&lt;/phase&gt;
    						&lt;goals&gt;
    							&lt;goal&gt;check&lt;/goal&gt;
    						&lt;/goals&gt;
    					&lt;/execution&gt;
    				&lt;/executions&gt;
    				&lt;configuration&gt;
    					&lt;configLocation&gt;checkstyle.xml&lt;/configLocation&gt;
    					&lt;excludes&gt;**/module-info.java,**/target/**/*,**/protocols/**/*&lt;/excludes&gt;
    					&lt;consoleOutput&gt;true&lt;/consoleOutput&gt;
    					&lt;failsOnError&gt;true&lt;/failsOnError&gt;
    					&lt;violationSeverity&gt;warning&lt;/violationSeverity&gt;
    					&lt;failOnViolation&gt;true&lt;/failOnViolation&gt;
    					&lt;linkXRef&gt;false&lt;/linkXRef&gt;
    				&lt;/configuration&gt;
    			&lt;/plugin&gt;
    		&lt;/plugins&gt;
    	&lt;/build&gt;
    
    &lt;/project&gt;

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.&lt;init&gt;() for reflection. Reason: java.lang.NoClassDefFoundError: org/joda/time/ReadableInstant.
    Warning: Could not register method org.hibernate.validator.internal.constraintvalidators.bv.time.futureorpresent.FutureOrPresentValidatorForReadablePartial.&lt;init&gt;() for reflection. Reason: java.lang.NoClassDefFoundError: org/joda/time/ReadableInstant.
    Warning: Could not register method org.hibernate.validator.internal.constraintvalidators.bv.time.past.PastValidatorForReadablePartial.&lt;init&gt;() for reflection. Reason: java.lang.NoClassDefFoundError: org/joda/time/ReadableInstant.
    Warning: Could not register method org.hibernate.validator.internal.constraintvalidators.bv.time.pastorpresent.PastOrPresentValidatorForReadablePartial.&lt;init&gt;() 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.&lt;init&gt;() for reflection. Reason: java.lang.NoClassDefFoundError: org/joda/time/ReadableInstant.
    Warning: Could not register method org.hibernate.validator.internal.constraintvalidators.bv.time.futureorpresent.FutureOrPresentValidatorForReadablePartial.&lt;init&gt;() for reflection. Reason: java.lang.NoClassDefFoundError: org/joda/time/ReadableInstant.
    Warning: Could not register method org.hibernate.validator.internal.constraintvalidators.bv.time.past.PastValidatorForReadablePartial.&lt;init&gt;() for reflection. Reason: java.lang.NoClassDefFoundError: org/joda/time/ReadableInstant.
    Warning: Could not register method org.hibernate.validator.internal.constraintvalidators.bv.time.pastorpresent.PastOrPresentValidatorForReadablePartial.&lt;init&gt;() 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.
&lt;plugin&gt;
&lt;groupId&gt;org.graalvm.buildtools&lt;/groupId&gt;
&lt;artifactId&gt;native-maven-plugin&lt;/artifactId&gt;
&lt;configuration&gt;
&lt;buildArgs&gt;
&lt;arg&gt;
--initialize-at-build-time=org.apache.commons.logging.LogFactory,org.apache.commons.logging.LogFactoryService,
&lt;/arg&gt;
&lt;/buildArgs&gt;
&lt;/configuration&gt;
&lt;/plugin&gt;

</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:

&lt;plugin&gt;
    &lt;groupId&gt;org.graalvm.buildtools&lt;/groupId&gt;
    &lt;artifactId&gt;native-maven-plugin&lt;/artifactId&gt;
&lt;/plugin&gt;

> 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:

&lt;plugin&gt;
    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
    &lt;configuration&gt;
        &lt;image&gt;
            &lt;buildpacks&gt;
                &lt;buildpack&gt;paketobuildpacks/builder:full&lt;/buildpack&gt;
            &lt;/buildpacks&gt;
            &lt;env&gt;
                &lt;BP_NATIVE_IMAGE&gt;true&lt;/BP_NATIVE_IMAGE&gt;
            &lt;/env&gt;
        &lt;/image&gt;
    &lt;/configuration&gt;
&lt;/plugin&gt;

huangapple
  • 本文由 发表于 2023年6月1日 13:04:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76378803.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定