参考地址:https://blog.csdn.net/lihe2008125/article/details/50443491

一、主要目标

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

  2、需使用了maven的profile功能

  3、使用了Spring Boot的profile功能

二、profile简介

  什么是profile,解决什么问题呢?  

  一般在开发项目的时候要有多个环境,如开发环境、测试环境、生产环境,配置文件一般不同。当要向各个环境发布程序时,需要人工处理这些配置文件。有了profile,只要在maven打包时使用下面命令即可。

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

  -P prod 就是告诉maven要使用名字为prod的profile来打包,即所有的配置文件都使用生产环境(prod:生产;test:测试;dev:开发)。

三、实现过程

  maven支持profile功能,当使用maven profile打包时,可以打包指定目录和指定文件,且可以修改文件中的变量。

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

3.1、父pom增加profile配置

  在maven的父pom中增加如下配置 :
  1. <profiles>
  2. <profile>
  3. <id>dev</id>
  4. <properties>
  5. <profileActive>dev</profileActive>
  6. </properties>
  7. <activation>
  8. <activeByDefault>true</activeByDefault>
  9. </activation>
  10. </profile>
  11. <profile>
  12. <id>test</id>
  13. <properties>
  14. <profileActive>test</profileActive>
  15. </properties>
  16. </profile>
  17. <profile>
  18. <id>prod</id>
  19. <properties>
  20. <profileActive>properties</profileActive>
  21. </properties>
  22. </profile>
  23. </profiles>

方式一、基本打包

测试环境:springboot 1.5.1下

在build中配置,下文 中会有详细解释

  1. <resources>
  2. <resource>
  3. <directory>src/main/resources</directory>
  4. <filtering>true</filtering>
  5. <excludes>
  6. <exclude>application-dev.properties</exclude>
  7. <exclude>application-test.properties</exclude>
  8. <exclude>application-produce.properties</exclude>
  9. </excludes>
  10. </resource>
  11. <resource>
  12. <directory>src/main/resources</directory>
  13. <filtering>true</filtering>
  14. <includes>
  15. <include>application.properties</include>
  16. <include>application-${profiles.active}.properties</include>
  17. <include>**/*.xml</include>
  18. </includes>
  19. </resource>
  20. <resource>
  21. <directory>src/main/java</directory>
  22. <includes>
  23. <include>**/*.xml</include>
  24. </includes>
  25. <filtering>false</filtering>
  26. </resource>
  27. </resources>

因可能版本较低,需要配置 maven-resources-plugin,否则变量不替换,但是更换了springboot 2.0.4后不用配置既可以

  1. <plugins>
  2. <plugin>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-maven-plugin</artifactId>
  5. </plugin>
  6. <plugin>
  7. <groupId>org.apache.maven.plugins</groupId>
  8. <artifactId>maven-resources-plugin</artifactId>
  9. <configuration>
  10. <delimiters><!-- 使用${..}作为占位符 -->
  11. <delimiter>${*}</delimiter>
  12. </delimiters>
  13. <!-- 使用默认的占位符(@..@) -->
  14. <useDefaultDelimiters>true</useDefaultDelimiters>
  15. </configuration>
  16. </plugin>
  17. </plugins>

方式二、自定义配置的打包

1、配置好:assembly/depolyment.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
  4. <id>dist</id>
  5. <formats>
  6. <format>zip</format>
  7. <format>dir</format>
  8. </formats>
  9. <includeBaseDirectory>false</includeBaseDirectory>
  10.  
  11. <fileSets>
  12. <fileSet>
  13. <directory>src/main/bin</directory>
  14. <outputDirectory>bin/</outputDirectory>
  15. </fileSet>
  16. <!--<fileSet>-->
  17. <!--<directory>src/main/resources</directory>-->
  18. <!--<outputDirectory>/</outputDirectory>-->
  19. <!--</fileSet>-->
  20.  
  21. <fileSet>
  22. <directory>${project.build.directory}</directory>
  23. <outputDirectory>/</outputDirectory>
  24. <includes>
  25. <include>*.jar</include>
  26. </includes>
  27. </fileSet>
  28. </fileSets>
  29. <dependencySets>
  30. <dependencySet>
  31. <outputDirectory>lib</outputDirectory>
  32. <scope>runtime</scope>
  33. <excludes>
  34. <exclude>${groupId}:${artifactId}</exclude>
  35. </excludes>
  36. </dependencySet>
  37. </dependencySets>
  38. </assembly>

2、多环境配置

方式2.1、文件后缀方式

  在需要打包的子项目pom设置

  1. <build>
  2. <resources>
  3. <resource>
  4. <!--指定打包时需要特殊处理的目录文件-->
  5. <directory>src/main/resources</directory>
  6. <!--处理文件是时,需要变量替换-->
  7. <filtering>true</filtering>
  8. <!--打包时,排除文件-->
  9. <excludes>
  10. <exclude>application.properties</exclude>
  11. <exclude>application-dev.properties</exclude>
  12. <exclude>application-test.properties</exclude>
  13. <exclude>application-prod.properties</exclude>
  14. </excludes>
  15. </resource>
  16. <resource>
  17. <!--指定打包时需要特殊处理的目录文件-->
  18. <directory>src/main/resources</directory>
  19. <!--处理文件是时,需要变量替换-->
  20. <filtering>true</filtering>
  21. <!--打包时,包含文件-->
  22. <includes>
  23. <include>application.properties</include>
  24. <include>application-${profileActive}.properties</include>
  25. </includes>
  26. </resource>
  27. </resources>
  28. <plugins>
  29. <plugin>
  30. <groupId>org.apache.maven.plugins</groupId>
  31. <artifactId>maven-compiler-plugin</artifactId>
  32. <configuration>
  33. <source>${java.version}</source>
  34. <target>${java.version}</target>
  35. </configuration>
  36. </plugin>
  37. <!--打jar包-->
  38. <plugin>
  39. <groupId>org.apache.maven.plugins</groupId>
  40. <artifactId>maven-jar-plugin</artifactId>
  41. <version>2.6</version>
  42. <configuration>
  43. <archive>
  44. <manifest>
  45. <!--jar入口累-->
  46. <mainClass>com.jd.bt.gateway.ZuulApplication</mainClass>
  47. <!-- classpath路径 -->
  48. <addClasspath>true</addClasspath>
  49. <classpathPrefix>lib/</classpathPrefix>
  50. </manifest>
  51. <manifestEntries>
  52. <Class-Path>./</Class-Path>
  53. </manifestEntries>
  54. </archive>
  55. <excludes>
  56. <!--注意从编译结果目录开始算目录结构-->
  57. <!--<exclude>/*.yml</exclude>-->
  58. <!--<exclude>/*.properties</exclude>-->
  59. <!--<exclude>/*.xml</exclude>-->
  60. <!--<exclude>/*.txt</exclude>-->
  61. </excludes>
  62. </configuration>
  63. </plugin>
  64. <!--自定义打包-->
  65. <plugin>
  66. <artifactId>maven-assembly-plugin</artifactId>
  67. <configuration>
  68. <finalName>${artifactId}</finalName>
  69. <!-- not append assembly id in release file name -->
  70. <appendAssemblyId>false</appendAssemblyId>
  71. <descriptors>
  72. <descriptor>src/main/assembly/depolyment.xml</descriptor>
  73. </descriptors>
  74. </configuration>
  75. <executions>
  76. <execution>
  77. <id>dist</id>
  78. <phase>package</phase>
  79. <goals>
  80. <goal>single</goal>
  81. </goals>
  82. </execution>
  83. </executions>
  84. </plugin>
  85. </plugins>
  86. </build>

  在resources配置管理boot 文件

  工程中有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时, 需要打包这个文件。

  

application.properties

  1. spring.profiles.active=@profileActive@
  2. server.port=8040

application-dev.properties

  1. server.port=8050

……

方式2.2、以文件夹方式

  在需要打包的子项目pom设置

  1. <build>
  2. <resources>
  3. <!--文件夹-->
  4. <resource>
  5. <!--指定打包时需要特殊处理的目录文件-->
  6. <directory>src/main/resources</directory>
  7. <!--处理文件是时,需要变量替换-->
  8. <filtering>true</filtering>
  9. <!--打包时,排除文件-->
  10. <excludes>
  11. <exclude>dev_conf/*</exclude>
  12. <exclude>test_conf/*</exclude>
  13. <exclude>prod_conf/*</exclude>
  14. </excludes>
  15. </resource>
  16. <resource>
  17. <!--指定打包时需要特殊处理的目录文件-->
  18. <directory>src/main/resources/${profileActive}_conf</directory>
  19. <!--处理文件是时,需要变量替换-->
  20. <filtering>true</filtering>
  21. </resource>
  22. </resources>
  23. <plugins>
  24. <plugin>
  25. <groupId>org.apache.maven.plugins</groupId>
  26. <artifactId>maven-compiler-plugin</artifactId>
  27. <configuration>
  28. <source>${java.version}</source>
  29. <target>${java.version}</target>
  30. </configuration>
  31. </plugin>
  32. <!--打jar包-->
  33. <plugin>
  34. <groupId>org.apache.maven.plugins</groupId>
  35. <artifactId>maven-jar-plugin</artifactId>
  36. <version>2.6</version>
  37. <configuration>
  38. <archive>
  39. <manifest>
  40. <!--jar入口累-->
  41. <mainClass>com.jd.bt.gateway.ZuulApplication</mainClass>
  42. <!-- classpath路径 -->
  43. <addClasspath>true</addClasspath>
  44. <classpathPrefix>lib/</classpathPrefix>
  45. </manifest>
  46. <manifestEntries>
  47. <Class-Path>./</Class-Path>
  48. </manifestEntries>
  49. </archive>
  50. <excludes>
  51. <!--注意从编译结果目录开始算目录结构-->
  52. <!--<exclude>/*.yml</exclude>-->
  53. <!--<exclude>/*.properties</exclude>-->
  54. <!--<exclude>/*.xml</exclude>-->
  55. <!--<exclude>/*.txt</exclude>-->
  56. </excludes>
  57. </configuration>
  58. </plugin>
  59. <!--自定义打包-->
  60. <plugin>
  61. <artifactId>maven-assembly-plugin</artifactId>
  62. <configuration>
  63. <finalName>${artifactId}</finalName>
  64. <!-- not append assembly id in release file name -->
  65. <appendAssemblyId>false</appendAssemblyId>
  66. <descriptors>
  67. <descriptor>src/main/assembly/depolyment.xml</descriptor>
  68. </descriptors>
  69. </configuration>
  70. <executions>
  71. <execution>
  72. <id>dist</id>
  73. <phase>package</phase>
  74. <goals>
  75. <goal>single</goal>
  76. </goals>
  77. </execution>
  78. </executions>
  79. </plugin>
  80. </plugins>
  81. </build>

  在resources

    

  log4j.xml:共有文件

  env.properties:

  1. env.profile.active=@profileActive@

  注意:使用$符号,不能替换,maven的maven-resources-plugin使用@替换的符号。

3.4、打包即可

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

004-集成maven和Spring boot的profile功能打包的更多相关文章

  1. [Spring Boot 系列] 集成maven和Spring boot的profile功能

    由于项目的需要, 今天给spirng boot项目添加了profile功能.再网上搜索了一圈,也没有找到满意的参考资料,其实配置并不难,就是没有一个one stop(一站式)讲解的地方,所以有了写这篇 ...

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

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

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

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

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

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

  5. 集成maven和Spring boot的profile

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

  6. Spring boot 的profile功能如何实现多环境配置自动切换

    通常服务端应用开发需要经过以下几个流程: 开发 -> 测试 -> RC验证 -> 上线 这就涉及到四个不同的环境,开发环境.测试环境.RC环境以及生产环境,为了避免不同环境之间相互干 ...

  7. 借助Maven入手Spring Boot第一个程序

    目前网上有不少Spring Boot的入门文章,都很有帮助,本人最近在深入学习Spring Cloud,在搭建第一个Hello World程序时,感觉对于新手而言,介绍文章怎么详细都不为过,因为其中坑 ...

  8. Maven 搭建spring boot多模块项目(附源码),亲测可以,感谢原创

    原创地址:https://segmentfault.com/a/1190000005020589 我的DEMO码云地址,持续添加新功能: https://gitee.com/itbase/Spring ...

  9. Myeclipse下使用Maven搭建spring boot项目

    开发环境:Myeclipse2017.JDK1.6.Tomcat 8.0.Myeclipse下使用Maven搭建spring boot项目,详细过程如下: 1. New -> Project.. ...

随机推荐

  1. PHP——修改数据库2-加提示框,加登录页面

    登录页面:0127lianxi.php <body> <h1>登陆</h1> <form action="0127lianxi.php" ...

  2. Spider Studio 新版本 (20131201) - BrowserManager / 节点选择器 / JQueryContext.Focus

    2013-12-1版本更新, 包含如下改动: 1. 修复BrowserManager重复初始化的bug; 2. 大幅提高节点选择器性能: 以网页 http://data.sports.sohu.com ...

  3. selenuim爬虫实战(日lofter.com)

    LOFTER是网易公司2011年8月下旬推出的一款轻博客产品. LOFTER专注于为用户提供简约.易用.有品质.重原创的博客工具.原创社区,以及有品质的手机博客应用. LOFTER首次采用独立域名,口 ...

  4. 24SpringMvc中的重定向和转发-解释return "redirect:/cargo/contractproduct/tocreate.action";

    我在做JK项目时发现:我们在增加一个货物时.会先根据提交的Action()进入到一个新增页面 //跳转到新增的页面 @RequestMapping("/cargo/contractprodu ...

  5. php -- each()、list()

    1.each的用法 先看API:array each ( array &$array ) api里是这么描述的:each — 返回数组中当前的键/值对并将数组指针向前移动一步 我们先来看看返回 ...

  6. IOC和AOP的一些基本概念

    IOC和AOP的一些基本概念介绍 IOC 介绍 IOC 一.什么是IOC IoC就是Inversion of Control,控制反转.在Java开发中,IoC意味着将你设计好的类交给系统去控制,而不 ...

  7. linux下源代码搭建php环境之mysql(一)

    如今已经大半夜了,五一劳动节挺无聊的. 折腾一下吧.实在是睡不着.于是乎在电脑上安装个虚拟机,然后呢,在虚拟机上搭建一个php环境. 首先我得安装MYSQL吧. 发现遇到的问题真多. .待我娓娓道来. ...

  8. Spring_day04--HibernateTemplate介绍_整合其他方式_Spring分模块开发

    HibernateTemplate介绍 1 HibernateTemplate对hibernate框架进行封装, 直接调用HibernateTemplate里面的方法实现功能 2 HibernateT ...

  9. Spring_day04--Spring框架整合hibernate框架

    Spring框架整合hibernate框架 1 把hibernate核心配置文件中配置数据库信息,把数据库信息在spring进行配置 2 把hibernate里面的sessionFactory创建交给 ...

  10. 如何隐藏js

    前端好像一直会遇到js容易被查看的问题,针对这种情况,如何隐藏js呢? 突发奇想,想到一个办法,如果说一段js只需要执行一次的话 可以尝试在所有js加载操作完毕后把它去掉.看代码 <!DOCTY ...