目录
一、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构建的插件。它提供了一些功能,使得将Spring Boot应用程序打包成可执行的JAR或WAR文件变得更加简单。以下是一些常见的用法和配置示例:
spring-boot-maven-plugin
插件到pom.xml
在
部分的
中添加spring-boot-maven-plugin
插件:
org.springframework.boot
spring-boot-maven-plugin
通过配置mainClass
来指定Spring Boot应用程序的主类。
org.springframework.boot
spring-boot-maven-plugin
com.example.YourApplicationClass
使用
元素来指定生成的JAR文件的名称。
custom-application-name
org.springframework.boot
spring-boot-maven-plugin
通过配置
元素来包含或排除特定的资源文件。
org.springframework.boot
spring-boot-maven-plugin
src/main/resources
**/*.properties
通过
元素指定额外的依赖项,这些依赖项会被包含到生成的JAR文件中
org.springframework.boot
spring-boot-maven-plugin
com.example
extra-library
1.0.0
使用
元素来配置运行时的JVM参数
org.springframework.boot
spring-boot-maven-plugin
--server.port=8081
通过
元素来自定义插件执行阶段。
org.springframework.boot
spring-boot-maven-plugin
repackage
spring-boot-starter-parent
在你的pom.xml
文件中,确保你的项目是继承自spring-boot-starter-parent
。这样可以确保使用Spring Boot的合适版本和默认配置。
org.springframework.boot
spring-boot-starter-parent
2.5.5
spring-boot-maven-plugin
插件在
部分添加spring-boot-maven-plugin
插件。
org.springframework.boot
spring-boot-maven-plugin
这个插件会自动将项目打包成可执行的JAR文件。
打开命令行,进入项目根目录,执行以下Maven命令:
mvn clean package
使用以下命令来运行打包后的JAR文件:
java -jar your-project.jar