? ? ? ? 實(shí)現(xiàn)不同環(huán)境的打包,就是在pom.xml中定義不同的環(huán)境配置,然后將需要的配置打包入最終的壓縮包中。
????????首先定義幾個(gè)不同的配置文件,然后在pom.xml中定義幾個(gè)不同的<profile>標(biāo)簽。每個(gè)profile可以看作是POM的一部分配置,我們可以根據(jù)不同的環(huán)境應(yīng)用不同的profile,從而達(dá)到不同環(huán)境使用不同的POM配置的目的。
????????假如配置了3種項(xiàng)目運(yùn)行環(huán)境,在src/main/resources目錄下增加如下環(huán)境配置
├── conf
│? ├── dev
│? │? ├── application.properties
│? │? ├── conf.properties
? ? │? ├── db.properties
│? ├── online
│? │? ├── application.properties
│? │? ├── conf.properties
│? │? ├── db.properties
│? └── test
│? │? ├── application.properties
│? │? ├── conf.properties
│? │? ├── db.properties
在(子)項(xiàng)目pom.xml中<project>標(biāo)簽下加入如下的<profiles>標(biāo)簽,
? ? <!-- 多環(huán)境打包 start -->
? ? <profiles>
? ? ? ? <!-- 開發(fā)環(huán)境配置 -->
? ? ? ? <profile>
? ? ? ? ? ? <id>dev</id>
? ? ? ? ? ? <properties>
? ? ? ? ? ? ? ? <profiles.active>dev</profiles.active>
? ? ? ? ? ? </properties>
? ? ? ? ? ? <activation>
? ? ? ? ? ? ? ? <activeByDefault>true</activeByDefault>
? ? ? ? ? ? </activation>
? ? ? ? </profile>
? ? ? ? <!-- 測(cè)試環(huán)境配置 -->
? ? ? ? <profile>
? ? ? ? ? ? <id>test</id>
? ? ? ? ? ? <properties>
? ? ? ? ? ? ? ? <profiles.active>test</profiles.active>
? ? ? ? ? ? </properties>
? ? ? ? </profile>
? ? ? ? <!-- 正式環(huán)境 -->
? ? ? ? <profile>
? ? ? ? ? ? <id>online</id>
? ? ? ? ? ? <properties>
? ? ? ? ? ? ? ? <profiles.active>online</profiles.active>
? ? ? ? ? ? </properties>
? ? ? ? </profile>
? ? </profiles>
? ? <!-- 多環(huán)境打包 end -->
????????在profile標(biāo)簽中定義一個(gè) id 標(biāo)簽,這樣就可以將不同環(huán)境的配置區(qū)分開。而變量profiles.active的值對(duì)應(yīng)了具體的環(huán)境配置名稱。為了引用這些環(huán)境配置,需要在pom.xml文件中使用該變量的值,在build->resources標(biāo)簽下增加如下內(nèi)容,
? ? ? ? <resource>
? ? ? ? ? ? ? ? <directory>src/main/resources/conf/${profiles.active}</directory>
? ? ? ? ? ? </resource>
????????在 pom 中配置好后就可以在 mvn 命令中用-P參數(shù)激活指定 id 對(duì)應(yīng)的 profile 進(jìn)行動(dòng)態(tài)構(gòu)建了。比如打測(cè)試環(huán)境包,執(zhí)行:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?mvn clean install -Ptest
????????這樣將src/main/resources/conf/test下的配置打包入項(xiàng)目。