在实际开发过程中,开发环境,测试环境和最后部署上线的环境都是不一样的,像数据库连接,都是要变的。

如果不使用Maven的话,我想到的就是修改配置文件,手动的修改;

使用Maven的话,就简单的多了。

先来看一个pom文件:

[html] view plaincopy
  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>org.ygy</groupId>
  5. <artifactId>maven</artifactId>
  6. <packaging>war</packaging>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <name>maven Maven Webapp</name>
  9. <url>http://maven.apache.org</url>
  10. <!-- 属性配置 -->
  11. <properties>
  12. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  13. </properties>
  14. <profiles>
  15. <profile>
  16. <id>devlopment</id>
  17. <properties>
  18. <username>lufei</username>
  19. <password>shishi</password>
  20. </properties>
  21. <build>
  22. <resources>
  23. <resource>
  24. <directory>src/main/resources</directory>
  25. <filtering>true</filtering>
  26. </resource>
  27. </resources>
  28. </build>
  29. <activation>
  30. <activeByDefault>true</activeByDefault>
  31. </activation>
  32. </profile>
  33. <profile>
  34. <id>test</id>
  35. <properties>
  36. <jdbc.url>http://www.deppon.com</jdbc.url>
  37. <jdbc.username>haha</jdbc.username>
  38. <jdbc.password>can you</jdbc.password>
  39. </properties>
  40. <build>
  41. <resources>
  42. <resource>
  43. <directory>src/main/resources</directory>
  44. <filtering>true</filtering>
  45. </resource>
  46. </resources>
  47. </build>
  48. <activation>
  49. <activeByDefault>false</activeByDefault>
  50. </activation>
  51. </profile>
  52. </profiles>
  53. <dependencies>
  54. <dependency>
  55. <groupId>junit</groupId>
  56. <artifactId>junit</artifactId>
  57. <version>4.10</version>
  58. <scope>test</scope>
  59. </dependency>
  60. <!-- 添加Spring依赖 -->
  61. <dependency>
  62. <groupId>org.springframework</groupId>
  63. <artifactId>spring-core</artifactId>
  64. <version>3.1.1.RELEASE</version>
  65. </dependency>
  66. <dependency>
  67. <groupId>org.springframework</groupId>
  68. <artifactId>spring-beans</artifactId>
  69. <version>3.1.1.RELEASE</version>
  70. </dependency>
  71. <dependency>
  72. <groupId>org.springframework</groupId>
  73. <artifactId>spring-context</artifactId>
  74. <version>3.1.1.RELEASE</version>
  75. </dependency>
  76. <dependency>
  77. <groupId>org.springframework</groupId>
  78. <artifactId>spring-jdbc</artifactId>
  79. <version>3.1.1.RELEASE</version>
  80. </dependency>
  81. </dependencies>
  82. <build>
  83. <finalName>maven</finalName>
  84. </build>
  85. </project>

其中,有些标签可能没有用过,就是<profiles>,<profile>

Profile 的作用是允许你在项目文件(pom.xml)里定义若干个 profile 段,然后在编译时选择其中的一个用于覆盖项目文件原先的定义。

[html] view plaincopy
  1. <profile>
  2. <id>devlopment</id>
  3. <properties>
  4. <username>lufei</username>
  5. <password>shishi</password>
  6. </properties>
  7. <build>
  8. <resources>
  9. <resource>
  10. <directory>src/main/resources</directory>
  11. <filtering>true</filtering>
  12. </resource>
  13. </resources>
  14. </build>
  15. <activation>
  16. <activeByDefault>true</activeByDefault>
  17. </activation>
  18. </profile>

我们大体上可以看懂,下面简单介绍一下具体的用法:

1.activation 激活方式

1)根据环境自动激活:如可以根据JDK版本,OS,Maven属性来激活

[html] view plaincopy
  1. <profile>
  2. <id>dev</id>
  3. <activation>
  4. <activeByDefault>false</activeByDefault>
  5. <jdk>1.5</jdk>
  6. <os>
  7. <name>Windows XP</name>
  8. <family>Windows</family>
  9. <arch>x86</arch>
  10. <version>5.1.2600</version>
  11. </os>
  12. <property>
  13. <name>mavenVersion</name>
  14. <value>2.0.5</value>
  15. </property>
  16. <file>
  17. <exists>file2.properties</exists>
  18. <missing>file1.properties</missing>
  19. </file>
  20. </activation>
  21. ...
  22. </profile>

2)通过命令行激活

这是最直接和最简单的方式,比如你定义了一个名为 myProfile 的 profile,你只需要在命令行输入 mvn clean install -P myprofile 就能将其激活,

这种方式的好处很明显,但是有一个很大的弊端,当 profile 比较多的时候,在命令行输入这写 -P 参数会让人觉得厌烦,

所以,如果你一直用这种方式,觉得厌烦了,可以考虑使用其它自动激活的方式。

3)配置默认自动激活

[html] view plaincopy
  1. <profile>
  2. <id>dev</id>
  3. <activation>
  4. <activeByDefault>true</activeByDefault>
  5. </activation>
  6. ...
  7. </profile>

4)配置 settings.xml 文件 profile 激活

settings.xml 文件可以在 ~/.m2 目录下,为某个用户的自定义行为服务,也可以在 M2_HOME/conf 目录下,为整台机器的所有用户服务。

而前者的配置会覆盖后者。同理,由 settings.xml 激活的 profile 意在为用户或者整个机器提供特定环境配置,

比如,你可以在某台机器上配置一个指向本地数据库 URL 的 profile,然后使用该机器的 settings.xml 激活它。激活方式如下:

[html] view plaincopy
  1. <settings>
  2. ...
  3. <activeProfiles>
  4. <activeProfile>local_db</activeProfile>
  5. </activeProfiles>
  6. </settings>

(注:参考博客  激活Maven profile的几种方式 )

2.filtering功能

这里的意思是,过滤src/main/resources下的文件

[html] view plaincopy
  1. <build>
  2. <resources>
  3. <resource>
  4. <directory>src/main/resources</directory>
  5. <filtering>true</filtering>
  6. </resource>
  7. </resources>
  8. </build>

Filtering 是 Maven Resources Plugin 的一个功能,它会使用系统属性或者项目属性的值替换资源文件(*.properties,*.xml)当中 ${…} 符号的值。

比如你系统属性有一项 “user.name=foobar”,那么资源文件当中的 ${user.name} 符号会在 Maven 编译时自动被替换为 “foobar”。

(注:参考博客 Apache Maven 使用 profile 和 filtering 实现多种环境下的资源)

Maven官方Filter讲解:http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html

3.说了这么多,下面来实践一下

这是我们的Maven项目:

一个是配置文件,一个是Spring的配置文件

demo.properties

[java] view plaincopy
  1. hello ,${username}
  2. jdbc.url = ${jdbc.url}
  3. jdbc.username = ${jdbc.username}
  4. jdbc.password = ${jdbc.password}

applicationContext.xml

[java] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"
  4. xmlns:tx="http://www.springframework.org/schema/tx"xmlns:context="http://www.springframework.org/schema/context"
  5. xsi:schemaLocation="
  6. http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  8. http://www.springframework.org/schema/tx
  9. http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  10. http://www.springframework.org/schema/aop
  11. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  12. http://www.springframework.org/schema/context
  13. http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  14. <bean id="simple" class="org.ygy.maven.SimpleEntity">
  15. <property name="username" value="${username}"></property>
  16. <property name="password" value="${password}"></property>
  17. </bean>
  18. </beans>

pom.xml就是最上面提到的:

[html] view plaincopy
  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>org.ygy</groupId>
  5. <artifactId>maven</artifactId>
  6. <packaging>war</packaging>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <name>maven Maven Webapp</name>
  9. <url>http://maven.apache.org</url>
  10. <!-- 属性配置 -->
  11. <properties>
  12. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  13. </properties>
  14. <profiles>
  15. <profile>
  16. <id>devlopment</id>
  17. <properties>
  18. <username>lufei</username>
  19. <password>shishi</password>
  20. </properties>
  21. <build>
  22. <resources>
  23. <resource>
  24. <directory>src/main/resources</directory>
  25. <filtering>true</filtering>
  26. </resource>
  27. </resources>
  28. </build>
  29. <activation>
  30. <activeByDefault>true</activeByDefault>
  31. </activation>
  32. </profile>
  33. <profile>
  34. <id>test</id>
  35. <properties>
  36. <jdbc.url>http://www.deppon.com</jdbc.url>
  37. <jdbc.username>haha</jdbc.username>
  38. <jdbc.password>can you</jdbc.password>
  39. </properties>
  40. <build>
  41. <resources>
  42. <resource>
  43. <directory>src/main/resources</directory>
  44. <filtering>true</filtering>
  45. </resource>
  46. </resources>
  47. </build>
  48. <activation>
  49. <activeByDefault>false</activeByDefault>
  50. </activation>
  51. </profile>
  52. </profiles>
  53. <dependencies>
  54. <dependency>
  55. <groupId>junit</groupId>
  56. <artifactId>junit</artifactId>
  57. <version>4.10</version>
  58. <scope>test</scope>
  59. </dependency>
  60. <!-- 添加Spring依赖 -->
  61. <dependency>
  62. <groupId>org.springframework</groupId>
  63. <artifactId>spring-core</artifactId>
  64. <version>3.1.1.RELEASE</version>
  65. </dependency>
  66. <dependency>
  67. <groupId>org.springframework</groupId>
  68. <artifactId>spring-beans</artifactId>
  69. <version>3.1.1.RELEASE</version>
  70. </dependency>
  71. <dependency>
  72. <groupId>org.springframework</groupId>
  73. <artifactId>spring-context</artifactId>
  74. <version>3.1.1.RELEASE</version>
  75. </dependency>
  76. <dependency>
  77. <groupId>org.springframework</groupId>
  78. <artifactId>spring-jdbc</artifactId>
  79. <version>3.1.1.RELEASE</version>
  80. </dependency>
  81. </dependencies>
  82. <build>
  83. <finalName>maven</finalName>
  84. </build>
  85. </project>

这里有2个profile,一个是development,一个是test,默认自动激活development

注意

[java] view plaincopy
  1. <properties>
  2. <username>lufei</username>
  3. <password>shishi</password>
  4. </properties>
[html] view plaincopy
  1. <properties>
  2. <username>索隆</username>
  3. <password>gogo</password>
  4. <jdbc.url>http://www.deppon.com</jdbc.url>
  5. <jdbc.username>haha</jdbc.username>
  6. <jdbc.password>can you</jdbc.password>
  7. </properties>

这里的<username>和<password>就是我们在配置文件中使用的会变化的配置,Maven会自动将 ${}替换成profile中配置的。

接下来,我们进入到该项目的根目录下,执行Maven命令

1.使用默认激活方式

[java] view plaincopy
  1. mvn clean compile

进入target/classes目录

打开demo.properties和applicationContext.xml文件

会发现,在development中指定的属性都已经成功替换

而demo.properties中,jdbc相关的并没有配置,所以没有替换

2.使用命令更改激活方式

重新输入命令

[java] view plaincopy
  1. mvn clean compile -P test

我们启用了test环境的配置方式

再次进入target/classes文件夹下查看,会发现不同的替换

好了,到这里就可以简单使用了。

Maven知识点记录 - profile相关推荐

  1. SpringMVC知识点记录

    SpringMVC知识点记录 1. SpringMVC简介 2. 入门案例 3. @RequestMapping注解 3.1 @RequestMapping注解的功能 3.2 @RequestMapp ...

  2. Maven中的profile和spring boot中的profile进行结合

    2019独角兽企业重金招聘Python工程师标准>>> 有一些应用,采用了spring boot和spring boot profile.然后想把maven 中的profile和sp ...

  3. javaweb基础知识点记录2

    javaweb基础知识点记录 1.在service方法中,首先获得请求的方法名,然后根据方法名调用对应的doXXXX方法,比如说请求参数为GET,那么就会去调用doGet方法,请求参数为POST,那么 ...

  4. javaweb基础知识点记录1

    javaweb基础知识点记录 1.当我们通过在浏览器的输入栏中直接输入网址的方式访问网页的时候,浏览器采用的就是GET方法向服务器获取资源. 2.我们可以将Servlet看做是嵌套了HTML代码的ja ...

  5. Maven : maven异常记录-must be unique maven duplicate declaration of version

    1.美图 2.背景 maven异常记录 INFO] Scanning for projects... [WARNING] [WARNING] Some problems were encountere ...

  6. 毕业论文知识点记录(四)——MaxEnt模型

    毕业论文知识点记录(四)--MaxEnt模型 0 序言 经过了几次文章分享,数据已经准备得差不多了,师姐说可以先利用现有数据跑一个结果,然后再逐步增加想要的环境数据,改善结果. 另外,谨记师姐的一句话 ...

  7. 毕业论文知识点记录(六)——基于R语言优化maxent模型

    毕业论文知识点记录(六)--基于R语言优化maxent模型 第一步:R安装 这个网上都有很多详细的步骤,就不再详细介绍了. 第二步:R安装包 因为优化maxent模型需要用到kuenm程序包,但是官网 ...

  8. 毕业论文知识点记录(三)——SPSS去相关

    毕业论文知识点记录(三)--SPSS去相关 #(一)数据下载 1.草地贪夜蛾的发生记录,这个数据在前面文章中有描述.草地贪夜蛾发生记录下载 2.气候数据 数据来源:worldclim 我选择的分辨率是 ...

  9. maven异常记录-must be unique

    maven异常记录 [INFO] Scanning for projects... [WARNING] [WARNING] Some problems were encountered while b ...

最新文章

  1. 编码实现字符串转整型的函数(实现函数atoi的功能)
  2. APP-V与RemotoAPP
  3. 一个Web前端自学者的自述
  4. Imagination
  5. Arduino编译bootloader
  6. 二叉树层次遍历--广度遍历和深度遍历
  7. 拆点并查集(poj 1182: 食物链)
  8. WDSR:Wide Activation for Efficient and Accurate Image Super-Resolution
  9. FlinkSQL 列转行/解开map array/unnest/lateral table udtf
  10. MI(mutal information)and Entropy
  11. 互联网公司中秋节礼盒大比拼(2019版)
  12. 3d在c语言中3的作用,c语言中%3d是什么意思?
  13. Referenced file contains errors (xml文件第一行小红叉错误)
  14. Prompt+对比学习,更好地学习句子表征
  15. CASAIM和工信部第五研究所(中国赛宝实验室)合作开展三维测量技术在产品可靠性研究的精确尺寸检测应用和建模仿真试验
  16. Linux 7 种文件类型
  17. 路由器wan口和lan口短接_路由器WAN口和LAN口功能介绍
  18. [深入研究4G/5G/6G专题-4]: DTU系统架构与软件架构
  19. #Arduino,去抖动
  20. 下载安装setuptools

热门文章

  1. eigen 列拼接_R语言-强大的矩阵运算
  2. hyper-v 安装centos7 后,虚拟机无法访问网络。
  3. ibm linux mq 发送消息_IBM MQ简明教程——2. 将消息发送至远程队列
  4. Timeline窗口详解
  5. Unity3d接入视频监控
  6. 带滚动条html转pdf只有一页,关于html页面导出pdf滚动条以下显示不全的问题
  7. OpenShift 4 - 用 Quay Operator 安装 Quay 环境(4.10 修正)
  8. OpenShift 4 - DevSecOps Workshop (4) - 为 Task 增加参数和Workspace
  9. Visual Studio 2019 v16.6 和 v16.7 Preview 1 发布
  10. C#坏习惯:通过不好的例子学习如何制作好的代码——第5部分