使用 maven profile

一个项目可以部署在不同的环境当中,maven 的 profile 针对不同的环境指定各自的编译方法。在 pom.xml 的 profile 中,可以根据不同的环境定制以下内容:

  • <repositories>
  • <pluginRepositories>
  • <dependencies>
  • <plugins>
  • <properties>
  • <dependencyManagement>
  • <distributionManagement>
  • <build>
    • <defaultGoal>
    • <resources>
    • <testResources>
    • <finalName>

可以设置默认激活的 profile

  1. <profiles>
  2. <profile>
  3. <id>profile-1</id>
  4. <activation>
  5. <activeByDefault>true</activeByDefault>
  6. </activation>
  7. ...
  8. </profile>
  9. </profiles>

build 配置项

build 配置项可以在两处出现:

  1. <project xmlns="http://maven.apache.org/POM/4.0.0"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
  4. http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. ...
  6. <!-- 项目级别的构建,基础配置 -->
  7. <build>...</build>
  8.  
  9. <profiles>
  10. <profile>
  11. <!-- 特殊的构建 -->
  12. <build>...</build>
  13. </profile>
  14. </profiles>
  15. </project>

以下是 build 的详细配置

  1. <build>
  2. <defaultGoal>install</defaultGoal>
  3. <directory>${basedir}/target</directory>
  4. <finalName>${artifactId}-${version}</finalName>
  5. <filters> <!-- 过滤器,用于过滤resource中的各个文件 -->
  6. <filter>filters/filter1.properties</filter>
  7. </filters>
  8. <resources>
  9. <resource>
  10. <targetPath>META-INF/plexus</targetPath>
  11. <filtering>false</filtering> <!-- 是否使用过滤器 -->
  12. <directory>${basedir}/src/main/plexus</directory>
  13. <includes>
  14. <include>configuration.xml</include>
  15. </includes>
  16. <excludes>
  17. <exclude>**/*.properties</exclude>
  18. </excludes>
  19. </resource>
  20. </resources>
  21. <testResources>
  22. ...
  23. </testResources>
  24. <plugins>
  25. <plugin>
  26. <groupId>org.apache.maven.plugins</groupId>
  27. <artifactId>maven-jar-plugin</artifactId>
  28. <version>2.0</version>
  29. <extensions>false</extensions> <!-- 是否使用扩展 -->
  30. <inherited>true</inherited> <!-- 是否可继承 -->
  31. <configuration> <!-- 当前插件的配置 -->
  32. <classifier>test</classifier>
  33. </configuration>
  34. <dependencies>...</dependencies>
  35. <executions> <!-- 配置插件在哪个阶段使用 -->
  36. <execution>
  37. <id>echodir</id>
  38. <goals>
  39. <goal>run</goal>
  40. </goals>
  41. <phase>verify</phase>
  42. <inherited>false</inherited>
  43. <configuration>
  44. <tasks>
  45. <echo>Build Dir: ${project.build.directory}</echo>
  46. </tasks>
  47. </configuration>
  48. </execution>
  49. </executions>
  50. </plugin>
  51. </plugins>
  52. <sourceDirectory>${basedir}/src/main/java</sourceDirectory>
  53. <scriptSourceDirectory>${basedir}/src/main/scripts</scriptSourceDirectory>
  54. <testSourceDirectory>${basedir}/src/test/java</testSourceDirectory>
  55. <outputDirectory>${basedir}/target/classes</outputDirectory>
  56. <testOutputDirectory>${basedir}/target/test-classes</testOutputDirectory>
  57. <extensions> <!-- 通过扩展来修改插件的行为 -->
  58. <extension>
  59. <groupId>org.apache.maven.wagon</groupId>
  60. <artifactId>wagon-ftp</artifactId>
  61. <version>1.0-alpha-3</version>
  62. </extension>
  63. </extensions>
  64. </build>

filter 规则

maven 通过过滤器来修改部署时的不同配置。部署时的所有资源的配置,如果根据环境不同,有不同的配置,则需要在资源中加上以下形式的标记:

  1. ${tag.subtag}

如,在 spring.xml 中要配置上传文件的路径:

  1. <beans>
  2. <bean id="uploadService" class="com.oist.project.service.UploadServiceImpl">
  3. <property name="uploadDir" value="${spring.uploadDir}"/>
  4. </bean>
  5. </beans>

在 pom.xml 中进行以下配置:

  1. <project xmlns="http://maven.apache.org/POM/4.0.0"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
  4. http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. ...
  6. <build>
  7. <filters> <!-- 指定 filter -->
  8. <filter>src/main/filters/${deploy.env}.properties</filter>
  9. </filters>
  10. <resources>
  11. <resource> <!-- spring.xml 应该在 src/main/resource 目录下 -->
  12. <filtering>true</filtering> <!-- 是否使用过滤器 -->
  13. </resource>
  14. </resources>
  15. </build>
  16.  
  17. <profiles>
  18. <profile>
  19. <id>development</id>
  20. <activation>
  21. <activeByDefault>true</activeByDefault>
  22. </activation>
  23. <propertys>
  24. <deploy.env>develop</deploy.env>
  25. </propertys>
  26. </profile>
  27.  
  28. <profile>
  29. <id>test</id>
  30. <propertys>
  31. <deploy.env>test</deploy.env>
  32. </propertys>
  33. </profile>
  34.  
  35. <profile>
  36. <id>production</id>
  37. <propertys>
  38. <deploy.env>production</deploy.env>
  39. </propertys>
  40. </profile>
  41.  
  42. </profiles>
  43. </project>

然后就可以针对不同的环境设置不同的目录了:

src/main/filters/develop.properties 文件

  1. # 上传路径:
  2. spring.uploadDir=c:/uploadDir

src/main/filters/test.properties 文件

  1. # 上传路径:
  2. spring.uploadDir=/tmp/upload_dir

src/main/filters/production.properties 文件

  1. # 上传路径:
  2. spring.uploadDir=/app/project/upload_dir

如果配置了多个 filter,并且两个 filter 中有相同的 key,则后面的 value 为最终取值。

  1. <build>
  2. <filters>
  3. <filter>src/main/filters/production.properties</filter>
  4. <filter>src/main/filters/test.properties</filter>
  5. </filters>
  6. </build>

如以上的配置,因为 test.properties 在最后,因而 spring.uploadDir 为 test.properties 的取值 /tmp/upload_dir

maven 的 properties 加载顺序

  1. <build><filters> 中的配置
  2. pom.xml 中的 <properties>
  3. mvn -Dproperty=value 中定义的 property

相同 key 的 property,以最后一个文件中的配置为最终配置。

利用这一规则,可以在打升级 war 包的时候,不将 lib/*.jar 打进 war 包。

  1. <project>
  2. ...
  3. <properties>
  4. <lib.exclude>abc.jar</lib.exclude>
  5. </properties>
  6. ...
  7. <build>
  8. <plugin>
  9. <groupId>org.apache.maven.plugins</groupId>
  10. <artifactId>maven-war-plugin</artifactId>
  11. <version>2.3</version>
  12. <configuration>
  13. <packagingExcludes>WEB-INF/lib/${lib.exclude}.jar</packagingExcludes>
  14. </configuration>
  15. </plugin>
  16. </build>
  17. </project>

打第一个 war 包的时候,可以使用以下命令:

  1. mvn clean compile war:war -Pproduction

今后要升级,不用再把 lib 打进 war 包(这样可以使得 war 包体积减少很多),可以使用以下的命令:

  1. mvn clean compile war:war -Pproduction -Dlib.execlude=*

用 maven filter 管理不同环境的配置文件的更多相关文章

  1. jenkins配置maven工程指定不同环境的配置文件打包

    打包命令 这里我们指定配置文件问test 这个是在pom.xml里面定义的, 里面有test,production和devlop三个定义 在不同环境使用Jenkins的时候,-P后面加上不同的参数 我 ...

  2. Maven Filter与Profile隔离生产环境与开发环境

    Maven Filter与Profile隔离生产环境与开发环境 在不同的开发阶段,我们一般用到不同的环境,开发阶段使用开发环境的一套东西,测试环境使用测试环境的东西,可能有多个测试环境,生产环境使用的 ...

  3. 使用maven来管理您的java项目

    maven是一个项目管理工具,使用maven可以自动管理java项目的整个生命周期,包括编译.构建.测试.发布和报告等.在大型项目开发中,使用maven来管理是必不可少的. 一.安装maven 1.W ...

  4. maven工程的多环境配置方案(profile)

    前言: 写一篇水文来打发下时间吧^_^. 在应用开发中, 总会遇到开发/测试/预发布/线上环境, 其环境不同, 其具体的配置项也有所不同, 因此如何快速的切换各个环境配置, 进行打包配置, 成了一个小 ...

  5. maven filter插件只替换了部分变量问题

    maven filter简介 maven的resources插件,有一个filter的作用,能够在打包的时候,从特定文件里读取key-value对,替换配置文件中的占位符变量.很多线上线下有不同环境的 ...

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

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

  7. 【Tool】Windows系统安装Maven依赖管理工具

    安装Maven依赖管理工具 官网下载地址:http://maven.apache.org/download.cgi 系统环境要求: [JDK]Maven3.3版本+需要JDK1.7版本以上支持 [内存 ...

  8. 使用maven profile实现多环境可移植构建(转自CSDN)

    使用maven profile实现多环境可移植构建 标签: maven profilemaven自动构建maven自动部署maven可移植构建持续集成 2014-04-25 23:37 26905人阅 ...

  9. Eclipse+Tomcat+MAVEN+SVN项目完整环境搭建

    1.JDK的安装 首先下载JDK,这个从sun公司官网可以下载,根据自己的系统选择64位还是32位,安装过程就是next一路到底.安装完成之后当然要配置环境变量了. ————————————————— ...

随机推荐

  1. [Linux] 查看jar包内容

    jar vtf  fileName.jar 用法: jar {ctxui}[vfm0Me] [jar-file] [manifest-file] [entry-point] [-C dir] file ...

  2. Flash: An Efficient and Portable Web Server

    Introduction This paper presents the design of a new Web server architecture called the asymmetric m ...

  3. c# 如何中List<object>中去掉object对象中的重复列数据?

    //去掉重复 var title = modelList.GroupBy(m => m.Title.ToLower().Trim()).Select(m => new { ID = m.F ...

  4. Python爬虫学习(11):Beautiful Soup的使用

    之前我们从网页中提取重要信息主要是通过自己编写正则表达式完成的,但是如果你觉得正则表达式很好写的话,那你估计不是地球人了,而且很容易出问题.下边要介绍的Beautiful Soup就可以帮你简化这些操 ...

  5. 树链剖分+线段树 CF 593D Happy Tree Party(快乐树聚会)

    题目链接 题意: 有n个点的一棵树,两种操作: 1. a到b的路径上,给一个y,对于路径上每一条边,进行操作,问最后的y: 2. 修改某个条边p的值为c 思路: 链上操作的问题,想树链剖分和LCT,对 ...

  6. Nodemanager Out of heap memory[fix bug全过程]

    问题: 自己写了一个yarn上的application,发现nodemanager过段时间,会out of memory退出,把nodemanager的heap memory从1G增大到2G也是无法避 ...

  7. linux I/O stack cache 强制刷新

    linux 存储子系统作为最为复杂的子系统之一,拥有很深的模块栈(如图),其中很多模块又有自己的缓存功能(如下图).实际应用中,用户下发的数据停留在哪个缓存中,是否已经写入磁盘,这些操作对用户来说是个 ...

  8. 回流(reflow)与重绘(repaint)

    最近项目排期不紧,于是看了一下之前看了好久也没看明白的chrome调试工具的timeline.但是很遗憾,虽然大概懂了每一项是做什么的,但是用起来并不能得心应手.所以今天的重点不是timeline,而 ...

  9. js get browser vertion (js获取浏览器信息版本)

    1问题:js get browser vertion (js获取浏览器信息版本) 2解决方案 Copy this script into your JavaScript files. It works ...

  10. 基于shell脚本比较数字大小

    让用户输入两个数来比较他们的大小 先用touch命令新建一个1.sh文件 在用vi进入i进入编辑状态 输入 #!/bin/bash read "" a read "&qu ...