關(guān)于SpringBoot打包測(cè)試、生產(chǎn)環(huán)境方式
1. SpringBoot創(chuàng)建不同環(huán)境的配置文件
1.1 新建各環(huán)境的配置文件
resources
目錄下新建 application-dev.yml
、 application-test.yml
、 application-prod.yml
文件
分別對(duì)應(yīng) 開發(fā)、測(cè)試、生產(chǎn)環(huán)境
1.2 修改 application.yml
application.yml
修改配置參數(shù)
spring: profiles: active: @profiles.active@
2. 修改 pom.xml
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <!--fork: 如果沒有該項(xiàng)配置,整個(gè) devtools 不會(huì)起作用--> <fork>true</fork> <!--啟動(dòng)類--> <mainClass>com.xx.DemoApplication</mainClass> </configuration> <executions> <execution> <goals> <!--創(chuàng)建一個(gè)自動(dòng)可執(zhí)行的jar或war文件 --> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> <resources> <resource> <directory>src/main/resources</directory> <!-- 資源根目錄排除各環(huán)境的配置,防止在生成目錄中多余其它目錄 --> <filtering>true</filtering> <excludes> <exclude>application*.yml</exclude> </excludes> </resource> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> <includes> <include>application.yml</include> <include>application-${profiles.active}.yml</include> </includes> </resource> </resources> </build> <!-- 不同環(huán)境的配置 --> <profiles> <!--開發(fā)環(huán)境--> <profile> <id>dev</id> <properties> <profiles.active>dev</profiles.active> </properties> <!--默認(rèn)激活--> <activation> <activeByDefault>true</activeByDefault> </activation> </profile> <!--測(cè)試環(huán)境--> <profile> <id>test</id> <properties> <profiles.active>test</profiles.active> </properties> </profile> <!--生產(chǎn)環(huán)境--> <profile> <id>prod</id> <properties> <profiles.active>prod</profiles.active> </properties> </profile> </profiles>
3. 打成指定環(huán)境的 jar包
3.1 使用 idea 開發(fā)工具進(jìn)行打包
使用 idea 開發(fā)工具操作即可,方便快捷:
- 打開右側(cè)的
Maven
工具欄。 - 雙擊
clean
,先清理一下。 - 勾選需要打包的環(huán)境
- 雙擊
package
,等到 SUCCESS后,查看target
目錄下,會(huì)有打包的項(xiàng)目jar包。
操作步驟如圖:
3.2 命令行打包
執(zhí)行命令:
mvn clean package -P {環(huán)境名} -D maven.test.skip=true
示例(打包test環(huán)境):
mvn clean package -P test -D maven.test.skip=true
3.3 成功后的jar包
查看 target
目錄下的 jar
包
4. 本地運(yùn)行jar包
jar包所在的路徑下,執(zhí)行:
java -jar -D spring.profiles.active={環(huán)境名} {jar包名}
示例
(打包的test環(huán)境,打包后jar包名為 springboot-demo1-1.0.0-SNAPSHOT.jar
):
java -jar -D spring.profiles.active=test springboot-demo1-1.0.0-SNAPSHOT.jar
示例圖:
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringSecurity跨域請(qǐng)求偽造(CSRF)的防護(hù)實(shí)現(xiàn)
本文主要介紹了SpringSecurity跨域請(qǐng)求偽造(CSRF)的防護(hù)實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07Java實(shí)現(xiàn)讀取設(shè)置pdf屬性信息
這篇文章主要為大家詳細(xì)介紹了如何使用Java實(shí)現(xiàn)讀取設(shè)置pdf屬性信息,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-01-01Python單元測(cè)試_使用裝飾器實(shí)現(xiàn)測(cè)試跳過和預(yù)期故障的方法
下面小編就為大家?guī)硪黄狿ython單元測(cè)試_使用裝飾器實(shí)現(xiàn)測(cè)試跳過和預(yù)期故障的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06Java中l(wèi)ist.foreach()和list.stream().foreach()用法詳解
在Java中List是一種常用的集合類,用于存儲(chǔ)一組元素,List提供了多種遍歷元素的方式,包括使用forEach()方法和使用Stream流的forEach()方法,這篇文章主要給大家介紹了關(guān)于Java中l(wèi)ist.foreach()和list.stream().foreach()用法的相關(guān)資料,需要的朋友可以參考下2024-07-07JavaIO?BufferedReader和BufferedWriter使用及說明
這篇文章主要介紹了JavaIO?BufferedReader和BufferedWriter使用及說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12