Maven打包SpringBoot工程的實(shí)現(xiàn)示例
在使用Spring Boot和Maven的項(xiàng)目中,你可以使用Maven來(lái)打包你的項(xiàng)目。Spring Boot項(xiàng)目通常使用Maven插件中的spring-boot-maven-plugin
來(lái)執(zhí)行打包操作
一、spring-boot-maven-plugin詳解
spring-boot-maven-plugin
是Spring Boot項(xiàng)目中用于支持Maven構(gòu)建的插件。它提供了一些功能,使得將Spring Boot應(yīng)用程序打包成可執(zhí)行的JAR或WAR文件變得更加簡(jiǎn)單。以下是一些常見(jiàn)的用法和配置示例:
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、配置主類(lèi)(Main Class)
通過(guò)配置mainClass
來(lái)指定Spring Boot應(yīng)用程序的主類(lèi)。
<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>
元素來(lái)指定生成的JAR文件的名稱(chēng)。
<build> <finalName>custom-application-name</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
4、包含或排除特定的資源文件
通過(guò)配置<resources>
元素來(lái)包含或排除特定的資源文件。
<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、指定額外的依賴(lài)項(xiàng)
通過(guò)<dependencies>
元素指定額外的依賴(lài)項(xiàng),這些依賴(lài)項(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>
元素來(lái)配置運(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í)行階段
通過(guò)<executions>
元素來(lái)自定義插件執(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打包命令
打開(kāi)命令行,進(jìn)入項(xiàng)目根目錄,執(zhí)行以下Maven命令:
mvn clean package
4、運(yùn)行Spring Boot應(yīng)用程序:
使用以下命令來(lái)運(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),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07HashSet如何保證元素不重復(fù)(面試必問(wèn))
HashSet 不保證集合的迭代順序,但允許插入 null 值,也就是說(shuō)它可以將集合中的重復(fù)元素自動(dòng)過(guò)濾掉,保證存儲(chǔ)在 HashSet 中的元素都是唯一的,這篇文章主要介紹了HashSet如何保證元素不重復(fù)(面試必問(wèn)),需要的朋友可以參考下2021-12-12Mybatis如何通過(guò)接口實(shí)現(xiàn)sql執(zhí)行原理解析
為了簡(jiǎn)化MyBatis的使用,MyBatis提供了接口方式自動(dòng)化生成調(diào)用過(guò)程,可以大大簡(jiǎn)化MyBatis的開(kāi)發(fā),下面這篇文章主要給大家介紹了關(guān)于Mybatis如何通過(guò)接口實(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簡(jiǎn)單實(shí)現(xiàn)Servlet文件下載功能
這篇文章主要教大家如何簡(jiǎn)單實(shí)現(xiàn)Servlet文件下載功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09Java項(xiàng)目中防止SQL注入的四種方案總結(jié)
SQL注入是一種代碼注入技術(shù),通過(guò)把SQL命令插入到Web表單遞交或輸入域名或頁(yè)面請(qǐng)求的查詢字符串,最終達(dá)到欺騙服務(wù)器執(zhí)行惡意的SQL命令,下面我們就來(lái)看看如何在項(xiàng)目中防止SQL注入吧2023-10-10SpringBoot整合freemarker實(shí)現(xiàn)代碼生成器
這篇文章主要為大家詳細(xì)介紹了SpringBoot如何整合freemarker實(shí)現(xiàn)一個(gè)簡(jiǎn)單的代碼生成器,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-03-03