004-集成maven和Spring boot的profile功能打包
参考地址:https://blog.csdn.net/lihe2008125/article/details/50443491
一、主要目标
1、通过mvn在命令行中打包时,可以指定相应的profile。
2、需使用了maven的profile功能
3、使用了Spring Boot的profile功能
二、profile简介
什么是profile,解决什么问题呢?
一般在开发项目的时候要有多个环境,如开发环境、测试环境、生产环境,配置文件一般不同。当要向各个环境发布程序时,需要人工处理这些配置文件。有了profile,只要在maven打包时使用下面命令即可。
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配置
<profiles>
<profile>
<id>dev</id>
<properties>
<profileActive>dev</profileActive>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>test</id>
<properties>
<profileActive>test</profileActive>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<profileActive>properties</profileActive>
</properties>
</profile>
</profiles>
方式一、基本打包
测试环境:springboot 1.5.1下
在build中配置,下文 中会有详细解释
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<excludes>
<exclude>application-dev.properties</exclude>
<exclude>application-test.properties</exclude>
<exclude>application-produce.properties</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>application.properties</include>
<include>application-${profiles.active}.properties</include>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
因可能版本较低,需要配置 maven-resources-plugin,否则变量不替换,但是更换了springboot 2.0.4后不用配置既可以
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<delimiters><!-- 使用${..}作为占位符 -->
<delimiter>${*}</delimiter>
</delimiters>
<!-- 使用默认的占位符(@..@) -->
<useDefaultDelimiters>true</useDefaultDelimiters>
</configuration>
</plugin>
</plugins>
方式二、自定义配置的打包
1、配置好:assembly/depolyment.xml
<?xml version="1.0" encoding="UTF-8"?>
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
<id>dist</id>
<formats>
<format>zip</format>
<format>dir</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory> <fileSets>
<fileSet>
<directory>src/main/bin</directory>
<outputDirectory>bin/</outputDirectory>
</fileSet>
<!--<fileSet>-->
<!--<directory>src/main/resources</directory>-->
<!--<outputDirectory>/</outputDirectory>-->
<!--</fileSet>--> <fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<outputDirectory>lib</outputDirectory>
<scope>runtime</scope>
<excludes>
<exclude>${groupId}:${artifactId}</exclude>
</excludes>
</dependencySet>
</dependencySets>
</assembly>
2、多环境配置
方式2.1、文件后缀方式
在需要打包的子项目pom设置
<build>
<resources>
<resource>
<!--指定打包时需要特殊处理的目录文件-->
<directory>src/main/resources</directory>
<!--处理文件是时,需要变量替换-->
<filtering>true</filtering>
<!--打包时,排除文件-->
<excludes>
<exclude>application.properties</exclude>
<exclude>application-dev.properties</exclude>
<exclude>application-test.properties</exclude>
<exclude>application-prod.properties</exclude>
</excludes>
</resource>
<resource>
<!--指定打包时需要特殊处理的目录文件-->
<directory>src/main/resources</directory>
<!--处理文件是时,需要变量替换-->
<filtering>true</filtering>
<!--打包时,包含文件-->
<includes>
<include>application.properties</include>
<include>application-${profileActive}.properties</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<!--打jar包-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration>
<archive>
<manifest>
<!--jar入口累-->
<mainClass>com.jd.bt.gateway.ZuulApplication</mainClass>
<!-- classpath路径 -->
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
</manifest>
<manifestEntries>
<Class-Path>./</Class-Path>
</manifestEntries>
</archive>
<excludes>
<!--注意从编译结果目录开始算目录结构-->
<!--<exclude>/*.yml</exclude>-->
<!--<exclude>/*.properties</exclude>-->
<!--<exclude>/*.xml</exclude>-->
<!--<exclude>/*.txt</exclude>-->
</excludes>
</configuration>
</plugin>
<!--自定义打包-->
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<finalName>${artifactId}</finalName>
<!-- not append assembly id in release file name -->
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>src/main/assembly/depolyment.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>dist</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</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
spring.profiles.active=@profileActive@
server.port=8040
application-dev.properties
server.port=8050
……
方式2.2、以文件夹方式
在需要打包的子项目pom设置
<build>
<resources>
<!--文件夹-->
<resource>
<!--指定打包时需要特殊处理的目录文件-->
<directory>src/main/resources</directory>
<!--处理文件是时,需要变量替换-->
<filtering>true</filtering>
<!--打包时,排除文件-->
<excludes>
<exclude>dev_conf/*</exclude>
<exclude>test_conf/*</exclude>
<exclude>prod_conf/*</exclude>
</excludes>
</resource>
<resource>
<!--指定打包时需要特殊处理的目录文件-->
<directory>src/main/resources/${profileActive}_conf</directory>
<!--处理文件是时,需要变量替换-->
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<!--打jar包-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration>
<archive>
<manifest>
<!--jar入口累-->
<mainClass>com.jd.bt.gateway.ZuulApplication</mainClass>
<!-- classpath路径 -->
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
</manifest>
<manifestEntries>
<Class-Path>./</Class-Path>
</manifestEntries>
</archive>
<excludes>
<!--注意从编译结果目录开始算目录结构-->
<!--<exclude>/*.yml</exclude>-->
<!--<exclude>/*.properties</exclude>-->
<!--<exclude>/*.xml</exclude>-->
<!--<exclude>/*.txt</exclude>-->
</excludes>
</configuration>
</plugin>
<!--自定义打包-->
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<finalName>${artifactId}</finalName>
<!-- not append assembly id in release file name -->
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>src/main/assembly/depolyment.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>dist</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
在resources
log4j.xml:共有文件
env.properties:
env.profile.active=@profileActive@
注意:使用$符号,不能替换,maven的maven-resources-plugin使用@替换的符号。
3.4、打包即可
mvn clean package -Dmaven.test.skip=true -P dev -e
004-集成maven和Spring boot的profile功能打包的更多相关文章
- [Spring Boot 系列] 集成maven和Spring boot的profile功能
由于项目的需要, 今天给spirng boot项目添加了profile功能.再网上搜索了一圈,也没有找到满意的参考资料,其实配置并不难,就是没有一个one stop(一站式)讲解的地方,所以有了写这篇 ...
- 集成maven和Spring boot的profile功能
思路:maven支持profile功能,当使用maven profile打包时,可以打包指定目录和指定文件,且可以修改文件中的变量.spring boot也支持profile功能,只要在applica ...
- 集成maven和Spring boot的profile 专题
maven中配置profile节点: <project> .... <profiles> <profile> <!-- 生产环境 --> <id& ...
- [Spring Boot 系列] 集成maven和Spring boot的profile 专题
maven中配置profile节点: <project> .... <profiles> <profile> <!-- 生产环境 --> <id& ...
- 集成maven和Spring boot的profile
如果在配置中勾选了多套配置,则以pom.xml文件中 profiles中 配置 最后一个配置为准. maven中配置profile节点: <project> .... <profi ...
- Spring boot 的profile功能如何实现多环境配置自动切换
通常服务端应用开发需要经过以下几个流程: 开发 -> 测试 -> RC验证 -> 上线 这就涉及到四个不同的环境,开发环境.测试环境.RC环境以及生产环境,为了避免不同环境之间相互干 ...
- 借助Maven入手Spring Boot第一个程序
目前网上有不少Spring Boot的入门文章,都很有帮助,本人最近在深入学习Spring Cloud,在搭建第一个Hello World程序时,感觉对于新手而言,介绍文章怎么详细都不为过,因为其中坑 ...
- Maven 搭建spring boot多模块项目(附源码),亲测可以,感谢原创
原创地址:https://segmentfault.com/a/1190000005020589 我的DEMO码云地址,持续添加新功能: https://gitee.com/itbase/Spring ...
- Myeclipse下使用Maven搭建spring boot项目
开发环境:Myeclipse2017.JDK1.6.Tomcat 8.0.Myeclipse下使用Maven搭建spring boot项目,详细过程如下: 1. New -> Project.. ...
随机推荐
- [android] AndroidManifest.xml -【manifest】
<manifest> 原文链接:http://www.android-doc.com/guide/topics/manifest/manifest-element.html 最早引入的AP ...
- ubuntu vnc安装
VNC(Virtual Network Computing),为一种使用RFB协议的屏幕画面分享及远程操作软件.此软件借由网络,可发送键盘与鼠标的动作及即时的屏幕画面. 1. 安装vnc服务器 sud ...
- OGNL(Object-Graph Navigation Language),可以方便地操作对象属性的开源表达式语言,使页面更简洁;
OGNL(Object-Graph Navigation Language),可以方便地操作对象属性的开源表达式语言,使页面更简洁: 支持运算符(如+-*/),比普通的标志具有更高的自由度和更强的功能 ...
- ResultSet是结果集对象
ResultSet是结果集对象 DriverManager管理一组驱动程序 PreparedStatement预编译的,用来发送和执行SQL语句的
- java.lang下面有一个接口:Comparable(可比较的)
对于自定义对象,Sort不知道规则,所以无法比较.这种情况下一定要定义排序规则.方式有两种: java.lang下面有一个接口:Comparable(可比较的) 可以让自定义对象实现一个接口,这个接口 ...
- leetcode -- Balanced Binary Tree TODO
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- Linux命令之乐--seq
用法及参数: -f, --format=格式 使用printf 样式的浮点格式,默认是g% -s, --separator=字符串 使用指定字符串分隔数字(默认使用:\n) - ...
- 传参方法:sharedApplication, NSUserDefaults, protocol 和 delegate(实例)
本文转载至 http://blog.csdn.net/learnios/article/details/8442201 分类: 功能模块2012-12-27 10:22 109人阅读 评论(0) 收 ...
- 《C++ Primer Plus》学习笔记 第1章 预备知识
第一章 预备知识C++在C语言的基础上添加了对"面向对象编程"的支持和对"泛型编程"的支持.类 —— 面向对象模板 —— 泛型编程1.1 C++简介1.2 C+ ...
- Objective-C内存管理基础
2011-05-11 15:45 朱祁林 http://zhuqil.cnblogs.com 字号:T | T 本文我们将介绍<Objective-C内存管理基础>,在iOS开发中,内存管 ...