Maven打包SpringBoot工程的實(shí)現(xiàn)示例
在使用Spring Boot和Maven的項(xiàng)目中,你可以使用Maven來打包你的項(xiàng)目。Spring Boot項(xiàng)目通常使用Maven插件中的spring-boot-maven-plugin來執(zhí)行打包操作
一、spring-boot-maven-plugin詳解
spring-boot-maven-plugin是Spring Boot項(xiàng)目中用于支持Maven構(gòu)建的插件。它提供了一些功能,使得將Spring Boot應(yīng)用程序打包成可執(zhí)行的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應(yīng)用程序的主類。
<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、指定額外的依賴項(xiàng)
通過<dependencies>元素指定額外的依賴項(xiàng),這些依賴項(xiàng)會(huì)被包含到生成的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、配置運(yùn)行參數(shù)
使用<arguments>元素來配置運(yùn)行時(shí)的JVM參數(shù)
<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、自定義插件執(zhí)行階段
通過<executions>元素來自定義插件執(zhí)行階段。
<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、項(xiàng)目配置文件中包含spring-boot-starter-parent
在你的pom.xml文件中,確保你的項(xiàng)目是繼承自spring-boot-starter-parent。這樣可以確保使用Spring Boot的合適版本和默認(rèn)配置。
<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>
這個(gè)插件會(huì)自動(dòng)將項(xiàng)目打包成可執(zhí)行的JAR文件。
3、執(zhí)行Maven打包命令
打開命令行,進(jìn)入項(xiàng)目根目錄,執(zhí)行以下Maven命令:
mvn clean package
4、運(yùn)行Spring Boot應(yīng)用程序:
使用以下命令來運(yùn)行打包后的JAR文件:
java -jar your-project.jar
到此這篇關(guān)于Maven打包SpringBoot工程的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)Maven打包SpringBoot內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
IDEA中使用Docker Compose容器編排的實(shí)現(xiàn)
這篇文章主要介紹了IDEA中使用Docker Compose容器編排的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
Mybatis如何通過接口實(shí)現(xiàn)sql執(zhí)行原理解析
為了簡化MyBatis的使用,MyBatis提供了接口方式自動(dòng)化生成調(diào)用過程,可以大大簡化MyBatis的開發(fā),下面這篇文章主要給大家介紹了關(guān)于Mybatis如何通過接口實(shí)現(xiàn)sql執(zhí)行原理解析的相關(guān)資料,需要的朋友可以參考下2023-01-01
詳解Java中的時(shí)間處理與時(shí)間標(biāo)準(zhǔn)
這篇文章主要為大家詳細(xì)介紹了三個(gè)時(shí)間標(biāo)準(zhǔn)GMT,CST,UTC的規(guī)定,以及Java進(jìn)行時(shí)間處理的相關(guān)知識(shí),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-09-09
Java項(xiàng)目中防止SQL注入的四種方案總結(jié)
SQL注入是一種代碼注入技術(shù),通過把SQL命令插入到Web表單遞交或輸入域名或頁面請(qǐng)求的查詢字符串,最終達(dá)到欺騙服務(wù)器執(zhí)行惡意的SQL命令,下面我們就來看看如何在項(xiàng)目中防止SQL注入吧2023-10-10
SpringBoot整合freemarker實(shí)現(xiàn)代碼生成器
這篇文章主要為大家詳細(xì)介紹了SpringBoot如何整合freemarker實(shí)現(xiàn)一個(gè)簡單的代碼生成器,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-03-03

