在项目开发中,部署测试环境和正式环境的时候配置文件必定会不同,通过profile标签能很方便的配置不同环境的配置文件,但是本人最近在项目部署中有个特定的js文件需要区分不同的环境,网上查了很多资料都没有对Maven编译时对特定文件进行修改的说明,要么介绍的很模糊,要么干脆都是标题党((╯°Д°)╯︵ ┻━┻)。
经过探索和测试后发现可以通过Maven AntRun Plugin插件进行实现。
在pom文件中通过profile标签配置不同环境
<profiles>
<profile>
<!--
<id>为唯一的标识,可以是任意
-->
<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文件夹,加入不同环境的配置文件,目录结构为...\META-INF\confg\dev(sit、pro),文件名称与properties标签里一致;在src\main\resources\META-INF\建立spring文件夹,加入其他不需要过滤的配置文件。
标准Maven项目的资源配置文件在src/main/resources下,这里我们对src/main/resources目录开启过滤-filtering:true,然后排除掉其中不需要进行过滤的目录,META-INF/spring/,最后一条src/main/conf/${env}则会根据<properties><env>dev</env></properties>在Maven编译时进行对应的资源替换。
如果项目部署时只需要区分配置文件,那么到这里就结束了,通过命令mvn package -P dev、mvn package -P sit、mvn package -P pro对应不同环境。
加入Maven AntRun Plugin实现对特定文件进行修改
http://maven.apache.org/plugins/maven-antrun-plugin/
如果部署中需要对特定某个文件进行修改,如生产环境的某个样式,某个页面需要特别处理等(js、jsp、css、html等,class文件感觉可行,但需要测试和调整,没有实际用到过),那就需要使用Maven AntRun Plugin插件的帮助。
引入插件。
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
</dependency>
在特定的环境profiles标签下,再加入build标签,上面的栗子就变更为:
<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任务放在这里,还可以在这里添加一个build.xml文件
此处为删除目标文件,把不同环境文件替换为目标文件
-->
<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>表示在编译阶段,即该任务为在编译时,把./src/main/webapp/js/Pass.js文件删除,把./src/main/webapp/js/Pass-pro.js文件重命名为./src/main/webapp/js/Pass.js,达到了不同环境下替换对应环境所需文件的目的。
有的资料中会使用<phase>package</phase>,该周期包含了compile阶段,但最后会执行jar打包阶段,可根据实际情况做选择。
<target>标签中可执行的Ant任务很强大,详细的功能可以搜下Ant任务文件操作╭(′▽)╭(′▽
)╯。