How to activate maven profile inside eclipse

Normally maven is use for project dependency management and lifecycle, so there are several developers working on it. Each has its own development environment either in windows, linux or mac. To do this we create several profiles for each developer in pom.xml.

 <profiles>
<profile>
<id>windows</id>
<properties>
<jboss.home>C:/manaty/jboss-4.2.3.GA</jboss.home>
</properties>
</profile>
<profile>
<id>linux</id>
<properties>
<jboss.home>/home/project/jboss-4.2.3.GA</jboss.home>
</properties>
</profile>
</profiles>

And then invoke mvn with the -P argument. Example:

mvn install -Pwindows
mvn install -Plinux

How could we do the same inside eclipse. There are 2 ways: 1.) Right click on the project->click properties->select maven menu


 

If in case maven is not present, right click on project->Maven->Enable Maven Dependency 2.) Right click on project->Run As->Run Configurations->Maven Build->Add a new build (set profile and goal)

 
 
 
 
 
 
 
 

Maven 整合 spring profile实现多环境自动切换

profile主要用在项目多环境运行的情况下,比如开发环境、测试环境、线上生产环境。 
我负责的项目某个数据管理后台涉及到包含测试环境在内的12个不同的运行环境,所以每次发布都很蛋疼很纠结,配置改过来改过去,到最后有些环境都忘了怎么配的。 
下面以这个项目为例介绍。 
准备条件:spring3.x、Maven 2

这里是整合spring的profile和Maven的profile功能

spring的profile配置

首先是spring的配置数据源和属性文件

  1. <beans profile="xmj_old">
  2. <bean id="dataSource"
  3. class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  4. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  5. <property name="url"
  6. value="jdbc:mysql://127.0.0.1:3306/db1?characterEncoding=utf8" />
  7. <property name="password" value="123456" />
  8. <property name="username" value="abc" />
  9. </bean>
  10. <bean id="messageSource"
  11. class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
  12. <property name="basenames">
  13. <list>
  14. <value>classpath:messages_zh_CN</value>
  15. <value>classpath:messages/messages_en_US-xmj_old</value>
  16. </list>
  17. </property>
  18. </bean>
  19. </beans>
  20. <beans profile="xmj_new">
  21. <bean id="dataSource"
  22. class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  23. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  24. <property name="url"
  25. value="jdbc:mysql://127.0.0.1:3306/db1?characterEncoding=utf8" />
  26. <property name="password" value="123456" />
  27. <property name="username" value="abc" />
  28. </bean>
  29. <bean id="messageSource"
  30. class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
  31. <property name="basenames">
  32. <list>
  33. <value>classpath:messages_zh_CN</value>
  34. <value>classpath:messages/messages_en_US-xmj_new</value>
  35. </list>
  36. </property>
  37. </bean>
  38. </beans>
  39. ...
  40. ...
  41. ...
  1. 这里的message_en_US-*系列属性文件是配置站点的名称和黑名单的位置:
  1. Java代码  
  1. resource.blacklist.dir=/var/resource/blacklist.txt
  2. system.title.id=system.xmj_old.title
  1. 激活spring的profile
  2. profile是配置完了,但还要激活spring的profile特性。
  3. 在web.xml做如下配置:
  1. Xml代码  
  1. <context-param>
  2. <param-name>spring.profiles.active</param-name>
  3. <param-value>xmj_old</param-value>
  4. </context-param>
  5. 这样就激活spring配置文件中的profile为xmj_old的配置,但这样手动配置依然要改web.xml配置。
  6. Maven也带了profile的功能,而且我们的项目一般都是由Maven管理的,下面介绍Maven配置profile。
  7. Maven 配置profile
  8. Maven配置profile也很简单,举例如下:
  1. Xml代码  
  1. <profiles>
  2. <profile>
  3. <id>xmj_old</id>
  4. <activation>
  5. <activeByDefault>true</activeByDefault>
  6. </activation>
  7. <properties>
  8. <profiles.activation>xmj_old</profiles.activation>
  9. </properties>
  10. <build>
  11. <plugins>
  12. <plugin>
  13. <groupId>org.codehaus.mojo</groupId>
  14. <artifactId>tomcat-maven-plugin</artifactId>
  15. <version>1.1</version>
  16. <configuration>
  17. <!-- 配置项目自动发布服务器 -->
  18. <url>http://xx.xx.xx.xx:8080/manager/text</url>
  19. <path>/xmj-manager</path>
  20. <server>Tomcat</server>
  21. <warFile>target/${profiles.activation}.war</warFile>
  22. </configuration>
  23. </plugin>
  24. </plugins>
  25. </build>
  26. </profile>
  27. <profile>
  28. <id>xmj_new</id>
  29. <properties>
  30. <profiles.activation>xmj_new</profiles.activation>
  31. </properties>
  32. <build>
  33. <plugins>
  34. <plugin>
  35. <groupId>org.codehaus.mojo</groupId>
  36. <artifactId>tomcat-maven-plugin</artifactId>
  37. <version>1.1</version>
  38. <configuration>
  39. <!-- 配置项目自动发布服务器 -->
  40. <url>http://xx2.xx.xx.xx:8080/manager/text</url>
  41. <path>/xmj-manager</path>
  42. <server>Tomcat</server>
  43. <warFile>target/${profiles.activation}.war</warFile>
  44. </configuration>
  45. </plugin>
  46. </plugins>
  47. </build>
  48. </profile>
  49. ...
  50. ...
  51. ...
  52. </profiles>
  53. activeByDefault标签配置true表示默认激活的环境,直接install就是使用此配置的环境。
  54. 这里有一个自定义属性配置:profiles.activation 它就是整合Maven profile 和spring profile 的重要配置
  55. 整合Maven profile 和spring profile
  56. 通过Maven管理web实现自动化,少改动就少出错,将上面web.xml配置改为下面的:
  1. Xml代码  
  1. <context-param>
  2. <param-name>spring.profiles.active</param-name>
  3. <param-value>${profiles.activation}</param-value>
  4. </context-param>
  5. 这还不算完,还要配置Maven的一个插件:
  1. Xml代码  
  1. <plugin>
  2. <groupId>org.apache.maven.plugins</groupId>
  3. <artifactId>maven-war-plugin</artifactId>
  4. <configuration>
  5. <warName>${profiles.activation}</warName>
  6. <!-- 激活spring profile -->
  7. <webResources>
  8. <resource>
  9. <filtering>true</filtering>
  10. <directory>src/main/webapp</directory>
  11. <includes>
  12. <include>**/web.xml</include>
  13. </includes>
  14. </resource>
  15. </webResources>
  16. <warSourceDirectory>src/main/webapp</warSourceDirectory>
  17. <webXml>src/main/webapp/WEB-INF/web.xml</webXml>
  18. </configuration>
  19. </plugin>
  20. 打包的时候Maven会去自动修改web.xml文件,根据占位符自动替换为对应的属性值
  21. warName标签可以不配置,我这里加上了所以需要加上前面的target/${profiles.activation}.war,否则会报找不到war包的异常。
  22. 执行profile
  23. 下面是见证奇迹的时刻
  24. 在eclipse中执行命令:
  1. Java代码  
  1. clean install
  2. 前面配置的默认环境是xmj_old这个,在执行完命令之后target文件夹下面出现了一个名为xmj_old.war的war包
  3. 打开war包查看web.xml
  4. 再测试下切换到其他的环境
  5. 执行命令
  1. Java代码  
  1. clean install -P xmj_new
  2. -P 参数后面加上profile的id即可完成自动切换,执行完之后看下是否已经自动切换到id为xmj_new的profile环境下
  3. target文件下有了xmj_new.war的war包(xmj_old.war被clean掉了)
  4. 打开war包查看web.xml文件
  5. 如预期所料,Maven结合spring成功的完成了多环境的自动切换
  6. 注:上面web.xml中的display-name和webAppRootKey都是使用占位符${profiles.activation}然后Maven自动替换的,如果你的项目中使用log4j/logback和spring,然后同一个项目部署多个实例到一个tomcat中,建议配置webAppRootKey这个参数
  7. 请参考 log4j/logback + spring的webRootKey bug
  8. 在本地eclipse内置tomcat中运行
  9. 由于内置tomcat是直接编译源码然后放到指定的位置去加载
  10. 所以上述的方法对于在内置tomcat运行是行不通的,在本地测试就非常蛋疼了
  11. 网上说有设置项目属性中Maven的profile,查了下eclipse官网说是由于m2eclipse有bug的问题,这个配置已经给禁用了
  12. 那就要另觅他法了,下面介绍一种探索出的一个方法:
  13. 首先在main目录下新建一个profile文件夹,将WEB-INF下面的web.xml复制过来一份
  14. 然后将WEB-INF下面的web.xml中的占位符修改成默认的配置(即没有占位符的,在本地测试用的profile值)
  15. profile文件夹下保留占位符的web.xml配置
  16. 下面是WEB-INF下面没有占位符的正常的配置
  17. 这些占位符我都换成了ysxj
  18. 接下来修改Maven配置
  19. 将配置:
  1. Xml代码  
  1. <plugin>
  2. <groupId>org.apache.maven.plugins</groupId>
  3. <artifactId>maven-war-plugin</artifactId>
  4. <configuration>
  5. <warName>${profiles.activation}</warName>
  6. <!-- 激活spring profile -->
  7. <webResources>
  8. <resource>
  9. <filtering>true</filtering>
  10. <directory>src/main/webapp</directory>
  11. <includes>
  12. <include>**/web.xml</include>
  13. </includes>
  14. </resource>
  15. </webResources>
  16. <warSourceDirectory>src/main/webapp</warSourceDirectory>
  17. <webXml>src/main/webapp/WEB-INF/web.xml</webXml>
  18. </configuration>
  19. </plugin>
  20. 修改为:
  1. Xml代码  
  1. <plugin>
  2. <groupId>org.apache.maven.plugins</groupId>
  3. <artifactId>maven-war-plugin</artifactId>
  4. <configuration>
  5. <warName>${profiles.activation}</warName>
  6. <!-- 激活spring profile -->
  7. <webResources>
  8. <resource>
  9. <filtering>true</filtering>
  10. <!-- 这里是刚刚创建的目录 -->
  11. <directory>src/main/profile</directory>
  12. <!-- 目标目录为WEB-INF -->
  13. <targetPath>WEB-INF</targetPath>
  14. <includes>
  15. <include>**/web.xml</include>
  16. </includes>
  17. </resource>
  18. </webResources>
  19. <warSourceDirectory>src/main/webapp</warSourceDirectory>
  20. <webXml>src/main/webapp/WEB-INF/web.xml</webXml>
  21. </configuration>
  22. </plugin>
  23. 最后见证奇迹
  24. 执行命令
  1. Java代码  
  1. install -P naruto
  2. 在target下面出现根据profile打的war包naruto.war
  3. 接下来证明可以在eclipse的内置tomcat使用
  4. 执行命令
  1. Java代码  
  1. eclipse:eclipse  -Dwtpversion=1.5
  2. 编译成正常的eclipse工程,然后加入tomcat服务器,你会发现能运行成功。
  3. 这里tomcat使用的是源码中WEB-INF下的web.xml,即刚才将占位符修改为ysxj的那个web.xml
  4. 其原理就是在编译阶段将profile文件夹下的web.xml中的占位符替换成Maven中配置的属性,然后覆盖WEB-INF下面的web.xml
  5. 这种方法麻烦的一点就是如果web.xml文件有修改 ,两个记得要同步,一般情况下这个文件也极少动
  6. ps. 骚年们不要手动了,来玩自动的吧

How to activate maven profile inside eclipse的更多相关文章

  1. 在eclipse激活maven profile配置

    profile简介 profile可以让我们定义一系列的配置信息,然后指定其激活条件.这样我们就可以定义多个profile,然后每个profile对应不同的激活条件和配置信息,从而达到不同环境使用不同 ...

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

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

  3. 有关使用Maven常见问题总结(Eclipse中使用Maven、Maven项目部署到tomcat等问题)

    http://blog.csdn.net/sunitjy/article/details/42709311 ********************************************** ...

  4. maven profile实现多环境配置

    每次项目部署上线都需要手动去修改配置文件(比如数据库配置,或者一个自定义的配置)然后才能打包,很麻烦,网上找到 maven profile可以完成这个工作,记录如下: 环境:eclipse + spr ...

  5. CAS (13) —— CAS 使用Maven Profile支持多环境编译

    CAS (13) -- CAS 使用Maven Profile支持多环境编译 摘要 CAS 使用Maven Profile支持多环境编译 版本 tomcat版本: tomcat-8.0.29 jdk版 ...

  6. maven之一:maven安装和eclipse集成

    maven作为一个项目构建工具,在开发的过程中很受欢迎,可以帮助管理项目中的bao依赖问题,另外它的很多功能都极大的减少了开发的难度,下面来介绍maven的安装及与eclipse的集成. maven的 ...

  7. 项目实现不同环境不同配置文件-maven profile

    最近接触的项目都是在很多地方都落地的项目,需要支持不同的环境使用不同的配置文件.一直以来都以为是人工的去写不同的配置文件,手动的去修改运用的配置文件.感觉自己还是太low呀.maven的使用的还停留在 ...

  8. maven环境配置+eclipse环境配置

    一 . (1),下载maven 有点类似于tomcat 解压后就可以用 ,不用安装 (2), 配置环境变量 在系统变量的path 中添加  E:\01Server\maven\bin    注意是bi ...

  9. [Maven] - 安装与Eclipse搭建

    Maven的具体参考书可以看:<Maven实战> 下载maven可以到:http://maven.apache.org/ Maven的eclipse基本使用可以在这里看到:http://w ...

随机推荐

  1. 【笔记】让360浏览器用chrome 内核渲染你的网页

    学校的项目还处在测试阶段 有一个痛点就是有一些页面在360浏览器中默认以ie 内核渲染 这样很不好 以为部分页面因技术方面的不足导致并不能很好地兼容ie 浏览器,于是在网上找了一下答案 可真还有解决方 ...

  2. 【python】下载网络文件到本地

    # 下载网络图片文件到本地 import urllib.request rsp=urllib.request.urlopen("http://n.sinaimg.cn/ent/transfo ...

  3. ASP.NET缺少程序集引用怎么办

    如下所示,提示缺少Windows的引用,在using System.Windows.Forms;有一个下划线 错误代码为: 错误    1    命名空间"System"中不存在类 ...

  4. shell中的字符串操作

    SHELL字符串操作 bash Shell提供了多种字符串处理的命令: awk命令 expr命令 字符串长度 ${#..} expr length awk的length(s) 实例: string=& ...

  5. CAD设置图层透明显示

    在生产制图过程中,图层有利于更好的地理信息的分类,但显示就有些问题了,比如多个面层重叠.通常为了显示多个面重叠区域的特征,我们通常的做法是设置图层填充(符号填充,如地籍处理中经常这样做),或者设置图层 ...

  6. nginx-rtmp流媒体服务器搭建【转】

    nginx-rtmp https://github.com/pengqiuyuan/nginx-rtmp nginx-rtmp 流媒体服务器的搭建(采集桌面,手机直播) 在线Demo,直播自己的pc机 ...

  7. 协议名称处理函数xxxprotoxxx()

    为了方便操作,Linux提供了一组用于查询协议的值及名称的函数. xxxprotoxxx()函数: 上面的函数对文件/etc/protocols中的记录进行操作,文件中记录了协议的名称.值和别名等值, ...

  8. 算法笔记_157:算法提高 c++_ch02_01(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 编写一个程序,利用强制类型转换打印元音字母大小写10种形式的ASCII码. 输出的顺序为:大写的字母A,E,I,O,U的ASCII码,小写的字母a, ...

  9. js 函数定义的2种方式

      js 函数定义的2种方式 CreateTime--2018年3月29日18:36:14 Author:Marydon 方式一: /** * 函数式声明 */ function mode() { c ...

  10. Flume日志收集系统介绍

    转自:http://blog.csdn.net/a2011480169/article/details/51544664 在具体介绍本文内容之前,先给大家看一下Hadoop业务的整体开发流程: 从Ha ...