使用maven的profile切换项目各环境的参数
Java后端开发经常需要面对管理多套环境,一般有三种环境:开发,测试,生产。
各个环境之间的参数各不相同,比如MySQL、Redis等不同环境的host不一样,若每个环境都手动替换环境很容易出错,Maven profile正是提供了配置多种环境的功能。
创建一个与resource文件夹同级的文件夹,我这里取名叫profiles

src/main/profiles/dev 目录对应开发环境的配置项目
src/main/profiles/beta 目录对应测试环境的配置项目
src/main/profiles/gray 目录对应灰度环境的配置项目
src/main/profiles/prd 目录对应生产环境的配置项目
接下来就是配置maven profile,如下,设定dev是默认激活的profile,此外根据具体的环境名称引入对应的资源文件夹。
<profiles>
<profile>
<!-- 本地开发环境 -->
<id>dev</id>
<properties>
<!-- 这里的属性名是随便取的,可以在后续配置中引用 -->
<profiles.dir>dev</profiles.dir>
</properties>
<!-- 是否默认 -->
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile> <profile>
<!-- beta环境 -->
<id>beta</id>
<properties>
<profiles.dir>beta</profiles.dir>
</properties>
</profile> <profile>
<!-- 灰度环境 -->
<id>gray</id>
<properties>
<profiles.dir>gray</profiles.dir>
</properties>
</profile> <profile>
<!-- 生产环境 -->
<id>prod</id>
<properties>
<profiles.dir>prod</profiles.dir>
</properties>
</profile>
</profiles>
activeByDefault标签的值为true的话表示默认的profile,使用mvn install命令起作用的就是它,这里为dev
<build>
<finalName>${project.artifactId}</finalName>
<resources>
<!-- 这里的resource配置的是需要导入到项目的资源文件夹 -->
<resource>
<directory>src/main/resources</directory>
</resource>
<!-- 这里的${profiles.dir}是你上面配置的属性值,用于动态替换,比如打包的时候输入的是-Pdev则这里就是的${profiles.dir}就是dev的值 -->
<resource>
<directory>src/main/profiles/${profiles.dir}</directory>
</resource>
</resources>
</build>
如果要把这些文件放在spring容器中呢?
在war包中这些文件就是在classpath中。
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="locations">
<list>
<value>classpath:environment.properties</value>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>
使用maven的打包命令并且指定使用那个文件夹做为资源文件
mvn clean package -Dmaven.test.skip=true -Pbeta
注意最后一个-P beta,maven会激活项目下的pom.xml配置的<profiles>标签下id为test。
maven命令选项
[root@winner_0715 bin]# mvn --help usage: mvn [options] [<goal(s)>] [<phase(s)>] Options:
-am,--also-make If project list is specified, also
build projects required by the
list
-amd,--also-make-dependents If project list is specified, also
build projects that depend on
projects on the list
-B,--batch-mode Run in non-interactive (batch)
mode
-b,--builder <arg> The id of the build strategy to
use.
-C,--strict-checksums Fail the build if checksums don't
match
-c,--lax-checksums Warn if checksums don't match
-cpu,--check-plugin-updates Ineffective, only kept for
backward compatibility
-D,--define <arg> Define a system property
-e,--errors Produce execution error messages
-emp,--encrypt-master-password <arg> Encrypt master security password
-ep,--encrypt-password <arg> Encrypt server password
-f,--file <arg> Force the use of an alternate POM
file (or directory with pom.xml).
-fae,--fail-at-end Only fail the build afterwards;
allow all non-impacted builds to
continue
-ff,--fail-fast Stop at first failure in
reactorized builds
-fn,--fail-never NEVER fail the build, regardless
of project result
-gs,--global-settings <arg> Alternate path for the global
settings file
-h,--help Display help information
-l,--log-file <arg> Log file to where all build output
will go.
-llr,--legacy-local-repository Use Maven Legacy Local
Repository behaviour, ie no use of
_remote.repositories. Can also be
activated by using
-Dmaven.legacyLocalRepo=true
-N,--non-recursive Do not recurse into sub-projects
-npr,--no-plugin-registry Ineffective, only kept for
backward compatibility
-npu,--no-plugin-updates Ineffective, only kept for
backward compatibility
-nsu,--no-snapshot-updates Suppress SNAPSHOT updates
-o,--offline Work offline
-P,--activate-profiles <arg> Comma-delimited list of profiles
to activate
-pl,--projects <arg> Comma-delimited list of specified
reactor projects to build instead
of all projects. A project can be
specified by [groupId]:artifactId
or by its relative path.
-q,--quiet Quiet output - only show errors
-rf,--resume-from <arg> Resume reactor from specified
project
-s,--settings <arg> Alternate path for the user
settings file
-T,--threads <arg> Thread count, for instance .0C
where C is core multiplied
-t,--toolchains <arg> Alternate path for the user
toolchains file
-U,--update-snapshots Forces a check for missing
releases and updated snapshots on
remote repositories
-up,--update-plugins Ineffective, only kept for
backward compatibility
-V,--show-version Display version information
WITHOUT stopping build
-v,--version Display version information
-X,--debug Produce execution debug output
使用maven的profile切换项目各环境的参数的更多相关文章
- IDEA结合Maven的profile构建不同开发环境(SpringBoot)
一.概述 在开发过程中,我们的项目会存在不同的开发环境,比如开发环境.生产环境.测试环境,而我们的项目在不同的环境中有些配置也是不一样的,比如数据源配置.日志文件配置等,假如我们每次将软件部署到不同的 ...
- 【maven】Maven根据Profile读取不同配置环境配置文件
开发需求:在日常开发中,我们大多都会有开发环境(dev).测试环境(test).生产环境(product),不同环境的参数肯定不一样,我们需要在打包的时候,不同环境打不同当包,如果手动改,一方面效率低 ...
- 【转】 利用spring的profile切换不同的环境
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- Maven之profile实现多环境配置动态切换
一般的软件项目,在开发.测试及生产等环境下配置文件中参数是不同的.传统的做法是在项目部署的时候,手动修改或者替换这个配置文件.这样太麻烦了,我们可以用Maven的profile来解决这 ...
- Spring boot项目maven的profile多环境配置不自动替换变量的问题解决
Spring boot项目maven的profile多环境配置不自动替换变量的问题解决 在网上找了好久,配置都很简单,可是我的程序就是不能自动替换变量,最终单独测试,发现原来是引用spring b ...
- maven profile切换正式环境和测试环境
有时候,我们在开发和部署的时候,有很多配置文件数据是不一样的,比如连接mysql,连接redis,一些properties文件等等 每次部署或者开发都要改配置文件太麻烦了,这个时候,就需要用到mave ...
- 【POM】maven profile切换正式环境和测试环境
有时候,我们在开发和部署的时候,有很多配置文件数据是不一样的,比如连接mysql,连接redis,一些properties文件等等 每次部署或者开发都要改配置文件太麻烦了,这个时候,就需要用到mave ...
- maven 利用 profile 进行多环境配置
我们在进行项目的多环境配置时,有很多种方式供我们选择,比如 SpringBoot 自带的 application-dev.yml.maven 的 profile 等.这里介绍的就是如何利用 profi ...
- jenkins+git+maven搭建自动化部署项目环境
简介 折腾了两个晚上,趁着今晚比较有空,把jenkins+git+maven搭建自动化部署项目环境搭建的过程记录一下,这里我把github作为git的远程仓库(https://github.co ...
随机推荐
- awk进阶
整理的awk的小技巧 begin是要放在正则前面的,按照这个顺序: awk 'begin{} /.*?/ {action}end{}' file FS=':' 和 -F: 是等同的 -F 表示以 XX ...
- elementUI 学习入门之 container 布局容器
Container 布局容器 用于布局的容器组件,方便快速搭建页面基本结构 <el-container> : 外层容器.当子元素包含 <el-header> 或 <el- ...
- [Codeforces166B]Polygons 凸包
大致题意: 给你一个凸多边形A,和一个任意多边形B,判断B是否在A的内部 先对A的点集建凸包,然后枚举B中的点,二分判断是否在A的内部. 二分时可用叉积判断,详细见代码 #include<cst ...
- 长沙理工大学第十二届ACM大赛-重现赛 L - 选择困难症
题目描述 小L有严重的选择困难症. 早上起床后,需要花很长时间决定今天穿什么出门. 假设一共有k类物品需要搭配选择,每类物品的个数为Ai,每个物品有一个喜欢值Vj,代表小L对这件物品的喜欢程度. 小L ...
- HDU 6026 Deleting Edges
最短路. 先建一个只包含最短路的有向无环图,每一个点选择任意一条入边即可生成一个树形图,那么树的种类就等于每个点的入度乘积. #include <bits/stdc++.h> using ...
- scrapy实现多级页面的抓取时使用meta传递item数据的问题(转)
name = 'doubanzufang'start_urls = ['https://www.douban.com/group/tianhezufang/discussion?start=50'] ...
- Dijkstra【P2446】 [SDOI2010]大陆争霸
Background 在一个遥远的世界里有两个国家:位于大陆西端的杰森国和位于大陆东端的克里斯国.两个国家的人民分别信仰两个对立的神:杰森国信仰象征黑暗和毁灭的神曾·布拉泽,而克里斯国信仰象征光明和永 ...
- Dictionary<TKey,TValue>
/* * 先将 key 和 bucket 的长度一起,经过简单的 hash 算法计算出元素应该放在哪个 bucket . * 但是,元素并不是放在 bucket 里面的,bucket 只是对元素存放位 ...
- Tomcat在Eclips中的使用及注意细节
1.运行环境,先配置Eclips Eclips中的Windows→ preferences→弹出框左边Server→Runtime Environments→右边Add添加需要的Apach Tomca ...
- hibernate 基于主键的单向一对一关联映射
1.设计表结构 表结构对于基于外键的关联关系来说就少了外键的关联列,并且两张表共用同一个ID,表示一对一. 2.创建Person对象 3.创建IdCard对象 4.写hbm.xml文件 5.生成数据库 ...