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.. ...
随机推荐
- ajax——用ajax写用户注册
zhuce.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://w ...
- DFS应用——遍历有向图+判断有向图是否有圈
[0]README 0.1) 本文总结于 数据结构与算法分析, 源代码均为原创, 旨在 理解 "DFS应用--遍历有向图+判断有向图是否有圈" 的idea 并用源代码加以实现 : ...
- sdut 2158:Hello World!(第一届山东省省赛原题,水题,穷举)
Hello World! Time Limit: 1000MS Memory limit: 65536K 题目描述 We know that Ivan gives Saya three problem ...
- LodRunner实现大负载测试的四部曲(配置系统参数、配置LR、修改脚本、设置组策略)
见 http://www.51testing.com/?uid-97659-action-viewspace-itemid-210924 LoadRunner以下简称(LR)是目前业界最流行的压力测试 ...
- 用Powershell调用DLL文件
http://blog.csdn.net/itanders/article/details/5702771
- Spring_day04--SSH框架整合过程
SSH框架整合过程 第一步 导入jar包 第二步 搭建struts2环境 (1)创建action,创建struts.xml配置文件,配置action (2)配置struts2的过滤器 第三步 搭建hi ...
- Java线程之CompletionService批处理任务
如果你向Executor提交了一个批处理任务,并且希望在它们完成后获得结果,怎么办呢? 为此你可以保存与每个任务相关联的Future,然后不断地调用 timeout为零的get,来检验Future是否 ...
- RF内建的变量
${CURDIR} 提供当前测试文件存放的绝对路径.该变量是大小写敏感的.${TEMPDIR} 获取操作系统临时文件夹的绝对路径. 在UNIX系统是在/tmp, 在windows系统是在c:\Docu ...
- Android之检查网络是否可用(跳转网络设置页面)
private boolean NetWorkStatus() { ConnectivityManager cwjManager = (ConnectivityManager) getSystemSe ...
- $(document).scrollTop()与$(window).scrollTop()
$(document).scrollTop() 获取垂直滚动的距离 即当前滚动的地方的窗口顶端到整个页面顶端的距离 要获取顶端 只需要获取到scrollTop()==0的时候 就是顶端了 要获取底端 ...