在項(xiàng)目開(kāi)發(fā)中,部署測(cè)試環(huán)境和正式環(huán)境的時(shí)候配置文件必定會(huì)不同,通過(guò)profile標(biāo)簽?zāi)芎芊奖愕呐渲貌煌h(huán)境的配置文件,但是本人最近在項(xiàng)目部署中有個(gè)特定的js文件需要區(qū)分不同的環(huán)境,網(wǎng)上查了很多資料都沒(méi)有對(duì)Maven編譯時(shí)對(duì)特定文件進(jìn)行修改的說(shuō)明,要么介紹的很模糊,要么干脆都是標(biāo)題黨((╯°Д°)╯︵ ┻━┻)。
經(jīng)過(guò)探索和測(cè)試后發(fā)現(xiàn)可以通過(guò)Maven AntRun Plugin插件進(jìn)行實(shí)現(xiàn)。
在pom文件中通過(guò)profile標(biāo)簽配置不同環(huán)境
<profiles>
<profile>
<!--
<id>為唯一的標(biāo)識(shí),可以是任意
-->
<id>dev</id>
<properties>
<env>dev</env>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>sit</id>
<properties>
<env>sit</env>
</properties>
</profile>
<profile>
<id>pro</id>
<properties>
<env>pro</env>
</properties>
</profile>
</profiles>
<build>
<finalName>${project.artifactId}</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>/*</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
<includes>
<include>META-INF/spring/</include>
</includes>
</resource>
<resource>
<directory>src/main/conf/${env}</directory>
<includes>
<include>**</include>
</includes>
</resource>
</resources>
</build>
在src\main\下建立conf文件夾,加入不同環(huán)境的配置文件,目錄結(jié)構(gòu)為...\META-INF\confg\dev(sit、pro),文件名稱(chēng)與properties標(biāo)簽里一致;在src\main\resources\META-INF\建立spring文件夾,加入其他不需要過(guò)濾的配置文件。

標(biāo)準(zhǔn)Maven項(xiàng)目的資源配置文件在src/main/resources下,這里我們對(duì)src/main/resources目錄開(kāi)啟過(guò)濾-filtering:true,然后排除掉其中不需要進(jìn)行過(guò)濾的目錄,META-INF/spring/,最后一條src/main/conf/${env}則會(huì)根據(jù)<properties><env>dev</env></properties>在Maven編譯時(shí)進(jìn)行對(duì)應(yīng)的資源替換。
如果項(xiàng)目部署時(shí)只需要區(qū)分配置文件,那么到這里就結(jié)束了,通過(guò)命令mvn package -P dev、mvn package -P sit、mvn package -P pro對(duì)應(yīng)不同環(huán)境。
加入Maven AntRun Plugin實(shí)現(xiàn)對(duì)特定文件進(jìn)行修改
http://maven.apache.org/plugins/maven-antrun-plugin/
如果部署中需要對(duì)特定某個(gè)文件進(jìn)行修改,如生產(chǎn)環(huán)境的某個(gè)樣式,某個(gè)頁(yè)面需要特別處理等(js、jsp、css、html等,class文件感覺(jué)可行,但需要測(cè)試和調(diào)整,沒(méi)有實(shí)際用到過(guò)),那就需要使用Maven AntRun Plugin插件的幫助。
引入插件。
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
</dependency>
在特定的環(huán)境profiles標(biāo)簽下,再加入build標(biāo)簽,上面的栗子就變更為:
<profiles>
<profile>
<id>dev</id>
<properties>
<env>dev</env>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>sit</id>
<properties>
<env>sit</env>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<phase>compile</phase>
<configuration>
<target>
<!--
將Ant任務(wù)放在這里,還可以在這里添加一個(gè)build.xml文件
此處為刪除目標(biāo)文件,把不同環(huán)境文件替換為目標(biāo)文件
-->
<delete file="./src/main/webapp/js/Passl.js" />
<move file="./src/main/webapp/js/Pass-sit.js"
tofile="./src/main/webapp/js/Pass.js"/>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>pro</id>
<properties>
<env>pro</env>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<phase>compile</phase>
<configuration>
<target>
<delete file="./src/main/webapp/js/Pass.js" />
<move file="./src/main/webapp/js/Pass-pro.js"
tofile="./src/main/webapp/js/Pass.js"/>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<phase>compile</phase>表示在編譯階段,即該任務(wù)為在編譯時(shí),把./src/main/webapp/js/Pass.js文件刪除,把./src/main/webapp/js/Pass-pro.js文件重命名為./src/main/webapp/js/Pass.js,達(dá)到了不同環(huán)境下替換對(duì)應(yīng)環(huán)境所需文件的目的。
有的資料中會(huì)使用<phase>package</phase>,該周期包含了compile階段,但最后會(huì)執(zhí)行jar打包階段,可根據(jù)實(shí)際情況做選擇。
<target>標(biāo)簽中可執(zhí)行的Ant任務(wù)很強(qiáng)大,詳細(xì)的功能可以搜下Ant任務(wù)文件操作╭(′▽)╭(′▽)╯。
