当前位置: 首页 > news >正文

详解Maven如何打包SpringBoot工程

目录

一、spring-boot-maven-plugin详解

1、添加spring-boot-maven-plugin插件到pom.xml

2、配置主类(Main Class)

3、配置打包的JAR文件名

4、包含或排除特定的资源文件

5、指定额外的依赖项

 6、配置运行参数

7、自定义插件执行阶段

二、Maven打包SpringBoot工程

1、项目配置文件中包含spring-boot-starter-parent

2、添加spring-boot-maven-plugin插件

3、执行Maven打包命令

 4、运行Spring Boot应用程序:


在使用Spring Boot和Maven的项目中,你可以使用Maven来打包你的项目。Spring Boot项目通常使用Maven插件中的spring-boot-maven-plugin来执行打包操作

一、spring-boot-maven-plugin详解

spring-boot-maven-plugin是Spring Boot项目中用于支持Maven构建的插件。它提供了一些功能,使得将Spring Boot应用程序打包成可执行的JAR或WAR文件变得更加简单。以下是一些常见的用法和配置示例:

1、添加spring-boot-maven-plugin插件到pom.xml

<build>部分的<plugins>中添加spring-boot-maven-plugin插件:

<build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins>
</build>
2、配置主类(Main Class)

通过配置mainClass来指定Spring Boot应用程序的主类。

<build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><mainClass>com.example.YourApplicationClass</mainClass></configuration></plugin></plugins>
</build>
3、配置打包的JAR文件名

使用<finalName>元素来指定生成的JAR文件的名称。

<build><finalName>custom-application-name</finalName><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins>
</build>
4、包含或排除特定的资源文件

通过配置<resources>元素来包含或排除特定的资源文件。

<build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><resources><resource><directory>src/main/resources</directory><includes><include>**/*.properties</include></includes></resource></resources></configuration></plugin></plugins>
</build>
5、指定额外的依赖项

通过<dependencies>元素指定额外的依赖项,这些依赖项会被包含到生成的JAR文件中

<build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><dependencies><dependency><groupId>com.example</groupId><artifactId>extra-library</artifactId><version>1.0.0</version></dependency></dependencies></configuration></plugin></plugins>
</build>
 6、配置运行参数

使用<arguments>元素来配置运行时的JVM参数

<build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><arguments><argument>--server.port=8081</argument></arguments></configuration></plugin></plugins>
</build>
7、自定义插件执行阶段

通过<executions>元素来自定义插件执行阶段。

<build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><executions><execution><goals><goal>repackage</goal></goals><configuration><!-- 自定义配置 --></configuration></execution></executions></plugin></plugins>
</build>

二、Maven打包SpringBoot工程

1、项目配置文件中包含spring-boot-starter-parent

在你的pom.xml文件中,确保你的项目是继承自spring-boot-starter-parent。这样可以确保使用Spring Boot的合适版本和默认配置。

<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.5.5</version> <!-- 使用你的Spring Boot版本 -->
</parent>
2、添加spring-boot-maven-plugin插件

<build>部分添加spring-boot-maven-plugin插件。

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

这个插件会自动将项目打包成可执行的JAR文件。

3、执行Maven打包命令

打开命令行,进入项目根目录,执行以下Maven命令:

mvn clean package
 

 4、运行Spring Boot应用程序:

使用以下命令来运行打包后的JAR文件:

java -jar your-project.jar

http://www.lryc.cn/news/260439.html

相关文章:

  • PyQt6 QFrame分割线控件
  • PostgreSql 序列
  • 【深度学习目标检测】六、基于深度学习的路标识别(python,目标检测,yolov8)
  • Vue3上传图片和删除图片
  • 华为配置VRRP负载分担示例
  • 【Python】按升序排列 Excel 工作表
  • 定时器TIM HAL库+cubeMX(上)
  • 我常用的几个经典Python模块
  • 课堂练习4.4:页式虚存
  • javascript实现Stack(栈)数据结构
  • Layui深入
  • 网络层--TCP/UDP协议
  • 前端发送请求之参数处理---multipart/form-data与application/x-www-form-urlencoded
  • 解决Ubuntu16.04没声音
  • 12.14每日一题(备战蓝桥杯归并排序)
  • 面试__Java常见异常有哪些?
  • linux 网络子系统 摘要
  • java发起http、https请求,并携带cookie、header,post参数放body并可选关闭ssl证书验证,高可用版
  • windows系统nodeJs报错node-sass npm ERR! command failed
  • 从零构建属于自己的GPT系列5:模型部署1(文本生成函数解读、模型本地化部署、文本生成文本网页展示、代码逐行解读)
  • 电脑篇——360浏览器打开新标签页自定义,和关闭360导航(强迫症福音)
  • 常见的Linux基本指令
  • ESXI 6.7升级update3
  • bugku--source
  • SpringBoot Maven 项目打包的艺术--主清单属性缺失与NoClassDefFoundError的优雅解决方案
  • 2023-12-14 二叉树的最大深度和二叉树的最小深度以及完全二叉树的节点个数
  • 利用闭包与高阶函数实现缓存函数的创建
  • P1042 [NOIP2003 普及组] 乒乓球 JAVA 题解
  • 最大公因数,最小公倍数详解
  • 无脑利用API实现文心一言AI对话功能?(附代码)