如何使用Gradle實現(xiàn)類似Maven的profiles功能
版本說明
- GraalVM JDK 21.0.3
- Gradle 8.7
- Spring Boot 3.2.5
目錄結(jié)構(gòu)
指定環(huán)境打包
application.yml/yaml/properties
執(zhí)行 bootJar 打包命令前要先執(zhí)行 clean【其它和 processResources 相關(guān)的命令也要先執(zhí)行 clean】,否則 active 值不會變!
spring: profiles: # 執(zhí)行 bootJar 打包命令前要先執(zhí)行 clean【其它和 processResources 相關(guān)的命令也要先執(zhí)行 clean】,否則 active 值不會變! active: @activeProfiles@
build.gradle 修改 processResources 任務
def activeProfiles = project.properties['activeProfiles'] ?: "dev" processResources { exclude { FileTreeElement details -> { (//排除不是當前環(huán)境的 yml 配置文件 details.file.name.startsWith("application-") && details.file.name.endsWith(".yml") && !details.file.name.equals("application.yml") && !details.file.name.equals("application-" + activeProfiles + ".yml") ) || (//排除不是當前環(huán)境的 yaml 配置文件 details.file.name.startsWith("application-") && details.file.name.endsWith(".yaml") && !details.file.name.equals("application.yaml") && !details.file.name.equals("application-" + activeProfiles + ".yaml") ) || (//排除不是當前環(huán)境的 properties 配置文件 details.file.name.startsWith("application-") && details.file.name.endsWith(".properties") && !details.file.name.equals("application.properties") && !details.file.name.equals("application-" + activeProfiles + ".properties") ) } } filter ReplaceTokens, tokens: [ activeProfiles: activeProfiles ] }
打包
因為沒有指定環(huán)境,默認dev
可以先在build/resources/main目錄下查看是否只包含對應環(huán)境的文件
jar包里的文件和變量也對的上
指定test環(huán)境打包
bootJar追加application.yml配置的spring.profiles.active的@activeProfiles@變量名并指定環(huán)境為test后點ok保存
先clean再bootJar,否則不會生效?。?!
打包輸出到控制臺的命令可以查看配置有沒有生效
查看文件是否符合預期
到此這篇關(guān)于Gradle實現(xiàn)類似Maven的profiles功能的文章就介紹到這了,更多相關(guān)Gradle profiles功能內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Mybatis-Plus接口BaseMapper與Services使用詳解
這篇文章主要為大家介紹了Mybatis-Plus接口BaseMapper與Services使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-05-05基于Springboot+Junit+Mockito做單元測試的示例
本篇文章主要介紹了基于Springboot+Junit+Mockito做單元測試的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-02-02Spring Boot + Kotlin整合MyBatis的方法教程
前幾天由于工作需要,便開始學習了kotlin,java基礎扎實學起來也還算比較快,對于kotlin這個編程語言自然是比java有趣一些,下面這篇文章主要給大家介紹了關(guān)于Spring Boot + Kotlin整合MyBatis的方法教程,需要的朋友可以參考下。2018-01-01Java設計模式之橋接模式詳解(Bridge Pattern)
橋接模式是一種結(jié)構(gòu)型設計模式,旨在將抽象部分與其實現(xiàn)部分分離,從而使兩者可以獨立地變化,橋接模式通過組合關(guān)系代替繼承關(guān)系,將抽象和實現(xiàn)解耦,使代碼更具擴展性和維護性2025-02-02