由于项目的需要, 今天给spirng boot项目添加了profile功能。再网上搜索了一圈,也没有找到满意的参考资料,其实配置并不难,就是没有一个one stop(一站式)讲解的地方,所以有了写这篇博客的想法。由于本人水平有限,文中任何错误和纰漏欢迎大家反馈。希望本文可以给你带来帮助。

本文实现的目标:

1 使用了maven的profile功能

2 使用了Spring Boot的profile功能

3 集成了1和2的功能

4 在eclipse中运行mvn工程,使用开发环境的profile。

5 通过mvn在命令行中打包时,可以指定相应的profile。

什么是profile,解决什么问题呢?举个例子。一般在开发项目的时候要有多个环境,如开发环境、测试环境、生产环境,他们的配置文件一般不同。当我们要向各个环境发布程序时,需要人工处理这些配置文件,这显然麻烦且易错。有了profile,一切问题就简单了。只要在maven打包时使用下面命令即可。

  1. mvn clean package -Dmaven.test.skip=true -P prod

解释一下, -P prod 就是告诉maven要使用名字为prod的profile来打包,即所有的配置文件都使用生产环境(prod是自己定义的,在这里自定义为生产环境)。

实现思路简述:

maven支持profile功能,当使用maven profile打包时,可以打包指定目录和指定文件,且可以修改文件中的变量。spring boot也支持profile功能,只要在application.properties文件中指定spring.profiles.active=xxx 即可,其中xxx是一个变量,当maven打包时,修改这个变量即可。

具体实现:

A段,介绍coolpro工程的配置。

这个工程只需要修改pom.xml文件即可,需要定义具体maven的profile。定义完毕之后,当我们使用mvn clean package -P dev 时,maven就知道了profileActive=dev这个属性生效了。其中profileActive可以自己定义,就是一个maven的自定义属性。

pom.xml文件如下:

B段,介绍coolpro-api工程的配置

这个工程是一个web工程,主要是想根据指定的profile配置相应的spring boot运行环境。如:如果profile是dev,配置web服务器的监听端口为8010;profile为test,则端口为8020;profile为prod,则端口为8030。

工程中有4个文件:

application.properties, 包含通用配置的文件。文件中有spring.profiles.active=@profileActive@的属性。spring boot的profile是通过spring.profiles.active属性来配置的,这里的profileActive和上面coolpro工程中配置的profileActive属性名要一致。这样,在运行mvn命令时,maven就会帮我们将@profileActive@替换成指定的profile。

application-dev.properties, 当mvn -P dev时, 需要打包这个文件。

application-test.properties, 当mvn -P test时, 需要打包这个文件。

application-prod.properties, 当mvn -P prod时, 需要打包这个文件。

pom.xml配置:

application.properties文件:

其他3个文件见截图:

C段,介绍coolpro-core工程的配置

配置完成了。看效果。

1 在Eclipse环境中,直接运行项目。

2 使用maven命令,打包这个应用。

1)以开发环境打包:mvn clean package -Dmaven.test.skip=true -P dev -e

结果如下:

查看api工程:

查看core工程:

以此类推, 可以运行

mvn clean package -Dmaven.test.skip=true -P test -e

mvn clean package -Dmaven.test.skip=true -P prod -e

注意两点:

1 在属性文件中替换变量时,使用@符合。

最开始,使用$符号,就是不能替换,在网上搜索一下,原来是maven的maven-resources-plugin可以定义这个替换的符号。通过eclipse提供的Maven Pom editor编辑器打开pom.xml文件,在“Effective POM”选项卡中,可以看到定义的是@这个符号。

2 怎么在启动spring boot应用时,打印正在使用的profile,避免配置错误呢?可以参考如下代码。

  1. public static void main(String[] args) {
  2. ApplicationContext ctx = SpringApplication.run(RestApiApplication.class, args);
  3. String[] activeProfiles = ctx.getEnvironment().getActiveProfiles();
  4. for (String profile : activeProfiles) {
  5. logger.warn("Spring Boot 使用profile为:{}" , profile);
  6. }
  7. }

参考:http://docs.spring.io/spring-boot/docs/current/reference/html/

http://www.tuicool.com/articles/NjqAF3

http://blog.csdn.net/jbgtwang/article/details/8642979

http://zilongsky-gmail-com.iteye.com/blog/2032001

http://www.huangyunkun.com/2015/01/01/run-code-after-spring-boot-started/

http://blog.csdn.net/lihe2008125/article/details/50443491

[Spring Boot 系列] 集成maven和Spring boot的profile功能的更多相关文章

  1. [Spring Boot 系列] 集成maven和Spring boot的profile 专题

    maven中配置profile节点: <project> .... <profiles> <profile> <!-- 生产环境 --> <id& ...

  2. Spring Boot2 系列教程(二十)Spring Boot 整合JdbcTemplate 多数据源

    多数据源配置也算是一个常见的开发需求,Spring 和 SpringBoot 中,对此都有相应的解决方案,不过一般来说,如果有多数据源的需求,我还是建议首选分布式数据库中间件 MyCat 去解决相关问 ...

  3. spring boot系列(五)spring boot 配置spring data jpa (查询方法)

    接着上面spring boot系列(四)spring boot 配置spring data jpa 保存修改方法继续做查询的测试: 1 创建UserInfo实体类,代码和https://www.cnb ...

  4. 集成maven和Spring boot的profile 专题

    maven中配置profile节点: <project> .... <profiles> <profile> <!-- 生产环境 --> <id& ...

  5. 集成maven和Spring boot的profile

    如果在配置中勾选了多套配置,则以pom.xml文件中 profiles中  配置 最后一个配置为准. maven中配置profile节点: <project> .... <profi ...

  6. 集成maven和Spring boot的profile功能

    思路:maven支持profile功能,当使用maven profile打包时,可以打包指定目录和指定文件,且可以修改文件中的变量.spring boot也支持profile功能,只要在applica ...

  7. spring boot系列01--快速构建spring boot项目

    最近的项目用spring boot 框架 借此学习了一下 这里做一下总结记录 非常便利的一个框架 它的优缺点我就不在这背书了 想了解的可以自行度娘谷歌 说一下要写什么吧 其实还真不是很清楚,只是想记录 ...

  8. Spring Boot系列(一) Spring Boot介绍和基础POM文件

    Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置.通过 ...

  9. Spring Boot2 系列教程(三十)Spring Boot 整合 Ehcache

    用惯了 Redis ,很多人已经忘记了还有另一个缓存方案 Ehcache ,是的,在 Redis 一统江湖的时代,Ehcache 渐渐有点没落了,不过,我们还是有必要了解下 Ehcache ,在有的场 ...

随机推荐

  1. Netsharp快速入门(之15) 销售管理(报表B 销售季度表)

    作者:秋时 杨昶   转载须说明出处 4.6.2  销售季度表(交叉表) 1.1.1.1 交叉表带数据源和不带数据源区别 带数据源的可以自定义数据源,可以从实体,也可以从Sql脚本得到数据源,并能自定 ...

  2. PCA算法

    本文出处:http://blog.csdn.net/xizhibei http://www.cnblogs.com/bourneli/p/3624073.html PrincipalComponent ...

  3. UVALive - 6952 Cent Savings dp

    题目链接: http://acm.hust.edu.cn/vjudge/problem/116998 Cent Savings Time Limit: 3000MS 问题描述 To host a re ...

  4. 【转】Basic C# OOP Concept

    This Article will explain a very simple way to understand the basic C# OOP Concept Download ShanuBas ...

  5. Oracle 11g安装与使用

    作为一个新手,学习Oracle,就连安装oracle都感觉到吃力! 经过不间断的搜罗.学习.尝试,找到一些比较有用的“指导”,罗列如下: 1. http://www.2cto.com/database ...

  6. ios UI实现技巧

    statusBar 移动位置 NSString *key = [[NSString alloc] initWithData:[NSData dataWithBytes:(unsigned ] enco ...

  7. 了解javascript中的事件(二)

    本文目录如下: 零.寒暄 一.事件的分类 二.事件代理 2.1 问题引出 2.2 什么是事件代理 2.3 完整示例 二.事件代理 三.事件代理思想的用处 四.总结 零.寒暄 这篇博客本该出现在两个月以 ...

  8. Notes on the Dirichlet Distribution and Dirichlet Process

    Notes on the Dirichlet Distribution and Dirichlet Process In [3]: %matplotlib inline   Note: I wrote ...

  9. DevExpress12.2.4 GridControl相关技巧

    1.DevExpress12.2.4中,设置GridControl的GridView为可编辑方法如下: gvMainControl.OptionsBehavior.Editable = true; 2 ...

  10. Performance tips

    HTML5 Techniques for Optimizing Mobile Performance Scrolling Performance layout-performance