将 Spring Boot 项目打包成可以独立运行的 jar 包

本文详细介绍了如何在Windows11Pro系统上使用SpringBoot3.1.10和Java17版本,通过Maven插件配置pom.xml文件,排除Lombok并执行repackage,以及指定本地开发环境的打包与运行过程。
摘要由CSDN通过智能技术生成
环境
Windows 11 Pro 23H2 OS build 22631.3447
OpenJDK 64-Bit Server VM Temurin-21.0.2+13 (build 21.0.2+13-LTS, mixed mode, sharing)
Spring Boot, Java version 等版本详见正文内容

1. pom.xml 文件配置

<parent>  
    <groupId>org.springframework.boot</groupId>  
    <artifactId>spring-boot-starter-parent</artifactId>  
    <version>3.1.10</version>  
    <relativePath/>
</parent>
<properties>  
    <spring-boot.version>3.1.10</spring-boot.version>  
    <java.version>17</java.version>
</properties>

2. 在 pom.xml 文件添加插件

<!-- https://docs.spring.io/spring-boot/docs/3.1.11/maven-plugin/reference/htmlsingle/#packaging -->
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <!-- 若项目未使用 Lombok,则可跳过此配置 -->
            <configuration>
                <excludes>
                    <exclude>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok</artifactId>
                    </exclude>
                </excludes>
            </configuration>
            <!-- 若项目已使用 spring-boot-starter-parent,则无需 repackage 执行 -->
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

3. 打包

运行如下命令,将在 .\target\ 目录内生成 xxx.jar

mvn package spring-boot:repackage -Dspring.profiles.active=local

-Dspring.profiles.active=local 用于指定打包环境的配置文件

4. 运行 jar 包

使用以下命令,可以运行 jar

java -Dspring.profiles.active=local -jar xxx.jar

-Dspring.profiles.active=local 用于指定运行环境的配置文件

5. 参考

  1. java - SpringBoot no main manifest attribute (maven) - Stack Overflow: https://stackoverflow.com/questions/54867295/springboot-no-main-manifest-attribute-maven
  2. Fixing the No Main Manifest Attribute in Spring Boot | Baeldung: https://www.baeldung.com/spring-boot-fix-the-no-main-manifest-attribute
  3. Spring Boot Maven Plugin Documentation: https://docs.spring.io/spring-boot/docs/3.1.11/maven-plugin/reference/htmlsingle/

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

零一魔法

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值