使用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 ...
随机推荐
- 【PAT】1013. 数素数 (20)
1013. 数素数 (20) 令Pi表示第i个素数.现任给两个正整数M <= N <= 104,请输出PM到PN的所有素数. 输入格式: 输入在一行中给出M和N,其间以空格分隔. 输出格式 ...
- vue js moment.js 过滤了双休日和法定节假日
源码:注!原创的!!!! <template> <div id="DATE"> <ul class="dateForm" @cha ...
- 动态引入js
function loadScript(url, callback){ var script = document.createElement("script") ...
- 【笔试题】怎样将 GB2312 编码的字符串转换为 ISO-8859-1 编码的字符串?
笔试题 怎样将 GB2312 编码的字符串转换为 ISO-8859-1 编码的字符串? import java.io.UnsupportedEncodingException; public clas ...
- Qt基础——让使用Designer创建的UI也能自动适应窗口大小
原文请看:http://www.cnblogs.com/linmeng/archive/2012/07/05/2559259.html 我们知道,通过Qt的各种Layout可以实现控件的自动布局. 但 ...
- Python 正则表达式中级
首先是?: 在括号中用?:用在findall和split之中,去除括号优先级. 如果不用只输出括号内匹配的值 r 的作用是转义python里面换行符等,像是\n 不用加\来转义 1.子表达式 ...
- 正则表达式 \ 和 原生字符串 r
使用python写字符串常量时,raw string是个很好用的东东,比如在C里我要写一个Windows下的路径,得这么写: char *path = "C:\\mydir\\myfile. ...
- HDU 1880 简单Hash
题目链接:[http://acm.hdu.edu.cn/showproblem.php?pid=1880] 中文题面,题意很简单: 题解: 把每个 魔咒 和 对应的功能分别Hash,然后分别映射到ma ...
- HDU 6203 ping ping ping [LCA,贪心,DFS序,BIT(树状数组)]
题目链接:[http://acm.hdu.edu.cn/showproblem.php?pid=6203] 题意 :给出一棵树,如果(a,b)路径上有坏点,那么(a,b)之间不联通,给出一些不联通的点 ...
- [BZOJ3583]杰杰的女性朋友(矩阵快速幂)
杰杰的女性朋友 时间限制:10s 空间限制:256MB 题目描述 杰杰是魔法界的一名传奇人物.他对魔法具有深刻的洞察力,惊人的领悟力,以及令人叹为观止的创造力.自从他从事魔法竞赛以来,短短几 ...