详解maven中profiles使用实现
作者:bbird2018 发布时间:2022-11-13 23:14:24
使用的场景
常常遇到一些项目中多环境切换的问题。比如在开发过程中用到开发环境,在测试中使用测试环境,在生产中用生产环境的情况。springboot中提供了 spring.profile.active的方式来实现多环境的切换,通过设置环境变量和启动参数的方式。但是这样做终究不能一劳永逸,要么需要修改yml文件,要么需要记得启动的时候带上参数。而利用maven的profiles,可以减少很多工作。让我们通过几个例子一步步的掌握使用maven的profiles属性。
快速上手
pom.xml文件设置
<profiles>
<profile>
<!--不同环境Profile的唯一id-->
<id>dev</id>
<properties>
<!--profiles.active是自定义的字段(名字随便起),自定义字段可以有多个-->
<profiles.active>dev</profiles.active>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<profiles.active>prod</profiles.active>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>test</id>
<properties>
<profiles.active>test</profiles.active>
</properties>
</profile>
</profiles>
目录结构
application.yml
spring:
profiles:
active: @profiles.active@
application-dev.yml中代码如下
server:
port: 7091
其他几个文件我只是把端口号进行了修改,方便打包看不同的效果。
maven打包与激活profiles
你可以执行命令
mvn clean package -Ptest
然后启动jar包,可以看到jar包启动的是test的配置,如果换成-Pdev启动的就是dev包的端口。
默认启动方式
如果不带-Ptest,启动的是 prod的端口。因为在profiles中我们看到有配置默认的选项。
<activation>
<activeByDefault>true</activeByDefault>
</activation>
settings.xml中使用activeProfiles指定
<activeProfiles>
<activeProfile>profileTest1</activeProfile>
</activeProfiles>
通过IDEA的可视化的方式
当然如果使用IDEA工具进行开发,还可以使用可视化的方式进行打包。
更高级的玩法
通过和pom结合的方式设置动态参数
如果我们希望通过docker-maven-plugin插件,把编译好的jar打包成docker并且传入相应的开发、测试、生产的服务器中去。这个时候,我们就需要根据不同的条件去传入不同的服务器。
在profiles中我们可以做以下定义
<profiles>
<profile>
<id>dev</id>
<properties>
<profile.id>dev</profile.id>
<docker.host>http://dev.demo.com:2375</docker.host>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>test</id>
<properties>
<profile.id>test</profile.id>
<docker.host>http://test.demo.com375</docker.host>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<profile.id>prod</profile.id>
<docker.host>http://prod.demo.com:2375</docker.host>
</properties>
</profile>
</profiles>
而在build控件中我们可以使用以下配置
<build>
<plugins>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.1.0</version>
<executions>
<execution>
<id>build-image</id>
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
<configuration>
<imageName>demo/${project.artifactId}</imageName>
<imageTags>
<imageTag>${project.version}-${current.time}</imageTag>
<imageTag>latest</imageTag>
</imageTags>
<forceTags>true</forceTags>
<dockerHost>${docker.host}</dockerHost>
<forceTags>true</forceTags>
<baseImage>java:8</baseImage>
<entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
</plugins>
</build>
其中 ${project.artifactId} 和${project.version}是关于节点下面和的引用。${current.time}是在build-helper-maven-plugin定义的,我们回头再研究。
${docker.host}则是我们在profiles中定义的,可以随着我们选择不同的profile,把jar包build成不同的docker镜像,并传入指定服务器。
通过和yml结合设置动态参数
除了可以在pom中设置动态参数,使得其根据profile的不同选择不同的参数。还可以通过设置不同的profile,让yml选择不同的参数。这点和快速上手的例子有点相似。具体如下:
设置profiles
<profiles>
<profile>
<id>dev</id>
<properties>
<profile.id>dev</profile.id>
<eureka.url>http://127.0.0.1:8001/eureka</eureka.url>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>test</id>
<properties>
<profile.id>test</profile.id>
<eureka.url>http://base-registry:8001/eureka</eureka.url>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<profile.id>prod</profile.id>
<eureka.url>http://base-registry:8001/eureka</eureka.url>
</properties>
</profile>
<profile>
<id>new</id>
<properties>
<profile.id>new</profile.id>
<eureka.url>http://base-registry:8001/eureka</eureka.url>
</properties>
</profile>
</profiles>
我们在profile中设置了一个eureka.url的属性,就可以在yml中直接调用。
eureka:
client:
service-url:
defaultZone: @eureka.url@
registry-fetch-interval-seconds: 10
instance:
prefer-ip-address: true
在IDEA调试和启动的时候,一般会报错如下:
org.yaml.snakeyaml.scanner.ScannerException: while scanning for the next token found character ‘@’ that cannot start any token.
解决方法就是引入yaml.sankeyaml的jar包
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
</dependency>
打包不同的资源
在profile打包yml文件的时候,如果我们解压了jar包,会发现还是把所有的application-profile.yml文件给打包进去了。这个可以通过设置打包参数,只打包需要的application文件。
<profiles>
<profile>
<id>dev</id>
<properties>
<env>dev</env>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>prd</id>
<properties>
<env>prd</env>
</properties>
</profile>
</profiles>
<build>
<finalName>springmvc</finalName>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>dev/*</exclude>
<exclude>prd/*</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources/${env}</directory>
</resource>
</resources>
</build>
目录结构如下:
来源:https://www.cnblogs.com/bbird/p/15865346.html


猜你喜欢
- 一、点睛邻接矩阵通常采用一个一维数组存储图中节点的信息,采用一个二维数组存储图中节点之间的邻接关系。邻接矩阵可以用来表示无向图、有向图和网。
- 本文实例为大家分享了java web个人通讯录系统的具体代码,供大家参考,具体内容如下现在开始上截图:下面粘贴代码:首先是目录结构:<
- 最近在做毕业设计,想有一个功能和QQ一样可以裁剪头像并设置圆形头像,额,这是设计狮的一种潮流。而纵观现在主流的APP,只要有用户系统这个功能
- SpringBoot的具体介绍可以参看其他网上介绍,这里就不多说了,就这几天的学习,个人理解,简而言之: (1)它是Spring的
- 本文实例为大家分享了java封装前端查询条件的具体代码,供大家参考,具体内容如下import hengyi.oa.mobile.except
- jedis是redis的java客户端,spring将redis连接池作为一个bean配置。redis连接池分为两种,一种是“redis.c
- Invoke Phing targets这个插件主要是读取xml形式包括自动化测试打包部署的配置文件,然后根据流程走下来。用ph
- Result可以设定全局结果集,如:<struts> <constant name="struts
- 为什么使用logback记得前几年工作的时候,公司使用的日志框架还是log4j,大约从16年中到现在,不管是我参与的别人已经搭建好的项目还是
- 本文实例讲述了Android开发中应用程序分享功能。分享给大家供大家参考,具体如下:Intent shareIntent = new Int
- 自定义注解1) 先定义布局文件注入//注解的作用域在类上@Target(ElementType.TYPE)//让保持性策略为运行时态,将注解
- watch机制Zookeeper watch是一种监听通知机制,可以随时监听一些数据的变化,从而实现数据的及时性。Zookeeper所有的读
- 一、单例模式的思想想整理一些 java 并发相关的知识,不知道从哪开始,想起了单例模式中要考虑的线程安全,就从单例模式开始吧。以前写过单例模
- Java基础面试题及答案集锦(基础题122道,代码题19道),具体详情如下所示:1、面向对象的特征有哪些方面1.抽象:抽象就是忽略一个主题中
- 部署在tomcat容器中首先需要添加一些新的包和启动程序1.在pom.xml文件中packaging便签下 jar 改为 war<pa
- 1. 引入依赖pom文件引入activemq依赖<!--activeMq配置--> &
- 代码:package com.lwj.test.proxy;import java.lang.reflect.InvocationHandl
- 一般来说一个 HTML 文档有很多标签,比如“<html>”、“<body>”、“<table>”等,想
- 什么是 MyBatis 缓存使⽤缓存可以减少 Java 应⽤与数据库的交互次数,从而提升程序的运行效率。⽐如查询出 id = 1 的对象,第
- 今天学习了Map集合的几种方法,尤其是遍历Map集合感觉尤为重要,所以发出来供大家学习和自己复习以用。众所周知Map集合里存储元素是以键值对