最近准备把正在开发的项目给分为两个环境来部署,使用jenkins进行自动构建。
把maven部署多环境相关的资料看了下,都比较难理解,于是自己摸索,找到一个比较好的办法。
首先在 src/main/resource 下建两个文件夹,具体几个看你的环境有几个,我这里是分了两个,dev和product,开发环境和正式环境。
然后将配置文件分别放入两个目录中,然后把两个环境中的配置配好
接下来编辑项目的pom文件
进行如下配置
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 
 | <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 ......
 ......
 <profiles>
 
 <profile>
 <id>dev</id>
 <properties>
 <env>dev</env>
 </properties>
 <activation>
 <activeByDefault>true</activeByDefault>
 </activation>
 </profile>
 
 <profile>
 <id>product</id>
 <properties>
 <env>product</env>
 </properties>
 <activation>
 <activeByDefault>false</activeByDefault>
 </activation>
 </profile>
 </profiles>
 ......
 ......
 </project>
 
 | 
中间的properties是用来作为变量能在下文中取到的
然后在build节点中进行如下配置
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 
 | <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 ......
 ......
 <build>
 ......
 ......
 <resources>
 
 
 <resource>
 <directory>src/main/java</directory>
 <includes>
 <include>**/*.properties</include>
 <include>**/*.xml</include>
 </includes>
 
 <filtering>false</filtering>
 </resource>
 
 <resource>
 <directory>src/main/resources/${env}</directory>
 
 <filtering>true</filtering>
 </resource>
 </resources>
 ......
 ......
 </build>
 ......
 ......
 </project>
 
 | 
最后使用maven命令进行编译打包
| 1
 | mvn clean package -Pproduct
 | 
-P为指定某个profile,后面跟上具体的profile就行了,比如上面就指定的是product的profile,如果不加,则默认是dev的profile,可以倒回上面的配置看。
接下来就会根据你的命令进行编译打包啦~