maven profile实现多环境打包

使用maven可以对不同环境进行打包部署:如  mvn clean  package -P development

其中development是环境的变量ID

借助maven提供的profile功能,通过不同的环境激活不同的profile来达到构建的可移植性

一、配置profile

  1. <profiles>  
  2.     <profile>  
  3.         <!-- 本地开发环境 -->  
  4.         <id>development</id>  
  5.         <properties>  
  6.             <profiles.active>development</profiles.active>  
  7.             <deploy.url>http://host:port/manager/text</deploy.url>  
  8.         </properties>  
  9.         <activation>  
  10.             <activeByDefault>true</activeByDefault>  
  11.         </activation>  
  12.     </profile>  
  13.     <profile>  
  14.         <!-- 测试环境 -->  
  15.         <id>test</id>  
  16.         <properties>  
  17.             <profiles.active>test</profiles.active>  
  18.             <deploy.url>http://host:port/manager/text</deploy.url>  
  19.         </properties>  
  20.     </profile>  
  21.     <profile>  
  22.         <!-- 生产环境 -->  
  23.         <id>production</id>  
  24.         <properties>  
  25.             <profiles.active>production</profiles.active>  
  26.             <deploy.url>http://host:port/manager/text</deploy.url>  
  27.         </properties>  
  28.     </profile>  
  29. </profiles

其中开发环境是默认激活的(activeByDefault为true),这样如果在不指定profile时默认是开发环境。

同时每个profile还定义了两个属性,其中profiles.active表示被激活的profile的名称,deploy.url表示发布服务器的地址。

另外host和port分别是发布服务器的主机地址和端口号。

二、配置文件

公用的配置直接放到src/main/resources目录下,开发环境、测试环境、生产环境的配置文件分别放到src/main/resources目录下的development、test、production三个子目录中

三、maven资源插件配置

在pom中的build节点下,配置资源文件的位置

  1. <build>  
  2.     <resources>  
  3.         <resource>  
  4.             <directory>src/main/resources</directory>  
  5.             <!-- 资源根目录排除各环境的配置,使用单独的资源目录来指定 -->  
  6.             <excludes>  
  7.                 <exclude>test/*</exclude>  
  8.                 <exclude>production/*</exclude>  
  9.                 <exclude>development/*</exclude>  
  10.             </excludes>  
  11.         </resource>  
  12.         <resource>  
  13.             <directory>src/main/resources/${profiles.active}</directory>  
  14.         </resource>  
  15.     </resources>  
  16. </build

首先第一个资源文件位置src/main/resources需要排队提各个环境的配置文件,各个环境的配置我们在第二个<resource>节点中通过前面在profile中配置的profiles.active属性来指定。即src/main/resources/${profiles.active}。这样在激活指定的profile时,会加载指定目录下的配置文件,如当前激活的是production profile,那么这个资源目录就是src/main/resources/production。这样就达到了不同环境加载不同配置的目的。

四、配置tomcat-maven-plugin插件

  1. <plugin>  
  2.     <groupId>org.codehaus.mojo</groupId>  
  3.     <artifactId>tomcat-maven-plugin</artifactId>  
  4.     <version>1.2-SNAPSHOT</version>  
  5.     <configuration>  
  6.         <url>${deploy.url}</url>  
  7.         <server>tomcat</server>  
  8.         <path>/appcontext</path>  
  9.     </configuration>  
  10. </plugin>  

其中发布的<url>节点就是在前面profile中配置的deploy.url属性,这样不同的环境就指定了不同的发布地址。<server>和<path>节点分别是发布服务器的用户配置的id以及应用的context名称。

五、构建或发布

运行maven命令时指定不同的profile

mvn clean package -P production即构建出生产环境需要的war包

mvn tomcat:redeploy -P test 即发布到测试环境