Spring Native 打包成 exe 可执行文件

Spring Native GraalVM Maven About 13,727 words

参考

https://docs.spring.io/spring-native/docs/current/reference/htmlsingle

前提

下载了GraalVM CE 22.0.0.222.1.0版本打包Spring Native应用为exe时有Bug,不推荐下载此版本。)

Fatal error: Unsupported OptionOrigin
Execution of native-image.cmd returned non-zero result

安装了native-image。使用如下命令安装:

gu.cmd install native-image

下载 Demo 工程

git clone https://github.com/spring-guides/gs-rest-service
cd gs-rest-service/complete

注意

experimental的依赖需要在spring-release仓库拉取,在settings.xml中确认是否不走代理。

主要是*,!spring-release!spring-release表示!spring-release不走代理下载。

<mirror>
    <id>local repo</id>
    <name>local nexus repository</name>
    <url>http://192.168.100.1</url>
    <mirrorOf>*,!spring-release</mirrorOf>
</mirror>

完整 pom

<?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.7.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>rest-service-complete</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>rest-service-complete</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.experimental</groupId>
            <artifactId>spring-native</artifactId>
            <version>0.12.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

            <plugin>
                <groupId>org.springframework.experimental</groupId>
                <artifactId>spring-aot-maven-plugin</artifactId>
                <version>0.12.0</version>
                <executions>
                    <execution>
                        <id>generate</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>test-generate</id>
                        <goals>
                            <goal>test-generate</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>


    <repositories>
        <!-- ... -->
        <repository>
            <id>spring-release</id>
            <name>Spring release</name>
            <url>https://repo.spring.io/release</url>
        </repository>
    </repositories>

    <pluginRepositories>
        <!-- ... -->
        <pluginRepository>
            <id>spring-release</id>
            <name>Spring release</name>
            <url>https://repo.spring.io/release</url>
        </pluginRepository>
    </pluginRepositories>

    <profiles>
        <profile>
            <id>native</id>
            <dependencies>
                <!-- Required with Maven Surefire 2.x -->
                <dependency>
                    <groupId>org.junit.platform</groupId>
                    <artifactId>junit-platform-launcher</artifactId>
                    <scope>test</scope>
                </dependency>
            </dependencies>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.graalvm.buildtools</groupId>
                        <artifactId>native-maven-plugin</artifactId>
                        <version>0.9.11</version>
                        <extensions>true</extensions>
                        <executions>
                            <execution>
                                <id>build-native</id>
                                <goals>
                                    <goal>build</goal>
                                </goals>
                                <phase>package</phase>
                            </execution>
                            <execution>
                                <id>test-native</id>
                                <goals>
                                    <goal>test</goal>
                                </goals>
                                <phase>test</phase>
                            </execution>
                        </executions>
                        <configuration>
                            <!-- ... -->
                        </configuration>
                    </plugin>
                    <!-- Avoid a clash between Spring Boot repackaging and native-maven-plugin -->
                    <plugin>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-maven-plugin</artifactId>
                        <configuration>
                            <classifier>exec</classifier>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

</project>

打包

需在X64 Native Tools Command Prompt for VS 2022命令行中运行mvn命令。

mvn -Pnative clean package

输出

========================================================================================================================
GraalVM Native Image: Generating 'rest-service-complete'...
========================================================================================================================
[1/7] Initializing...                                                                                   (22.3s @ 0.22GB)
Warning: Could not register org.springframework.boot.autoconfigure.jdbc.HikariDriverConfigurationFailureAnalyzer: allDeclaredConstructors for reflection. Reason: java.lang.NoClassDefFoundError: org/springframework/jdbc/CannotGetJdbcConnectionException.
Warning: Could not register org.springframework.boot.diagnostics.analyzer.ValidationExceptionFailureAnalyzer: allDeclaredConstructors for reflection. Reason: java.lang.NoClassDefFoundError: javax/validation/ValidationException.
Warning: Could not register org.springframework.boot.liquibase.LiquibaseChangelogMissingFailureAnalyzer: allDeclaredConstructors for reflection. Reason: java.lang.NoClassDefFoundError: liquibase/exception/ChangeLogParseException.
 Version info: 'GraalVM 22.0.0.2 Java 17 CE'
The bundle named: org.apache.tomcat.util.threads.res.LocalStrings, has not been found. If the bundle is part of a module, verify the bundle name is a fully qualified class name. Otherwise verify the bundle path is accessible in the classpath.
[2/7] Performing analysis...  [**********]                                                              (92.7s @ 3.42GB)
Warning: Could not register complete reflection metadata for org.springframework.aop.aspectj.autoproxy.AspectJAwareAdvisorAutoProxyCreator. Reason(s): java.lang.NoClassDefFoundError: org/aspectj/util/PartialOrder$PartialComparable
Warning: Could not register complete reflection metadata for org.springframework.boot.diagnostics.analyzer.ValidationExceptionFailureAnalyzer. Reason(s): java.lang.NoClassDefFoundError: javax/validation/ValidationException
Warning: Could not register complete reflection metadata for org.springframework.boot.liquibase.LiquibaseChangelogMissingFailureAnalyzer. Reason(s): java.lang.NoClassDefFoundError: liquibase/exception/ChangeLogParseException
Warning: Could not register complete reflection metadata for org.springframework.boot.autoconfigure.jdbc.HikariDriverConfigurationFailureAnalyzer. Reason(s): java.lang.NoClassDefFoundError: org/springframework/jdbc/CannotGetJdbcConnectionException
Warning: Could not register complete reflection metadata for org.springframework.validation.beanvalidation.SpringValidatorAdapter$ViolationFieldError. Reason(s): java.lang.NoClassDefFoundError: javax/validation/Validator, java.lang.NoClassDefFoundError: javax/validation/ConstraintViolation
Warning: Could not register complete reflection metadata for org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator. Reason(s): java.lang.NoClassDefFoundError: org/aspectj/util/PartialOrder$PartialComparable
  13,806 (90.66%) of 15,228 classes reachable
  22,214 (73.31%) of 30,300 fields reachable
  68,163 (62.55%) of 108,977 methods reachable
     755 classes,   268 fields, and 3,409 methods registered for reflection
      81 classes,    75 fields, and    67 methods registered for JNI access
[3/7] Building universe...                                                                               (6.0s @ 4.32GB)
[4/7] Parsing methods...      [***]                                                                      (5.7s @ 2.83GB)
[5/7] Inlining methods...     [*****]                                                                    (5.6s @ 5.14GB)
[6/7] Compiling methods...    [*******]                                                                 (52.5s @ 3.34GB)
[7/7] Creating image...                                                                                  (6.1s @ 1.86GB)
  29.07MB (43.00%) for code area:   45,649 compilation units
  33.25MB (49.19%) for image heap:   9,581 classes and 425,231 objects
   5.28MB ( 7.81%) for other data
  67.60MB in total
------------------------------------------------------------------------------------------------------------------------
Top 10 packages in code area:                               Top 10 object types in image heap:
   1.67MB sun.security.ssl                                    11.87MB byte[] for general heap data
   1.02MB java.util                                            3.26MB java.lang.Class
1010.29KB com.oracle.svm.core.reflect                          2.90MB java.lang.String
 718.04KB com.sun.crypto.provider                              2.42MB byte[] for java.lang.String
 617.74KB org.apache.tomcat.util.net                           1.50MB java.util.LinkedHashMap
 528.21KB org.apache.catalina.core                             1.06MB java.lang.reflect.Method
 481.90KB org.apache.coyote.http2                            861.31KB s.r.a.AnnotatedTypeFactory$AnnotatedTypeBaseImpl
 478.57KB sun.security.x509                                  596.39KB java.util.HashMap$Node
 444.52KB com.oracle.svm.jni                                 560.39KB java.lang.String[]
 442.50KB java.util.concurrent                               524.34KB com.oracle.svm.core.util.LazyFinalReference
      ... 561 additional packages                                 ... 2905 additional object types
                                           (use GraalVM Dashboard to see all)
------------------------------------------------------------------------------------------------------------------------
                       42.5s (21.5% of total time) in 42 GCs | Peak RSS: 6.90GB | CPU load: 4.65
------------------------------------------------------------------------------------------------------------------------
Produced artifacts:
 C:\Users\zhangb76\practice\gs-rest-service\complete\target\rest-service-complete.exe (executable)
 C:\Users\zhangb76\practice\gs-rest-service\complete\target\rest-service-complete.build_artifacts.txt
========================================================================================================================
Finished generating 'rest-service-complete' in 3m 16s.
[INFO]
[INFO] --- spring-boot-maven-plugin:2.7.0:repackage (repackage) @ rest-service-complete ---
[INFO] Attaching repackaged archive C:\Users\zhangb76\practice\gs-rest-service\complete\target\rest-service-complete-0.0.1-SNAPSHOT-exec.jar with classifier exec
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  03:35 min
[INFO] Finished at: 2022-07-08T12:28:32+08:00
[INFO] ------------------------------------------------------------------------

相关 issue

https://github.com/oracle/graal/labels/spring

Views: 1,950 · Posted: 2022-10-08

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh