Maven 整合 spring profile实现多环境自动切换
Maven 整合 spring profile实现多环境自动切换
|
profile主要用在项目多环境运行的情况下,比如开发环境、测试环境、线上生产环境。 <beans profile="xmj_old">
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://127.0.0.1:3306/db1?characterEncoding=utf8" />
<property name="password" value="123456" />
<property name="username" value="abc" />
</bean>
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames">
<list>
<value>classpath:messages_zh_CN</value>
<value>classpath:messages/messages_en_US-xmj_old</value>
</list>
</property>
</bean>
</beans>
<beans profile="xmj_new">
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://127.0.0.1:3306/db1?characterEncoding=utf8" />
<property name="password" value="123456" />
<property name="username" value="abc" />
</bean>
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames">
<list>
<value>classpath:messages_zh_CN</value>
<value>classpath:messages/messages_en_US-xmj_new</value>
</list>
</property>
</bean>
</beans>
...
...
...
这里的message_en_US-*系列属性文件是配置站点的名称和黑名单的位置: resource.blacklist.dir=/var/resource/blacklist.txt
system.title.id=system.xmj_old.title
激活spring的profile <context-param>
<param-name>spring.profiles.active</param-name>
<param-value>xmj_old</param-value>
</context-param>
这样就激活spring配置文件中的profile为xmj_old的配置,但这样手动配置依然要改web.xml配置。 <profiles>
<profile>
<id>xmj_old</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<profiles.activation>xmj_old</profiles.activation>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
<version>1.1</version>
<configuration>
<!-- 配置项目自动发布服务器 -->
<url>http://xx.xx.xx.xx:8080/manager/text</url>
<path>/xmj-manager</path>
<server>Tomcat</server>
<warFile>target/${profiles.activation}.war</warFile>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>xmj_new</id>
<properties>
<profiles.activation>xmj_new</profiles.activation>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
<version>1.1</version>
<configuration>
<!-- 配置项目自动发布服务器 -->
<url>http://xx2.xx.xx.xx:8080/manager/text</url>
<path>/xmj-manager</path>
<server>Tomcat</server>
<warFile>target/${profiles.activation}.war</warFile>
</configuration>
</plugin>
</plugins>
</build>
</profile>
...
...
...
</profiles>
activeByDefault标签配置true表示默认激活的环境,直接install就是使用此配置的环境。 <context-param>
<param-name>spring.profiles.active</param-name>
<param-value>${profiles.activation}</param-value>
</context-param>
这还不算完,还要配置Maven的一个插件: <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<warName>${profiles.activation}</warName>
<!-- 激活spring profile -->
<webResources>
<resource>
<filtering>true</filtering>
<directory>src/main/webapp</directory>
<includes>
<include>**/web.xml</include>
</includes>
</resource>
</webResources>
<warSourceDirectory>src/main/webapp</warSourceDirectory>
<webXml>src/main/webapp/WEB-INF/web.xml</webXml>
</configuration>
</plugin>
打包的时候Maven会去自动修改web.xml文件,根据占位符自动替换为对应的属性值 clean install
前面配置的默认环境是xmj_old这个,在执行完命令之后target文件夹下面出现了一个名为xmj_old.war的war包 clean install -P xmj_new
-P 参数后面加上profile的id即可完成自动切换,执行完之后看下是否已经自动切换到id为xmj_new的profile环境下 <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<warName>${profiles.activation}</warName>
<!-- 激活spring profile -->
<webResources>
<resource>
<filtering>true</filtering>
<directory>src/main/webapp</directory>
<includes>
<include>**/web.xml</include>
</includes>
</resource>
</webResources>
<warSourceDirectory>src/main/webapp</warSourceDirectory>
<webXml>src/main/webapp/WEB-INF/web.xml</webXml>
</configuration>
</plugin>
修改为: <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<warName>${profiles.activation}</warName>
<!-- 激活spring profile -->
<webResources>
<resource>
<filtering>true</filtering>
<!-- 这里是刚刚创建的目录 -->
<directory>src/main/profile</directory>
<!-- 目标目录为WEB-INF -->
<targetPath>WEB-INF</targetPath>
<includes>
<include>**/web.xml</include>
</includes>
</resource>
</webResources>
<warSourceDirectory>src/main/webapp</warSourceDirectory>
<webXml>src/main/webapp/WEB-INF/web.xml</webXml>
</configuration>
</plugin>
最后见证奇迹 install -P naruto
在target下面出现根据profile打的war包naruto.war eclipse:eclipse -Dwtpversion=1.5
编译成正常的eclipse工程,然后加入tomcat服务器,你会发现能运行成功。 |
Maven 整合 spring profile实现多环境自动切换的更多相关文章
- 使用maven整合spring+springmvc+mybatis
使用maven整合spring+springmvc+mybatis 开发环境: 1. jdk1.8 2. eclipse4.7.0 (Oxygen) 3. mysql 5.7 在pom.xml文件中, ...
- springboot+maven整合spring security
springboot+maven整合spring security已经做了两次了,然而还是不太熟悉,这里针对后台简单记录一下需要做哪些事情,具体的步骤怎么操作网上都有,不再赘述.1.pom.xml中添 ...
- Maven整合Spring与Solr
首先,在maven的pom.xml文件中配置对spring和solrj客户端的依赖: <project xmlns="http://maven.apache.org/POM/4.0.0 ...
- Maven Filter与Profile隔离生产环境与开发环境
Maven Filter与Profile隔离生产环境与开发环境 在不同的开发阶段,我们一般用到不同的环境,开发阶段使用开发环境的一套东西,测试环境使用测试环境的东西,可能有多个测试环境,生产环境使用的 ...
- (转载)maven profile多环境自动切换配置
原文:https://www.cnblogs.com/adeng/p/7059588.html 痛点: 在java开发的过程中,我们经常要面对各种各样的环境,比如开发环境,测试环境,正式环境,而这些环 ...
- maven学习利用Profile构建不同环境的部署包
项目开发好以后,通常要在多个环境部署,象我们公司多达5种环境:本机环境(local).(开发小组内自测的)开发环境(dev).(提供给测试团队的)测试环境(test).预发布环境(pre).正式生产环 ...
- spring Profile 为不同环境提供不同的配置支持
说明 Profile为在不同环境下使用不同的配置提供了支持(开发环境下的配置和生产环境下的配置肯定是不同的, 例如, 数据库的配置) . 在spring开发中用@Profile 注解使用来选择行配置系 ...
- Maven + Spring 进行多环境自动切换功能
在pom.xml的<project></project>的最下放写入如下代码: <!-- profiles setting start [mvn install -P x ...
- maven profile多环境自动切换配置,配置分离,排除文件
痛点: 在java开发的过程中,我们经常要面对各种各样的环境,比如开发环境,测试环境,正式环境,而这些环境对项目的需求也不相同. 在此之前,我们往往需要手动去修改相对应的配置文件然后打成war,才能部 ...
随机推荐
- 实数---Currency讲解
Currency 实际上是 Int64 的变体,Int64/10000 就是实际的值
- Hibernate一对一外键双向关联(Annotation配置)
如上图所示:一个学生有一个学生证号,一个学生证号对应一名学生.在Hibernate中怎么用Annotation来实现呢? 学生类,主键是id:学生证的主键也是Id: Student.java pack ...
- cocos2d-js去掉左下角的三行数字(帧数)
project.json 里面"showFPS": true, 改成 false 就行了... 调整帧率也在这里调整 或者是 同cocos2dx,cocos2d-js左下角的FPS ...
- jquery实现章节目录效果
<html><head><title>jquery实现章节目录效果</title> <script type="text/javascr ...
- HTML DOM Element
HTML DOM Element(元素) 文档本身是文档节点 . 所有 HTML 元素是元素节点 . 所有 HTML 属性是属性节点 HTML 元素内的文本是文本节点 . 注释是注释节点 . NODE ...
- Javascript--装饰器模式和观察者模式
装饰器模式 (function(){//装饰一棵树,装饰器模式通过对对象的属性进行改变来装饰对象.需要一个设置属性的方法 var tree={}; tree.decorate=function(){ ...
- Unix调试工具dbx使用方法
dbx 命令 用途 提供了一个调试和运行程序的环境. 语法 dbx [ -a ProcessID ] [ -c CommandFile ] [ -d NestingDepth ] [ -I Dire ...
- Acadia Lab 6 轮盘游戏机
WRTnode 肯定不是亲生的... 果断转投Acadia —.— 不是国军不给力,奈何共军有高达 为啥不转树莓派?因为选做实验肯定有很多人用树莓派做...我抢不过他们,只能挑点冷门的蹭分_(:з」 ...
- vim+slimv+sbcl搭建lisp的IDE
流水账而已,分享给需要的人. slimv的首页 http://kovisoft.bitbucket.org/tutorial.html里面介绍说slimv开箱即可用,可是老天总会给一些奇怪的问题给你, ...
- 【转】NVelocity模板引擎初学总结
转自:http://sunxitao88.blog.163.com/blog/static/68314439200861963326251/ 前不久,接触到.NET下的MVC-MonoRail,它推荐 ...