maven不同环境的profile配置
1.开发的时候经常需要加载不同的环境,比如本地开发环境dev,生产环境product。如果需要手动去修改的话就太麻烦了,自己实现了maven资源替换,然后多环境下的配置文件管理的demo,在此贴出来。
2.实现需求:
根据本地or开发配置文件,加载不同的配置,如果使用本地数据库demodb,zhangsan,123456才能登录成功;
如果使用生产环境数据库productdb,wangwu,123455才能登录成功;
代码如下:
1.父项目myssm-pom下两套配置,dev.properties为本地开发配置,product.properties为生产环境配置;
dev.properties:
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/demodb
username=root
password=root
product.properties:
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/productdb
username=root
password=root
2.myssm-pom.xml配置:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.cy.myssm</groupId>
<artifactId>myssm-pom</artifactId>
<version>1.0.0</version>
<modules>
<module>../myssm-web</module>
<module>../myssm-service</module>
<module>../myssm-dao</module>
<module>../myssm-support</module>
</modules>
<packaging>pom</packaging> <!-- 环境配置 -->
<profiles>
<!-- 本地环境 -->
<profile>
<id>dev</id>
<activation>
<!-- 默认激活dev -->
<activeByDefault>true</activeByDefault>
</activation>
<build>
<!-- 指定当前profile环境下,属性文件路径 -->
<filters>
<filter>../${project.parent.artifactId}/properties/dev.properties</filter>
</filters>
</build>
</profile>
<!-- 生产环境 -->
<profile>
<id>product</id>
<build>
<filters>
<filter>../${project.parent.artifactId}/properties/product.properties</filter>
</filters>
</build>
</profile>
</profiles> <build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<useDefaultDelimiters>false</useDefaultDelimiters>
<delimiters>
<delimiter>$[*]</delimiter>
</delimiters>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<!-- 要替换属性的文件目录 -->
<directory>${basedir}/src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build> </project>
说明:
1) profile下面标签相关概念:
id:profile的标示
activeByDefault: 我们可以在profile中的activation元素中指定激活条件,当没有指定条件,然后指定activeByDefault为true的时候就表示当没有指定其他profile为激活状态时,该 profile就默认会被激活。所以当我们调用mvn package的时候上面的dev将会被激活,但是当我们使用mvn package –P product的时候将激活product,而这个时候 dev将不会被激活。
filters:比较重要,指定当前profile环境下,属性文件路径;
2)resouces标签下面属性:
directory: 配置那个目录下的文件通过${key} (el表达式,默认的,但是我这里改成$[]了)会被替换成属性值,resource目录下,我们一般放jdbc连接,或配置文件;
includes: 指定那个目录下那个文件
filtering: 这个配置的意思是过滤上面指定属性文件中的占位符,占位符是${变量名称}这样的形式,maven会自动读取配置文件,然后解析其中的占位符,使用上面pom文件中定义 的属性进行替换
exclueds: 在resource目录下,有很多文件,但用些文件不希望替换,则可以通过<excluede>指定
filters: 这里的filters与<profile>的filter意思一样,都是指属性文件地址,这个如果上面定义<profile>的时候指定了,这里就不需要了,但有些开发习惯是在<profile>不定义,然后在 <build>里指定。
3) maven-resources-plugin
useDefaultDelimiters不使用默认的分割,默认的是将${}即el表达式替换掉;我这里将所有basedir下面的src/main/resources下面的文件中,只要含有$[key]的表达式替换成myssm-pom下面已经配置好的开发/生产配置;
4) datasource.properties:
driverClassName=$[driverClassName]
url=$[url]
username=$[username]
password=$[password]
3.sprint-context.xm配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> <!-- 自动扫描 -->
<context:component-scan base-package="com.cy" />
<import resource="spring-mq.xml"/>
<mvc:annotation-driven/> <!-- 属性文件加载 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:properties/datasource.properties</value>
</list>
</property>
<property name="placeholderPrefix" value="@{" />
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_NEVER" />
</bean> <!-- 配置数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="@{driverClassName}"/>
<property name="url" value="@{url}"/>
<property name="username" value="@{username}"/>
<property name="password" value="@{password}"/>
</bean> <!-- 配置mybatis的sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自动扫描mappers.xml文件 -->
<property name="mapperLocations" value="classpath:mappers/*.xml"></property>
<!-- mybatis配置文件 -->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean> <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.cy.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean> <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean> </beans>
为了方便,将整段配置贴下来了;
说明:
1).使用PropertyPlaceholderConfigurer加载属性配置文件datasource.properties,但是这里使用前缀@{来引用属性配置,而默认是${}即el表达式;
2).systemPropertiesModeName的选择有三种:
SYSTEM_PROPERTIES_MODE_FALLBACK: 如果给定的配置文件没有该属性,检查系统配置文件;
SYSTEM_PROPERTIES_MODE_NEVER: 从不检查系统属性
SYSTEM_PROPERTIES_MODE_OVERRIDE: 先检查系统配置文件,如果没有该属性,再检查给定的配置文件;
3).现在就可以使用@{key}来引用datasource.properties中的属性配置啦;比如上面dataSource中的driverClassName,url等;
4.测试代码:
package com.cy.controller; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import com.cy.entity.User;
import com.cy.service.UserService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
@RequestMapping("/user")
public class UserController { private static Logger logger = Logger.getLogger(UserController.class); @Autowired
private UserService userService; @Value("@{driverClassName}")
private String driverClassName; @Value("@{url}")
private String url; @Value("@{username}")
private String username; @Value("@{password}")
private String password; /**
* 用户登录
* @param user
* @param request
* @return
*/
@RequestMapping("/login")
public String login(User user, HttpServletRequest request){
logger.info("login入参:username:"+user.getUsername()+",passowrd:"+user.getPassword()); User resultUser=userService.login(user);
if(resultUser==null){
request.setAttribute("user", user);
request.setAttribute("errorMsg", "用户名或密码错误!");
return "index";
}else{
System.err.println("username:" +user.getUsername() +"登录成功!");
HttpSession session=request.getSession();
session.setAttribute("currentUser", resultUser);
return "redirect:/success.jsp";
}
} /**
* 测试@Value注解 注入的值是否生效
*/
@RequestMapping("/testAnnotationValue")
public void testAnnotationValue(){
logger.info("driverClassName:" +driverClassName);
logger.info("url:" +url);
logger.info("username:" +username);
logger.info("password:" +password);
}
}
1)idea中maven projects工具栏中,如果使用默认dev profile,启动项目
zhangsan能登录,console打印;
2018-08-23 20:50:19,237 [http-bio-8888-exec-3] INFO [com.cy.controller.UserController] - driverClassName:com.mysql.jdbc.Driver
2018-08-23 20:50:19,237 [http-bio-8888-exec-3] INFO [com.cy.controller.UserController] - url:jdbc:mysql://localhost:3306/demodb
2018-08-23 20:50:19,237 [http-bio-8888-exec-3] INFO [com.cy.controller.UserController] - username:root
2018-08-23 20:50:19,237 [http-bio-8888-exec-3] INFO [com.cy.controller.UserController] - password:root
2) 如果勾选product,maven就使用product的配置, 这时候使用的是productdb,只有wangwu才能登录了。
maven不同环境的profile配置的更多相关文章
- Maven安装与全局profile配置
Maven 3.2 需要 JDK 1.6, Maven 3.0/3.1 需要 JDK 1.5 · 解压. · 环境变量 M2_HOME · M2 = %M2_HOME%\bin 同时也添加到PATH ...
- Maven的环境搭建及新建web项目
第一次接触maven,做一个简单的记录 一.下载maven及环境变量的配置 下载地址 http://maven.apache.org/download.cgi 配置其环境变量 MAVEN_HOME= ...
- (转载)maven profile多环境自动切换配置
原文:https://www.cnblogs.com/adeng/p/7059588.html 痛点: 在java开发的过程中,我们经常要面对各种各样的环境,比如开发环境,测试环境,正式环境,而这些环 ...
- Maven项目中通过profile定义使不同环境使用不同配置信息
profile可以让我们定义一系列的配置信息,然后指定其激活条件.这样我们就可以定义多个profile,然后每个profile对应不同的激活条件和配置信息,从而达到不同环境使用不同配置信息的效果.比如 ...
- Eclipse+Maven整合开发Java项目(一)➣Maven基础环境配置
概述 Maven是一个Java语言编写的开源项目管理工具,是Apache软件基金会的顶级项目.主要用于项目构建,依赖管理,项目信息管理.有些项目需要添加响应的依赖包,Maven就是公用包集合.存在远程 ...
- 在eclipse激活maven profile配置
profile简介 profile可以让我们定义一系列的配置信息,然后指定其激活条件.这样我们就可以定义多个profile,然后每个profile对应不同的激活条件和配置信息,从而达到不同环境使用不同 ...
- SpringBoot系列四:SpringBoot开发(改变环境属性、读取资源文件、Bean 配置、模版渲染、profile 配置)
声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅. 1.概念 SpringBoot 开发深入 2.具体内容 在之前已经基本上了解了整个 SpringBoot 运行机制,但是也需要清 ...
- 架构实战项目心得(三):JAVA和MAVEN的环境配置
1 java环境配置: 1 下载并安装jdk1.82 配置java环境变量: vi /etc/profile,在文件底部增加以下内容:export JAVA_HOME=/data/program/so ...
- SpringBoot开发(改变环境属性、读取资源文件、Bean 配置、模版渲染、profile 配置)
1.概念 SpringBoot 开发深入 2.具体内容 在之前已经基本上了解了整个 SpringBoot 运行机制,但是也需要清楚的认识到以下的问题,在实际的项目开发之中,尤其是 Java 的 MVC ...
随机推荐
- (8)Python连接操作MySQL
pymysql模块下的方法 '''必须实例化对象才能建立连接''' 1.pymysql.connect #和MySQL建立连接 '''得由对象去调用定义游标''' 2.xxx.sursor() # ...
- C++学习(二十二)(C语言部分)之 项目扫雷实例
一.新建项目 二.新建源文件 main.cpp和mining,cpp 三.新建头文件 mining.h 四.图片资源添加 添加完成后会在头文件里面生成一个.h头文件,用来调用资源 打开之后可以看到,对 ...
- java知识整理
整理一下Java知识点. 一.final finally finalize区别 1.final 修饰符(关键字).被final修饰的类,不能再派生出新的子类,不能作为父类而被子类继承.因此一个类不能既 ...
- eventEmitter
wade-mac:fin_server_invest mac$ node > var events =require('events') undefined > var eventEmit ...
- spring 自己定义标签 学习二
在上篇中写的仅仅支持写属性,不支持标签property的写法,可是假设有时候我们还想做成支持 property的使用方法,则能够在xsd中添加spring 自带的xsd引用 改动xsd文件例如以下 ...
- 深入详解美团点评CAT跨语言服务监控(四)服务端消息分发
这边首先介绍下大众点评CAT消息分发大概的架构如下: 图4 消息分发架构图 分析管理器的初始化 我们在第一章讲到服务器将接收到的消息交给解码器(MessageDecoder)去做解码最后交给具体的消费 ...
- c++内存泄漏原因及解决办法(智能指针)
内存泄漏 由于疏忽或错误造成程序未能释放已经不再使用的内存的情况.内存泄漏并非指内存在物理上的消失,而是应用程序分配某段内存后,由于设计错误,失去了对该段内存的控制,因而造成了内存的浪费. 内存泄露的 ...
- Cobbler自动装机--2
自动重装工具--koan 客户机已经通过cobbler安装centos7系统完毕. 安装koan,能实现重装,安装之前先安装epel源 koan是kickstart-over-a-network的缩 ...
- Redis Cluster 4.0高可用集群安装、在线迁移操作记录
之前介绍了redis cluster的结构及高可用集群部署过程,今天这里简单说下redis集群的迁移.由于之前的redis cluster集群环境部署的服务器性能有限,需要迁移到高配置的服务器上.考虑 ...
- git push文件到远程github或者gitlab
Git global setup git config --global user.name "luozeng" git config --global user.email &q ...