http://www.cnblogs.com/tartis/p/5391079.html

  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.</modelVersion>
  4. <groupId>book</groupId>
  5. <artifactId>book</artifactId>
  6. <packaging>war</packaging>
  7. <version>1.0</version>
  8. <name>book</name>
  9.  
  10. <repositories>
  11. <repository>
  12. <id>eap</id>
  13. <url>http://maven.repository.redhat.com/techpreview/all</url>
  14. <releases>
  15. <enabled>true</enabled>
  16. </releases>
  17. <snapshots>
  18. <enabled>true</enabled>
  19. </snapshots>
  20. </repository>
  21. </repositories>
  22.  
  23. <pluginRepositories>
  24. <pluginRepository>
  25. <id>eap</id>
  26. <url>http://maven.repository.redhat.com/techpreview/all</url>
  27. <releases>
  28. <enabled>true</enabled>
  29. </releases>
  30. <snapshots>
  31. <enabled>true</enabled>
  32. </snapshots>
  33. </pluginRepository>
  34. </pluginRepositories>
  35.  
  36. <properties>
  37. <project.build.sourceEncoding>UTF-</project.build.sourceEncoding>
  38. <maven.compiler.source>1.6</maven.compiler.source>
  39. <maven.compiler.target>1.6</maven.compiler.target>
  40.  
  41. <package.environment>local</package.environment>
  42. </properties>
  43.  
  44. <dependencies>
  45.  
  46. <dependency>
  47. <groupId>org.postgresql</groupId>
  48. <artifactId>postgresql</artifactId>
  49. <version>9.2--jdbc4</version>
  50. </dependency>
  51.  
  52. <dependency>
  53. <groupId>mysql</groupId>
  54. <artifactId>mysql-connector-java</artifactId>
  55. <version>5.1.</version>
  56. </dependency>
  57.  
  58. <dependency>
  59. <groupId>org.springframework</groupId>
  60. <artifactId>spring-test</artifactId>
  61. <version>3.2..RELEASE</version>
  62. </dependency>
  63.  
  64. <dependency>
  65. <groupId>org.springframework</groupId>
  66. <artifactId>spring-webmvc</artifactId>
  67. <version>3.2..RELEASE</version>
  68. </dependency>
  69.  
  70. <dependency>
  71. <groupId>org.springframework</groupId>
  72. <artifactId>spring-core</artifactId>
  73. <version>3.2..RELEASE</version>
  74. </dependency>
  75.  
  76. <dependency>
  77. <groupId>org.springframework</groupId>
  78. <artifactId>spring-web</artifactId>
  79. <version>3.2..RELEASE</version>
  80. </dependency>
  81.  
  82. <dependency>
  83. <groupId>org.springframework</groupId>
  84. <artifactId>spring-context</artifactId>
  85. <version>3.2..RELEASE</version>
  86. </dependency>
  87.  
  88. <dependency>
  89. <groupId>org.springframework</groupId>
  90. <artifactId>spring-context-support</artifactId>
  91. <version>3.2..RELEASE</version>
  92. </dependency>
  93.  
  94. <dependency>
  95. <groupId>org.springframework</groupId>
  96. <artifactId>spring-aop</artifactId>
  97. <version>3.2..RELEASE</version>
  98. </dependency>
  99.  
  100. <dependency>
  101. <groupId>org.springframework</groupId>
  102. <artifactId>spring-aspects</artifactId>
  103. <version>3.2..RELEASE</version>
  104. </dependency>
  105.  
  106. <dependency>
  107. <groupId>javax.servlet</groupId>
  108. <artifactId>servlet-api</artifactId>
  109. <version>2.5</version>
  110. <scope>provided</scope>
  111. </dependency>
  112.  
  113. <dependency>
  114. <groupId>javax.servlet.jsp</groupId>
  115. <artifactId>jsp-api</artifactId>
  116. <version>2.2</version>
  117. <scope>provided</scope>
  118. </dependency>
  119.  
  120. <dependency>
  121. <groupId>log4j</groupId>
  122. <artifactId>log4j</artifactId>
  123. <version>1.2.</version>
  124. </dependency>
  125.  
  126. </dependencies>
  127.  
  128. <profiles>
  129. <profile>
  130. <id>local</id>
  131. <properties>
  132. <package.environment>local</package.environment>
  133. </properties>
  134. <activation>
  135. <activeByDefault>true</activeByDefault>
  136. </activation>
  137. </profile>
  138. <profile>
  139. <id>product</id>
  140. <properties>
  141. <package.environment>product</package.environment>
  142. </properties>
  143. </profile>
  144. </profiles>
  145.  
  146. <build>
  147. <finalName>book</finalName>
  148. <resources>
  149. <resource>
  150. <directory>src/main/resources</directory>
  151. <filtering>true</filtering>
  152. <excludes>
  153. <exclude>local/*</exclude>
  154. <exclude>product/*</exclude>
  155. <exclude>public/*</exclude>
  156. </excludes>
  157. </resource>
  158. </resources>
  159.  
  160. <plugins>
  161. <plugin>
  162. <groupId>org.apache.maven.plugins</groupId>
  163. <artifactId>maven-war-plugin</artifactId>
  164. <version>2.1.1</version>
  165. <configuration>
  166. <archive>
  167. <addMavenDescriptor>false</addMavenDescriptor>
  168. </archive>
  169. <warName>book</warName>
  170. <webResources>
  171. <resource>
  172. <directory>src/main/resources/${package.environment}</directory>
  173. <targetPath>WEB-INF/classes</targetPath>
  174. <filtering>true</filtering>
  175. </resource>
  176. <resource>
  177. <directory>src/main/resources/public</directory>
  178. <targetPath>WEB-INF/classes</targetPath>
  179. <filtering>true</filtering>
  180. </resource>
  181. <!--<resource>-->
  182. <!--<directory>${project.build.directory}/classes</directory>-->
  183. <!--<includes>-->
  184. <!--<include>**/*.properties</include>-->
  185. <!--<include>**/*.xml</include>-->
  186. <!--</includes>-->
  187. <!--</resource>-->
  188. </webResources>
  189. </configuration>
  190. </plugin>
  191. </plugins>
  192. </build>
  193.  
  194. </project>

关键代码:

  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>product</id>
  13. <properties>
  14. <package.environment>product</package.environment>
  15. </properties>
  16. </profile>
  17. </profiles>

      这里就是不同的resources文件夹, 我这里只区分本地 产品; 设置为true的就是默认被激活的. 所以后面打包默认就是本地;

  1. <resources>
  2. <resource>
  3. <directory>src/main/resources</directory>
  4. <filtering>true</filtering>
  5. <excludes>
  6. <exclude>local/*</exclude>
  7. <exclude>product/*</exclude>
  8. <exclude>public/*</exclude>
  9. </excludes>
  10. </resource>
  11. </resources>

  这里就是我的资源文件, public里面存放我的公用的, 比如Spring的配置文件, 就是本地与产品都一样的;local 与 product一看就知道了;

  1. <plugin>
  2. <groupId>org.apache.maven.plugins</groupId>
  3. <artifactId>maven-war-plugin</artifactId>
  4. <version>2.1.1</version>
  5. <configuration>
  6. <archive>
  7. <addMavenDescriptor>false</addMavenDescriptor>
  8. </archive>
  9. <warName>book</warName>
  10. <webResources>
  11. <resource>
  12. <directory>src/main/resources/${package.environment}</directory>
  13. <targetPath>WEB-INF/classes</targetPath>
  14. <filtering>true</filtering>
  15. </resource>
  16. <resource>
  17. <directory>src/main/resources/public</directory>
  18. <targetPath>WEB-INF/classes</targetPath>
  19. <filtering>true</filtering>
  20. </resource>
  21. <!--<resource>-->
  22. <!--<directory>${project.build.directory}/classes</directory>-->
  23. <!--<includes>-->
  24. <!--<include>**/*.properties</include>-->
  25. <!--<include>**/*.xml</include>-->
  26. <!--</includes>-->
  27. <!--</resource>-->
  28. </webResources>
  29. </configuration>
  30. </plugin>

  这里就是war的插件了,

  1. ${package.environment}

就是动态指定文件夹了.

  1. <filtering>true</filtering>

这里一定需要设置为true才行.

然后就差不多了. 最后执行 mvn clean ; mvn compile; mvn package; 这里是Maven的生命周期, 其他介绍的文章太多了, 我就不再具体讲. 如果说我要打product的war包;

mvn clean ; mvn compile; mvn -Pproduct package;

那就激活了product 的资源文件;

就这样子, 很简单吧~

Maven根据不同个环境打包, 获取不同的配置文件等等的更多相关文章

  1. maven 使用-P指定环境打包,linux移动配置文件失败,windows成功!

    问题描述:    windows机器使用-P指定环境打包,最后组装文件组装成功,配置文件成功移动,linux下却只移动了jar包. windows:                  linux:   ...

  2. Maven_根据不同个环境打包, 获取不同的配置文件等等

    1 2 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 34 35 36 3 ...

  3. 用maven按环境打包SpringBoot的不同配置文件

    利用maven按环境打包SpringBoot的不同配置文件 application-dev.properties对应开发环境 application-test.properties对应测试环境 app ...

  4. 【转】maven profile实现多环境打包

    作为一名程序员,在开发的过程中,经常需要面对不同的运行环境(开发环境.测试环境.生产环境.内网环境.外网环境等等),在不同的环境中,相关的配置一般不一样,比如数据源配置.日志文件配置.以及一些软件运行 ...

  5. maven profile实现多环境打包

    快速解决: 项目目录 1.pom文件中添加profile <profiles> <profile> <!-- 本地开发环境 --> <id>dev< ...

  6. Maven根据不同的环境打包不同的配置

    前言: 在开发过程中,我们的软件会面对不同的运行环境,比如开发环境.测试环境.生产环境,而我们的软件在不同的环境中,有的配置可能会不一样,比如数据源配置.日志文件配置等等. 那么就需要借助maven提 ...

  7. maven为不同环境打包(hibernate)-超越昨天的自己系列(6)

    超越昨天的自己系列(6) 使用ibatis开发中,耗在dao层的开发时间,调试时间,差错时间,以及适应修改需求的时间太长,导致项目看起来就添删改查,却特别费力.   在项目性能要求不高的情况下,开始寻 ...

  8. Maven插件之portable-config-maven-plugin(不同环境打包)

    在大型的项目组中,分不同的开发环境,测试环境,生产环境(说白了就是配置文件不同,或者数据源,或者服务器,或者数据库等);问题来了,如何使用Maven针对不同的环境来打包呢? Maven提供了Profi ...

  9. Maven适配多种运行环境的打包方案

    项目从开发到部署会历经多个运行环境,如开发环境.测试环境和生产环境,不同环境中项目的配置文件通常也会不同,典型的如数据库连接配置.我们当然不希望每次部署打包前都去修改配置文件以适配环境,利用Maven ...

随机推荐

  1. 【转】关于 Web GIS

    以下部分选自2015-03-01出版的<Web GIS从基础到开发实践(基于ArcGIS API for JavaScript)>一书中的前言部分: Web GIS 概念于1994 年首次 ...

  2. C#窗体 LISTVIEW

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  3. 初学者:JSP登陆界面

    学生登陆查询系统 1 程序的主要功能及特点 实现一个登录界面的基本功能,具体要求: 登录界面login.jsp含有表单,用户能够输入用户名和密码,并提交表单给verify.jsp. Verify.js ...

  4. 利用OVER开窗函数分页

    在SQL Server中,利用SQL进行分页的方法也有很多,今天要总结的是SQL Server 2005中引入的OVER开窗口函数,然后利用开窗函数进行分页. 示例代码如下: -- 设置数据库上下文 ...

  5. Oracle错误:动态执行表不可访问,本会话自动统计被禁止,关闭自动统计之后的问题

    使用PL/SQL时, 每次第一次打开表的时候会提示"动态执行表不可访问,本会话的自动统计被禁止"的错误,一消息如下: V$SESSION,V$SESSTAT,V$STATNAME没 ...

  6. poj 2777

    题意:两个操作:c l r x   l到r之间的颜色变成x q l r      询问l到r有多少种颜色 思路:记一个整数表示哪种颜色是否取了 这里真的是煞笔了,看到这一题第一直觉是异或,但是A^A= ...

  7. scp不可用:WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED

    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! After doing ssh are you seeing this error.No proble ...

  8. js里function的apply vs. bind vs. call

    js里除了直接调用obj.func()之外,还提供了另外3种调用方式:apply.bind.call,都在function的原型里.这3种方法的异同在stackoverflow的这个答案里说的最清楚, ...

  9. sql中replace函数与like结合达到提换的效果

    create table ts ( idd varchar() , co1 varchar() , co2 varchar() , co3 varchar() , gai varchar() ); i ...

  10. Jmeter—5 关联 响应数据传递-正则表达式提取器

    在测试过程中,遇到一个问题:用户登录成功后服务器会返回一个登录凭证,之后所有的操作都需要带上此凭证.我们怎么获取登录凭证并传递给后续的操作? Jmeter提供了正则表达式提取器,用变量提取参数,后续通 ...