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,才能部 ...
随机推荐
- Python 3.5安装JPype
使用命令pip install jpype1可安装jpype. 如果出现如下情况: creating build\lib.win-amd64-3.5\jpypex copying jpypex\__i ...
- ubuntu 配置nginx+php+mysql 遇到的一些问题
/* 公司内网打算配置一台ubuntu为主机的测试服务器.刚好手头有一个昂达的主机,装的windows 声音又大,还不如直接装ubuntu .声音又小,还占用资源少. */ 刚开始安装php5 结果提 ...
- SEO优化小技巧
/** * seo优化课程 * 先谢慕课网 */ /** * SEO基本介绍 * SEO与前端工程师 */ /** * SEO基本介绍 * 搜索引擎工作原理:输入关键字------查询------显示 ...
- RedHat6安装gcc
RedHat RHEL 6.0安装gcc的方法 最近在折腾RedHat6.0 ,全是命令,号称比windows安全的Linux,没有界面,也不知道所谓的专家们如何比的,一个界面做的非常好的Window ...
- BZOJ2038: [2009国家集训队]小Z的袜子(hose)
Time Limit: 20 Sec Memory Limit: 259 MB Description 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿.终于有一天, ...
- ubuntu用apt安装apache2时,出现E:未发现软件包 apache2
解决方法:使用sudo apt-get update更新软件包 更新软件包失败,多半使用因为源文件不干净,在/etc/apt下重新自己新写一份源文件 然后执行 sudo apt-get update
- SOA架构介绍和理解
SOA架构介绍和理解 SOA的正确方法论及目标模型,其实SOA在实现架构落地上,需要考虑到对服务的组合,不断的重用现有的服务,让企业应用可以逐步集成,快速实现业务的迭代. 通过SOA架构分层将服务按照 ...
- fedora 24下修改IP
在ROOT环境下 cd /etc/sysconfig/network-scripts 找到类似 ifcfg-enp1s0的文件 sudo vi ifcfg-enp1s0 HWADDR=XX:XX:X ...
- 图解直方图均衡化及其Python实现
在理解直方图均衡化的过程中,参考了一些书籍和博客,让人困惑的是,笔者对于直方图的理解还是停留在表面,并没有深入理解其内涵.因此,本文拟结合图片对直方图的概念进行阐述,并给出其Python实现,最后对她 ...
- 驱动开发学习笔记. 0.04 linux 2.6 platform device register 平台设备注册 1/2 共2篇
驱动开发读书笔记. 0.04 linux 2.6 platform device register 平台设备注册 1/2 共2篇下面这段摘自 linux源码里面的文档 : Documentatio ...