Maven根据不同个环境打包, 获取不同的配置文件

本文转自:http://blog.csdn.net/saindy5828/article/details/52704609

本地的测试配置文件与内测的配置,甚至线上的配置不一样, 以前比较土的做法就是每次复制粘贴。然后不停的来回改文件。

现在是将本地的开发环境,内测的,生产环境的,各个不同的配置文件放在配置文件夹里。例如:

在项目的资源文件夹 resources下面再新建四个文件夹:

local: 表示本地开发环境的配置文件

internal : 表示 内测环境配置文件 

product : 表示生产环境配置环境

除了三这不同的配置文件之外,还有一些是共同的,与环境无关的,就可以放在同一个文件夹里,统一管理,这个文件夹就是:

public : 表示共同的配置文件

结果如一下:

相应在的pom里进行设置:

[html]  view plain  copy
  1. <resources>  
  2.     <resource>  
  3.         <directory>src/main/resources</directory>  
  4.         <filtering>true</filtering>  
  5.         <excludes>  
  6.             <exclude>local/*</exclude>  
  7.             <exclude>internal/*</exclude>  
  8.             <exclude>product/*</exclude>  
  9.             <exclude>public/*</exclude>  
  10.         </excludes>  
  11.     </resource>  
  12. </resources>  

完整理的 pom.xml  内容如下,整个贴上,仅供参考:

[html]  view plain  copy
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  
  3.     <modelVersion>4.0.0</modelVersion>  
  4.     <groupId>net.saindy</groupId>  
  5.     <artifactId>faq</artifactId>  
  6.     <packaging>war</packaging>  
  7.     <version>0.0.1-SNAPSHOT</version>  
  8.     <name>faq Maven Webapp</name>  
  9.     <url>http://maven.apache.org</url>  
  10.   
  11.     <properties>  
  12.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  13.         <tomcat.version>2.1</tomcat.version>  
  14.         <slf4j.version>1.7.5</slf4j.version>  
  15.         <log4j.version>1.2.17</log4j.version>  
  16.         <java.version>1.7</java.version>  
  17.         <commons-io.version>2.4</commons-io.version>  
  18.         <java.encoding>utf8</java.encoding>  
  19.         <java.home>D:\Development\Java\jdk1.7.0_76\jre</java.home>  
  20.   
  21.         <package.environment>local</package.environment>  
  22.     </properties>  
  23.   
  24.     <dependencies>  
  25.         <dependency>  
  26.             <groupId>junit</groupId>  
  27.             <artifactId>junit</artifactId>  
  28.             <version>3.8.1</version>  
  29.             <scope>test</scope>  
  30.         </dependency>  
  31.   
  32.         <!-- web setting start -->  
  33.         <dependency>  
  34.             <groupId>javax.servlet</groupId>  
  35.             <artifactId>jstl</artifactId>  
  36.             <version>1.2</version>  
  37.             <type>jar</type>  
  38.         </dependency>  
  39.         <dependency>  
  40.             <groupId>javax.servlet</groupId>  
  41.             <artifactId>javax.servlet-api</artifactId>  
  42.             <version>3.0.1</version>  
  43.             <scope>provided</scope>  
  44.         </dependency>  
  45.         <dependency>  
  46.             <groupId>javax.servlet.jsp</groupId>  
  47.             <artifactId>jsp-api</artifactId>  
  48.             <version>2.2</version>  
  49.             <scope>provided</scope>  
  50.         </dependency>  
  51.         <!-- web setting end -->  
  52.   
  53.         <dependency>  
  54.             <groupId>commons-io</groupId>  
  55.             <artifactId>commons-io</artifactId>  
  56.             <version>${commons-io.version}</version>  
  57.         </dependency>  
  58.   
  59.         <dependency>  
  60.             <groupId>org.apache.commons</groupId>  
  61.             <artifactId>commons-lang3</artifactId>  
  62.             <version>3.4</version>  
  63.         </dependency>  
  64.   
  65.         <dependency>  
  66.             <groupId>org.apache.commons.lang</groupId>  
  67.             <artifactId>commons-lang</artifactId>  
  68.             <version>2.1</version>  
  69.         </dependency>  
  70.   
  71.         <dependency>  
  72.             <groupId>org.apache.commons</groupId>  
  73.             <artifactId>commons-fileupload</artifactId>  
  74.             <version>1.2.1</version>  
  75.         </dependency>  
  76.   
  77.         <dependency>  
  78.             <groupId>org.apache.commons</groupId>  
  79.             <artifactId>commons-email</artifactId>  
  80.             <version>1.2</version>  
  81.         </dependency>  
  82.   
  83.         <dependency>  
  84.             <groupId>org.apache.commons</groupId>  
  85.             <artifactId>commons-pool2</artifactId>  
  86.             <version>2.4.2</version>  
  87.         </dependency>  
  88.   
  89.         <dependency>  
  90.             <groupId>com.alibaba</groupId>  
  91.             <artifactId>fastjson</artifactId>  
  92.             <version>1.2.5</version>  
  93.         </dependency>  
  94.   
  95.         <dependency>  
  96.             <groupId>org.slf4j</groupId>  
  97.             <artifactId>slf4j-api</artifactId>  
  98.             <version>${slf4j.version}</version>  
  99.         </dependency>  
  100.   
  101.         <dependency>  
  102.             <groupId>log4j</groupId>  
  103.             <artifactId>log4j</artifactId>  
  104.             <version>${log4j.version}</version>  
  105.         </dependency>  
  106.   
  107.         <dependency>  
  108.             <groupId>mysql</groupId>  
  109.             <artifactId>mysql-connector-java</artifactId>  
  110.             <version>5.1.38</version>  
  111.         </dependency>  
  112.   
  113.         <dependency>  
  114.             <groupId>net.sf.ehcache</groupId>  
  115.             <artifactId>ehcache-core</artifactId>  
  116.             <version>2.5.2</version>  
  117.         </dependency>  
  118.   
  119.         <dependency>  
  120.             <groupId>com.alibaba</groupId>  
  121.             <artifactId>druid</artifactId>  
  122.             <version>1.0.9</version>  
  123.         </dependency>  
  124.   
  125.         <dependency>  
  126.             <groupId>javax.mail</groupId>  
  127.             <artifactId>mail</artifactId>  
  128.             <version>1.4.7</version>  
  129.             <scope>provided</scope>  
  130.         </dependency>  
  131.   
  132.     </dependencies>  
  133.   
  134.     <!-- 配置架包从中央仓库下载 -->  
  135.     <repositories>  
  136.         <!-- 仓库地址 -->  
  137.         <repository>  
  138.             <id>my-nexus-repositories</id>  
  139.             <name>my-nexus-repository</name>  
  140.             <url>http://192.168.0.9:8081/nexus/content/groups/public/</url>  
  141.             <releases>  
  142.                 <enabled>true</enabled>  
  143.             </releases>  
  144.             <snapshots>  
  145.                 <enabled>false</enabled>  
  146.             </snapshots>  
  147.         </repository>  
  148.   
  149.     </repositories>  
  150.   
  151.     <pluginRepositories>  
  152.         <!-- 插件地址 -->  
  153.         <pluginRepository>  
  154.             <id>my-nexus-pluginRepositories</id>  
  155.             <name>my-nexus-repository</name>  
  156.             <url>http://192.168.0.9:8081/nexus/content/groups/public/</url>  
  157.             <releases>  
  158.                 <enabled>true</enabled>  
  159.             </releases>  
  160.             <snapshots>  
  161.                 <enabled>false</enabled>  
  162.             </snapshots>  
  163.         </pluginRepository>  
  164.   
  165.     </pluginRepositories>  
  166.     <!-- 配置发布到中央仓库 -->  
  167.     <distributionManagement>  
  168.         <repository>  
  169.             <id>my-nexus-releases</id>  
  170.             <name>my-nexus-releases</name>  
  171.             <url>http://192.168.0.9:8081/nexus/content/repositories/my-nexus-releases/</url>  
  172.         </repository>  
  173.   
  174.   
  175.         <snapshotRepository>  
  176.             <id>my-nexus-snapshot</id>  
  177.             <name>my-nexus-snapshots</name>  
  178.             <url>http://192.168.0.9:8081/nexus/content/repositories/my-nexus-snapshots/</url>  
  179.         </snapshotRepository>  
  180.     </distributionManagement>  
  181.   
  182.     <profiles>  
  183.         <profile>  
  184.             <id>local</id><!-- 本地开发环境 -->  
  185.             <properties>  
  186.                 <package.environment>local</package.environment>  
  187.             </properties>  
  188.             <activation>  
  189.                 <activeByDefault>true</activeByDefault>  
  190.             </activation>  
  191.         </profile>  
  192.         <profile>  
  193.             <id>internal</id><!-- 内测环境 -->  
  194.             <properties>  
  195.                 <package.environment>internal</package.environment>  
  196.             </properties>  
  197.         </profile>  
  198.         <profile>  
  199.             <id>product</id><!-- 生产环境 -->  
  200.             <properties>  
  201.                 <package.environment>product</package.environment>  
  202.             </properties>  
  203.         </profile>  
  204.     </profiles>  
  205.   
  206.     <build>  
  207.         <finalName>${project.artifactId}</finalName>  
  208.         <resources>  
  209.             <resource>  
  210.                 <directory>src/main/resources</directory>  
  211.                 <filtering>true</filtering>  
  212.                 <excludes>  
  213.                     <exclude>local/*</exclude>  
  214.                     <exclude>internal/*</exclude>  
  215.                     <exclude>product/*</exclude>  
  216.                     <exclude>public/*</exclude>  
  217.                 </excludes>  
  218.             </resource>  
  219.         </resources>  
  220.         <plugins>  
  221.             <!-- tomcat7插件 -->  
  222.             <plugin>  
  223.                 <groupId>org.apache.tomcat.maven</groupId>  
  224.                 <artifactId>tomcat7-maven-plugin</artifactId>  
  225.                 <version>${tomcat.version}</version>  
  226.                 <configuration>  
  227.                     <!-- 自动发布到tomcat -->  
  228.                     <url>http://localhost:80/manager/text</url>  
  229.                     <update>true</update>  
  230.                     <server>tomcat</server>  
  231.                     <username>admin</username>  
  232.                     <password>admin</password>  
  233.                     <uriEncoding>utf-8</uriEncoding>  
  234.                     <path>/${project.artifactId}</path>  
  235.                 </configuration>  
  236.             </plugin>  
  237.   
  238.             <plugin>  
  239.                 <groupId>org.apache.maven.plugins</groupId>  
  240.                 <artifactId>maven-war-plugin</artifactId>  
  241.                 <version>2.1.1</version>  
  242.                 <configuration>  
  243.                     <archive>  
  244.                         <addMavenDescriptor>false</addMavenDescriptor>  
  245.                     </archive>  
  246.                     <warName>${project.artifactId}</warName>  
  247.                     <webResources>  
  248.                         <!-- 不同的环境,使用不同的配置文件 -->  
  249.                         <resource>  
  250.                             <directory>src/main/resources/${package.environment}</directory>  
  251.                             <targetPath>WEB-INF/classes</targetPath>  
  252.                             <filtering>true</filtering>  
  253.                         </resource>  
  254.                         <!-- 公共的配置文件 -->  
  255.                         <resource>  
  256.                             <directory>src/main/resources/public</directory>  
  257.                             <targetPath>WEB-INF/classes</targetPath>  
  258.                             <filtering>true</filtering>  
  259.                         </resource>  
  260.                     </webResources>  
  261.                 </configuration>  
  262.             </plugin>  
  263.         </plugins>  
  264.   
  265.         <pluginManagement>  
  266.             <plugins>  
  267.                 <plugin>  
  268.                     <groupId>org.apache.maven.plugins</groupId>  
  269.                     <artifactId>maven-compiler-plugin</artifactId>  
  270.                     <configuration>  
  271.                         <source>${java.version}</source>  
  272.                         <target>${java.version}</target>  
  273.                         <encoding>${java.encoding}</encoding>  
  274.                         <compilerArguments>  
  275.                             <verbose />  
  276.                             <!-- <bootclasspath>${java.home}/lib/rt.jar;${java.home}/lib/jce.jar</bootclasspath> -->  
  277.                         </compilerArguments>  
  278.                     </configuration>  
  279.                 </plugin>  
  280.             </plugins>  
  281.         </pluginManagement>  
  282.     </build>  
  283. </project>  

对这个pom 再多作一些说明:

[html]  view plain  copy
  1. <profiles>  
  2.     <profile>  
  3.         <id>local</id><!-- 本地开发环境 -->  
  4.         <properties>  
  5.             <package.environment>local</package.environment>  
  6.         </properties>  
  7.         <activation>  
  8.             <activeByDefault>true</activeByDefault>  
  9.         </activation>  
  10.     </profile>  
  11.     <profile>  
  12.         <id>internal</id><!-- 内测环境 -->  
  13.         <properties>  
  14.             <package.environment>internal</package.environment>  
  15.         </properties>  
  16.     </profile>  
  17.     <profile>  
  18.         <id>product</id><!-- 生产环境 -->  
  19.         <properties>  
  20.             <package.environment>product</package.environment>  
  21.         </properties>  
  22.     </profile>  
  23. </profiles>  

这里就是不同的resources文件夹, 在这里可以区分本地、内测、生产;  

[html]  view plain  copy
  1. <activeByDefault>true</activeByDefault>  
设置为true的就是默认被激活的. 所以后面打包默认就是本地;

[html]  view plain  copy
  1. <!-- 不同的环境,使用不同的配置文件 -->  
  2.                         <resource>  
  3.                             <directory>src/main/resources/${package.environment}</directory>  
  4.                             <targetPath>WEB-INF/classes</targetPath>  
  5.                             <filtering>true</filtering>  
  6.                         </resource>  

[html]  view plain  copy
  1. <directory>src/main/resources/${package.environment}</directory>  

这边指的是动态文件夹

最后别忘了 

[html]  view plain  copy
  1. <filtering>true</filtering>  

这个一定要设置成true

最后, 打包, 使用Maven的生命周期,比如,打包生产环境的配置文件,package  打包生成 war 文件。

mvn -Pproduct package; 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值