使用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 ...
随机推荐
- python存取数据进阶技巧-pickle,array模块
我们在存/取数据时,没有必要存成文本形式,多试试二进制形式,文本只是骗骗眼睛的,要更快和更高效 1.数组形式 如果我们需要一个之包含数字的列表,那就试试array.array,注意,不是numpy模块 ...
- windows下怎样使用md命令一次建立多级子目录
在Windows系统中一次只能够创建一个子目录,在命令提示符窗口则可以一次性创建多个子目录,例如如果想在f盘创建多级子目录,则md 23\13\65\45,后面的数字随便都可以.如果想一次性删除多级目 ...
- ref:phpstorm配置远程调试(xdebug)(docker中)
ref:https://www.cnblogs.com/yjken/p/6555438.html readme:本文设置远程调试ubuntu中的php代码. 在docker中也可以,经过测试phpin ...
- 使用afl-dyninst fuzz无源码的二进制程序
转:http://ele7enxxh.com/Use-AFL-dyninst-To-Fuzz-Blackbox-Binaries.html 使用afl-dyninst fuzz无源码的二进制程序 通常 ...
- Windows环境上装在VM,VM安装CentOS7
1.下载VM并且安装 VM下载地址:https://www.vmware.com/products/workstation-pro.html 来自百度经验的的一个密钥(VMware Workstati ...
- SQL1:基础
1.SQL命令类型: 1)DDL:CREATE TABLE/INDEX/VIEW ; ALTER TABLE/INDEX/VIEW ; DROP TABLE/INDEX 2)DML:INSERT,UP ...
- CentOS下Supervisor的安装与使用入门
[转载]http://www.51bbo.com/archives/2120 Supervisor是一个进程管理工具,官方的说法 用途就是有一个进程需要每时每刻不断的跑,但是这个进程又有可能由于各种原 ...
- BeautifulSoup解析库
解析库 解析器 使用方法 优势 劣势 Python标准库 BeautifulSoup(html, 'html.parser') 速度适中,容错能力强 老版本python容错能力差 lxml HTML解 ...
- Linux下对拍脚本
使用说明: 1. 被测代码.正确代码.生成器代码均使用文件输入输出: 2. 对拍前会清屏,请注意: 3. 输出文件的文件名请和代码文件名保持一致: 4. 若无 ...
- 数据准备<1>:数据质量检查-理论篇
数据行业有一句很经典的话--"垃圾进,垃圾出"(Garbage in, Garbage out, GIGO),意思就是,如果使用的基础数据有问题,那基于这些数据得到的任何产出都是没 ...