SpringBoot3设置maven package直接打包成二进制可执行文件

发布于:2025-04-21 ⋅ 阅读:(103) ⋅ 点赞:(0)

注意事项

SpringBoot普通native打包顺序clean compile spring-boot:process-aot native:compile
使用以下配置只会的打包顺序clean package(注意:使用此配置以后打包会有编译后的class文件、jar包、original源文件、二进制可执行文件【Linux是无后缀的包名,Windows是exe】)

pom.xml

<build>
        <plugins>
            <!-- AOT Graalvm native maven 插件 -->
            <plugin>
                <groupId>org.graalvm.buildtools</groupId>
                <artifactId>native-maven-plugin</artifactId>
                <version>${native.maven.plugin.version}</version>
                <extensions>true</extensions>
                <executions>
                    <execution>
                        <id>build-native</id>
                        <goals>
							<!-- <goal>compile-no-fork</goal>(不推荐使用) -->
                            <goal>compile</goal>
                        </goals>
                        <!-- 让mvn package 直接打包成二进制可执行文件的主要配置 -->
                        <phase>package</phase>
                    </execution>
                    <execution>
                        <id>test-native</id>
                        <goals>
                            <goal>test</goal>
                        </goals>
                        <!-- mvn test 测试 native(如果skipTests,这里也会跳过!) -->
                        <phase>test</phase>
                    </execution>
                </executions>
                <configuration>
                	<!-- main 文件 -->
                    <mainClass>com.meta39.springboot3demo.SpringBoot3DemoApplication</mainClass>
                    <!-- 快速构建【CPU和内存疯涨,这时候电脑会卡顿】或者将 GRAALVM_QUICK_BUILD 环境变量设置为 true -->
                    <quickBuild>true</quickBuild>
                    <skipNativeTests>true</skipNativeTests>
                    <debug>false</debug>
                    <verbose>false</verbose>
                    <buildArgs combine.children="append">
                        <buildArg>--enable-url-protocols=http</buildArg>
                    </buildArgs>
                    <jvmArgs>
                        <!-- 单独设置编译进程内存 -Xms 和 -Xmx(不设置的话,有多少用多少。推荐不设置) -->
<!--                        <arg>-Xms4G</arg>-->
<!--                        <arg>-Xmx4G</arg>-->
                    </jvmArgs>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven-compiler-plugin.version}</version>
                <configuration>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                            <version>${lombok.version}</version>
                        </path>
                    </annotationProcessorPaths>
                </configuration>
            </plugin>
            <!-- Spring Boot Maven 插件【前面设置了版本就会替换掉 spring-boot-maven-plugin 里默认的版本号】 -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                    	<!-- 不设置 process-aot 可能无法打成 native ,因为Spring boot 用了很多反射,需要 aot 优化后才能成功打成native -->
                        <id>process-test-aot</id>
                        <goals>
                            <goal>process-test-aot</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

进阶

不使用上面的配置,使用命令打包。

mvn clean compile spring-boot:process-aot native:compile -DskipTests -Dmaven.compile.fork=true -T 1C -f pom.xml -Pprod

网站公告

今日签到

点亮在社区的每一天
去签到