摘要:此時(shí)可以嘗試或命令打包,安裝包內(nèi)的文件中占位符已被替換。整合原理項(xiàng)目中一般都會(huì)加上可以查看的文件,里面包含定義的值是這樣插件會(huì)將或文件中的替換為中對(duì)應(yīng)的值。
在Maven和Spring中,都有profile這個(gè)概念。profile是用于區(qū)分各種環(huán)境的,例如開(kāi)發(fā)環(huán)境、測(cè)試環(huán)境、正式環(huán)境等。Maven的profile用于在打包時(shí)根據(jù)指定環(huán)境替換不同環(huán)境的配置文件配置,如數(shù)據(jù)庫(kù)配置。Spring的Profile可以用于在不同的環(huán)境下加載不同的bean,例如@Profile注解。兩者一個(gè)是Maven編譯和打包時(shí)生效,另一個(gè)是運(yùn)行時(shí)生效,默認(rèn)是沒(méi)有關(guān)聯(lián)的,本文會(huì)分別介紹非Spring Boot項(xiàng)目和Spring Boot項(xiàng)目整合Maven profile。
Maven profile配置在pom.xml中,可以配置test和product兩個(gè)profile,分別對(duì)應(yīng)測(cè)試環(huán)境和正式環(huán)境。這里也可以根據(jù)具體情況自定義。
test ...product ...
此時(shí),運(yùn)行mvn package -Ptest就會(huì)使用id為test的profile內(nèi)的配置打包,mvn package -Pproduct就是用來(lái)打正式環(huán)境包的命令。
Spring Framework(非Spring Boot)整合Maven profile Spring Framework如何啟用一個(gè)profileSpring啟用某個(gè)profile有多種方式(摘自官方文檔:https://docs.spring.io/spring... ):
Activating a profile can be done in several ways, but the most straightforward is to do it programmatically against the Environment API which is available through an ApplicationContext.
In addition, you can also declaratively activate profiles through the spring.profiles.active property, which may be specified through system environment variables, JVM system properties, servlet context parameters in web.xml, or even as an entry in JNDI.
總結(jié)一下有以下幾種方式:
通過(guò)代碼設(shè)置:ApplicationContext.getEnvironment().setActiveProfiles("yourProfile")
通過(guò)系統(tǒng)環(huán)境變量spring.profiles.active值來(lái)設(shè)置
通過(guò)JVM系統(tǒng)屬性spring.profiles.active值來(lái)設(shè)置
通過(guò)web.xml中的context-param來(lái)設(shè)置
為了便于跟Maven整合,我們使用web.xml來(lái)設(shè)置Spring profile,如下:
spring.profiles.active product
以上配置會(huì)啟用Spring的product profile,即正式環(huán)境。
Spring Framework profile整合Maven profile如果想要整合Maven profile和Spring Framework profile,需要在Maven打包時(shí)對(duì)web.xml中的spring.profiles.active值進(jìn)行替換,可以在web.xml中配置一個(gè)占位符${activeProfile}:
spring.profiles.active ${activeProfile}
在pom.xml配置maven-war-plugin:
maven-war-plugin 3.2.2 true dev test test product product
以上配置完成后,再通過(guò)mvn package -Ptest或mvn package -Pproduct打包后,再解壓war包,可以看到web.xml中原有的
spring.profiles.active ${activeProfile}
被替換為了Maven中對(duì)應(yīng)的profile,例如mvn package -Pproduct打包后web.xml內(nèi)容:
spring.profiles.active product
以上就完成了Maven profile和Spring profile的整合。
兼容jetty-maven-plugin如果恰好在項(xiàng)目中使用到jetty-maven-plugin用于開(kāi)發(fā)環(huán)境調(diào)試,那么在web.xml配置占位符${activeProfile}后,通過(guò)mvn jetty:run啟動(dòng)應(yīng)用時(shí)會(huì)Spring框架會(huì)報(bào)錯(cuò):
Could not resolve placeholder "activeProfile" in string value "${activeProfile}"
這是因?yàn)檫\(yùn)行mvn jetty:run命令時(shí)插件并沒(méi)有打war包,而是直接使用源碼中的web.xml,此時(shí)占位符${activeProfile}未被maven-war-plugin替換,所以Spring框架會(huì)報(bào)錯(cuò)。
參考文檔:https://www.eclipse.org/jetty...
解決方法一使用mvn jetty:run-war或mvn jetty:run-exploded命令替代mvn jetty:run,這兩個(gè)命令會(huì)先用maven-war-plugin打好war包后再運(yùn)行,此時(shí)占位符${activeProfile}已被替換為Maven的profile。
但是這種方案會(huì)帶來(lái)一個(gè)問(wèn)題:由于這種方式需要先打war包再運(yùn)行,開(kāi)發(fā)時(shí)項(xiàng)目中資源(例如html、jsp)修改后就不會(huì)實(shí)時(shí)生效,而是需要重新打包啟動(dòng),不便于調(diào)試。
解決方法二(推薦)這種方案還是使用mvn jetty:run命令,只需要給jetty-maven-plugin插件添加一個(gè)名為activeProfile的系統(tǒng)屬性,讓Spring框架來(lái)解析web.xml中的${activeProfile}:
org.eclipse.jetty jetty-maven-plugin 9.2.10.v20150310 / activeProfile ${activeProfile}
參考文檔:https://www.eclipse.org/jetty...
Spring Boot整合Maven profile如果項(xiàng)目采用的框架是Spring Boot而不是直接使用Spring Framework,那么Spring Boot的profile可以在resources目錄下的application.properties或application.yml文件中指定,以application.properties為例:
spring.profiles.active=product
要想整合Maven profile只需要改為@activeProfile@占位符即可:
spring.profiles.active=@activeProfile@
僅需要這一行配置就完成了Spring Boot profile整合Maven profile,非常方便。此時(shí)可以嘗試mvn package -Ptest或mvn package -Pproduct命令打包,安裝包內(nèi)的文件中@activeProfile@占位符已被替換。
Spring Boot整合Maven profile原理Spring Boot項(xiàng)目中一般都會(huì)加上spring-boot-starter-parent:
org.springframework.boot spring-boot-starter-parent ${spring.boot.version}
可以查看spring-boot-starter-parent的pom.xml文件,里面包含maven-resources-plugin:
maven-resources-plugin ${resource.delimiter} false
${resource.delimiter}定義的值是@:
@
這樣maven-resources-plugin插件會(huì)將application.properties或application.yml文件中的@activeProfile@替換為pom.xml中對(duì)應(yīng)profile的值。
至于為什么Spring Boot要使用@..@而不是Maven默認(rèn)的${..}作為占位符的符號(hào),官方文檔也給出了解釋,以下摘自:https://docs.spring.io/spring...
Note that, since the application.properties and application.yml files accept Spring style placeholders (${…?}), the Maven filtering is changed to use @..@ placeholders. (You can override that by setting a Maven property called resource.delimiter.)
因?yàn)镾pring Boot框架本身也用${..}作為占位符,Maven插件maven-resources-plugin如果還使用相同的占位符,那么可能會(huì)導(dǎo)致一些沖突,所以spring-boot-starter-parent將maven-resources-plugin的占位符改為@..@。
參考文檔Spring Framework: Activating a Profile
Spring Boot: Maven
Jetty Maven Plugin
維基百科:部署描述符(Deployment Descriptor)
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://m.hztianpu.com/yun/73558.html
摘要:清理上一次執(zhí)行創(chuàng)建的文件處理資源文件編譯代碼執(zhí)行單元測(cè)試文件創(chuàng)建拷貝到本地的倉(cāng)庫(kù)下面發(fā)布生成文檔將工程所有文檔生成網(wǎng)站,生成的網(wǎng)站界面默認(rèn)和的項(xiàng)目站點(diǎn)類似,但是其文檔用格式寫(xiě)的,目前不支持,需要用其他插件配合才能支持。 前言 本文可以幫助你加深對(duì)Maven的整體認(rèn)識(shí),不是一篇基礎(chǔ)文章。如果你現(xiàn)在還沒(méi)有用 Maven 跑過(guò) HelloWorld,那么本文可能不適合你。 一、Maven簡(jiǎn)介...
摘要:下一篇介紹基于的服務(wù)注冊(cè)與調(diào)用。服務(wù)提供者工程配置這里服務(wù)提供者是使用之前進(jìn)階教程第三篇整合連接池以及監(jiān)控改造而來(lái),這里一樣的部分就不再重復(fù)說(shuō)明,下面將說(shuō)明新增的部分。 Spring Cloud簡(jiǎn)介 Spring Cloud是一個(gè)基于Spring Boot實(shí)現(xiàn)的云應(yīng)用開(kāi)發(fā)工具,它為基于JVM的云應(yīng)用開(kāi)發(fā)中涉及的配置管理、服務(wù)發(fā)現(xiàn)、斷路器、智能路由、微代理、控制總線、全局鎖、決策競(jìng)選、分...
摘要:的配置文件默認(rèn)為或,此外僅以配置為說(shuō)明。的由的標(biāo)簽管理。管理由于構(gòu)建是基于或,此處僅以說(shuō)明。管理分五步,以下詳細(xì)介紹。并且為表示,會(huì)將文件內(nèi)容的替換為相應(yīng)的變量如文件中的會(huì)替換為屬性值。 1. Spring Profile Spring可使用Profile決定程序在不同環(huán)境下執(zhí)行情況,包含配置、加載Bean、依賴等。 Spring的Profile一般項(xiàng)目包含:dev(開(kāi)發(fā)), test...
閱讀 2811·2023-04-26 02:02
閱讀 2678·2023-04-25 20:38
閱讀 4267·2021-09-26 09:47
閱讀 3187·2021-09-10 10:50
閱讀 3862·2021-09-07 09:58
閱讀 3392·2019-08-30 15:54
閱讀 2762·2019-08-30 15:54
閱讀 1989·2019-08-29 17:03