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

resources标签定义要包含的资源,在下面的配置下package阶段会把resources文件夹里的 ${profiles.activation}/* 文件打包
这里的${profiles.activation}由命令maven的-P选项指定,例:mvn install -Pbeta 就是打包 beta/* 即beta目录下的所有文件 
<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切换项目各环境的参数的更多相关文章

  1. IDEA结合Maven的profile构建不同开发环境(SpringBoot)

    一.概述 在开发过程中,我们的项目会存在不同的开发环境,比如开发环境.生产环境.测试环境,而我们的项目在不同的环境中有些配置也是不一样的,比如数据源配置.日志文件配置等,假如我们每次将软件部署到不同的 ...

  2. 【maven】Maven根据Profile读取不同配置环境配置文件

    开发需求:在日常开发中,我们大多都会有开发环境(dev).测试环境(test).生产环境(product),不同环境的参数肯定不一样,我们需要在打包的时候,不同环境打不同当包,如果手动改,一方面效率低 ...

  3. 【转】 利用spring的profile切换不同的环境

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  4. Maven之profile实现多环境配置动态切换

            一般的软件项目,在开发.测试及生产等环境下配置文件中参数是不同的.传统的做法是在项目部署的时候,手动修改或者替换这个配置文件.这样太麻烦了,我们可以用Maven的profile来解决这 ...

  5. Spring boot项目maven的profile多环境配置不自动替换变量的问题解决

    Spring boot项目maven的profile多环境配置不自动替换变量的问题解决   在网上找了好久,配置都很简单,可是我的程序就是不能自动替换变量,最终单独测试,发现原来是引用spring b ...

  6. maven profile切换正式环境和测试环境

    有时候,我们在开发和部署的时候,有很多配置文件数据是不一样的,比如连接mysql,连接redis,一些properties文件等等 每次部署或者开发都要改配置文件太麻烦了,这个时候,就需要用到mave ...

  7. 【POM】maven profile切换正式环境和测试环境

    有时候,我们在开发和部署的时候,有很多配置文件数据是不一样的,比如连接mysql,连接redis,一些properties文件等等 每次部署或者开发都要改配置文件太麻烦了,这个时候,就需要用到mave ...

  8. maven 利用 profile 进行多环境配置

    我们在进行项目的多环境配置时,有很多种方式供我们选择,比如 SpringBoot 自带的 application-dev.yml.maven 的 profile 等.这里介绍的就是如何利用 profi ...

  9. jenkins+git+maven搭建自动化部署项目环境

    简介    折腾了两个晚上,趁着今晚比较有空,把jenkins+git+maven搭建自动化部署项目环境搭建的过程记录一下,这里我把github作为git的远程仓库(https://github.co ...

随机推荐

  1. HTK训练错误消息意义

    在HTK训练线上数据的时候,遇到了ERROR [+6550] LoadHTKLabels: Junk at end of HTK transcription,这个问题,网上查阅是说有空行,结果根本没有 ...

  2. 项目里用到的python知识点

    1 ini文件处理创建ini文件config = configparser.ConfigParser()config.read(AUTH_STATUS_FILE)config.add_section( ...

  3. PHP学习笔记(一)数组

    初始化数组的方法:统一初始化或逐项初始化. 遍历数组的方法: 1.FOR循环 2.DO...WHILE语句 3.WHILE语句 4.foreach foreach($arr as $key=>$ ...

  4. XML和HTML的区别

    1.很多新手程序员总是会问HTML和XML有什么区别,接下来就解释一下: 什么是XML? XML 指可扩展标记语言(EXtensible Markup Language). XML 是一种很像HTML ...

  5. Spring Cloud Config 使用总结

    Spring Cloud Config 使用总结 源码 https://github.com/ChangMuChen/Spring-Boot/tree/master/studies/sourcecod ...

  6. 无框架完整搭建安卓app及其服务端(一)

    技术背景: 我的一个项目做的的是图片处理,用 python 实现图片处理的核心功能后,想部署到安卓app中,但是对于一个对安卓和服务器都一知半解的小白来说要现学的东西太多了. 而实际上,我们的项目要求 ...

  7. hihocoder #1580 : Matrix (DP)

    #1580 : Matrix 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 Once upon a time, there was a little dog YK. On ...

  8. Maven的安装及修改为阿里云下载依赖

    使用JAVA工程管理越来越多的jar包,担心导错了,多导了,漏导了怎么办? 换一个IDE项目后项目会不会出一堆BUG,看的头皮发麻? 自己写的代码放在别人的机器上运行会不会出问题? Maven的强大毋 ...

  9. nyoj 737 石子合并 http://blog.csdn.net/wangdan11111/article/details/45032519

    http://blog.csdn.net/wangdan11111/article/details/45032519 http://acm.nyist.net/JudgeOnline/problem. ...

  10. HDU 1698 Just a Hook (线段树)

    Problem Description In the game of DotA, Pudge’s meat hook is actually the most horrible thing for m ...