转载:https://blog.csdn.net/java_collect/article/details/83870215

前言
       在开发过程中,我们的项目会存在不同的运行环境,比如开发环境、测试环境、生产环境,而我们的项目在不同的环境中,有的配置可能会不一样,比如数据源配置、日志文件配置、以及一些软件运行过程中的基本配置,那每次我们将软件部署到不同的环境时,都需要修改相应的配置文件,这样来回修改,很容易出错,而且浪费劳动力。
       在前面的文章profile之springboot,springboot为我们提供了一种解决方案,而maven也提供了一种更加灵活的解决方案,就是profile功能。

1. 原理
1.1 先看一段pom文件中的profile定义

<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>

可以看到定义了多个profile,每个profile都有唯一的id,也包含properties属性。这里为每个profile都定义一个名为profiles.active的properties,每个环境的值不同。当我们打包项目时,激活不同的环境,profiles.active字段就会被赋予不同的值。
1.2 结合resource属性
       这个profiles.active字段可以应用到许多地方,及其灵活。可以在配置文件里被引用(参考此博客);也可以结合pom文件里的resource和filter属性,作为文件名的一部分或者文件夹名的一部分,下面会详细讲解这个用法。

注意:maven的profile用法有许多种,但基本原理就是根据激活环境的不同,自定义字段被赋予不同的值。

2. 应用演示
2.1 项目结构

这里定义了dev,prod,test三个文件夹,用来演示maven中profile的使用。注意,每个文件夹里还定义了application-{xxx}.properties件,这里相当于结合springboot的Profile的使用,是我比较推荐的方式,和本文maven的profile使用无关系,在application.properties都有spring.profiles.active=xxx去加载对应的application-{xxx}.properties。

pom文件里的关键配置为

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
</plugins>

<resources>
<resource>
<directory>src/main/resources/</directory>
<!--打包时先排除掉三个文件夹-->
<excludes>
<exclude>dev/*</exclude>
<exclude>prod/*</exclude>
<exclude>test/*</exclude>
</excludes>
<includes>
<!--如果有其他定义通用文件,需要包含进来-->
<!--<include>messages/*</include>-->
</includes>
</resource>
<resource>
<!--这里是关键! 根据不同的环境,把对应文件夹里的配置文件打包-->
<directory>src/main/resources/${profiles.active}</directory>
</resource>
</resources>
</build>

<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用来指定激活方式,可以根据jdk环境,环境变量,文件的存在或缺失-->
<activation>
<!--这个字段表示默认激活-->
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>test</id>
<properties>
<profiles.active>test</profiles.active>
</properties>
</profile>
</profiles>

可以看到我们利用resource属性来配置打包时,根据激活的环境来选取要打包的文件夹。我们使用maven命令

mvn clean package
1
prod环境被默认激活,打包后的包结构为

可以看到prod文件夹下的配置文件被打包进去,通过激活不同的profile也就实现了动态切换配置文件。

2.2 激活方式
profile的激活方式有很多种

1. 通过maven命令参数
即在使用maven打包时通过-P参数,-P后跟上profile的唯一id,如

mvn clean package -Ptest
1
打包时test的profile被激活,打包后的包结构为:

2. 通过pom文件里的activation属性
<profile>
<id>prod</id>
<properties>
<profiles.active>prod</profiles.active>
</properties>
<!--activation用来指定激活方式,可以根据jdk环境,环境变量,文件的存在或缺失-->
<activation>
<!--配置默认激活-->
<activeByDefault>true</activeByDefault>

<!--通过jdk版本-->
<!--当jdk环境版本为1.5时,此profile被激活-->
<jdk>1.5</jdk>
<!--当jdk环境版本1.5或以上时,此profile被激活-->
<jdk>[1.5,)</jdk>

<!--根据当前操作系统-->
<os>
<name>Windows XP</name>
<family>Windows</family>
<arch>x86</arch>
<version>5.1.2600</version>
</os>

<!--通过系统环境变量,name-value自定义-->
<property>
<name>env</name>
<value>test</value>
</property>

<!--通过文件的存在或缺失-->
<file>
<missing>target/generated-sources/axistools/wsdl2java/
com/companyname/group</missing>
<exists/>
</file>
</activation>
</profile>

这里我写了多种方式,可以通过activeByDefault、jdk版本、操作系统、系统环境变量(在win10我试了不成功,win7可以,不知道为啥)、文件的存在或缺失,实际项目可以根据需要选取一种即可。这种的优先级低于maven命令参数指定的方式。

3. settings.xml中使用activeProfiles指定(了解即可)
即mave目录下的settings.xml也可以添加下面的代码来指定激活哪个profile。

<activeProfiles>
<activeProfile>profileTest1</activeProfile>
</activeProfiles>
1
2
3
       值得注意的是1. setting.xml在当前系统用户的.m2文件夹有(如没有可手动拷贝过去也会生效),针对的当前用户的profile配置,在maven的安装目录下“conf/settings.xml”,针对的是全局的profile配置。2.profile也可以定义在setting.xml文件中,但是这种方式个人感觉并不实用的,不推荐。
————————————————
版权声明:本文为CSDN博主「余生之君」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/java_collect/article/details/83870215

maven中profile的使用的更多相关文章

  1. [转]Maven中profile和filtering实现多个环境下的属性过滤

    背景 项目构建的时候,需要根据不同的场景来改变项目中的属性资源,最为常见的莫过于数据库连接配置了,试想有生产环境.测试缓存.发布环境等,需要为不同的场景下来动态的改变数据库的连接配置.而使用maven ...

  2. maven中profile的激活方式

    1.默认激活 Maven给我们提供了多种不同的profile激活方式.比如我们可以使用-P参数显示的激活一个profile,也可以根据环境条件的设置让它自动激活等. <profile> & ...

  3. Maven中基于POM.xml的Profile来动态切换配置信息

    [转载:https://blog.csdn.net/blueheart20/article/details/52838093] 1. Maven中的profile设置 Maven是目前主流的项目代码结 ...

  4. gradle项目中profile的实现

    gradle中并没有直接类似maven中的profile支持,只能变通的用其它方法来处理,在打包不同环境的应用时,通常会遇到二类问题: 一.不同的环境依赖的jar包不同 拿web开发来说,生产环境一般 ...

  5. IntelliJ IDEA14.1中java项目Maven中没有配置JDK时的问题

    在IntelliJ IDEA 14.1中使用在java项目中使用Maven时当没有在Maven中配置JDK编译版本.源码版本时,IDEA将默认的编译版本.源码版本设置为jdk5. 在IDEA中Lang ...

  6. maven spring profile 协同

    请在网上查相关的使用情景,这里直接上要点.另外,可能不只一种方法,但这里只有一种. 1.POM.XML片段,使web.xml文件中有关活跃spring profile的内容可以被maven自动替换 & ...

  7. 在Maven中设置Nexus私有服务为中央工厂(repository)

    原文:http://blog.csdn.net/mexican_jacky/article/details/50275695 nexus中的仓库列表 第一种方式: <repositories&g ...

  8. Maven中settings.xml的配置项说明

    本文部分引用自:http://haohaoxuexi.iteye.com/blog/1827778 在Maven中提供了一个settings.xml文件来定义Maven的全局环境信息.这个文件会存在于 ...

  9. Maven学习-Profile详解

    Profile能让你为一个特殊的环境自定义一个特殊的构建:profile使得不同环境间构建的可移植性成为可能.Maven中的profile是一组可选的配置,可以用来设置或者覆盖配置默认值.有了prof ...

随机推荐

  1. 「模拟赛20191019」B 容斥原理+DP计数

    题目描述 将\(n\times n\)的网格黑白染色,使得不存在任意一行.任意一列.任意一条大对角线的所有格子同色,求方案数对\(998244353\)取模的结果. 输入 一行一个整数\(n\). 输 ...

  2. LeetCode 859. 亲密字符串(Buddy Strings) 23

    859. 亲密字符串 859. Buddy Strings 题目描述 给定两个由小写字母构成的字符串 A 和 B,只要我们可以通过交换 A 中的两个字母得到与 B 相等的结果,就返回 true:否则返 ...

  3. css3 网页图片轮播的实现

    .lunbo{ height: 640px; width: 100%; background-position: -280px; margin-top: 103px; -webkit-animatio ...

  4. epoll_ctl函数的使用

    #include <sys/epoll.h> int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event);作用: ...

  5. Shiro集成SSM基于动态URL权限管理(二)

    这个案例基于上一个demo扩展而来.所以数据库表,在Shiro集成SSM基于URL权限管理(一)开篇的一致.如果上个demo操作的建议重新导入一次,避免出现问题. 而这次都不是通过固定写在方法上的注解 ...

  6. tkinter学习笔记_04

    8.勾选项 checkbutton import tkinter as tk root = tk.Tk() root.title("xxx") root.geometry('200 ...

  7. [高清] Excel函数速记手册

    ------ 郑重声明 --------- 资源来自网络,纯粹共享交流, 如果喜欢,请您务必支持正版!! --------------------------------------------- 下 ...

  8. System.ComponentModel.Win32Exception (0x80004005): 无效的窗口句柄。

    原文:System.ComponentModel.Win32Exception (0x80004005): 无效的窗口句柄. 在 WPF 获取鼠标当前坐标的时候,可能会得到一个异常:System.Co ...

  9. PowerShell命令批量添加、导出AD用户

    导入单个AD用户命令 New-ADUser -Name "周八" -Surname "周" -GivenName "八"-SamAccoun ...

  10. php操作表格(写)

    一,转载:http://www.thinkphp.cn/extend/832.html 二,转载:http://m.blog.csdn.net/article/details?id=7827038