原文网址:maven--profile--使用/教程/示例_IT利刃出鞘的博客-CSDN博客
简介
说明
本文用示例说明maven的profile的使用。
mavan的profile作用
Maven中的profile能让你为不同的环境定义不同的构建;
Maven中的profile是一组可选的配置,可以用来设置或者覆盖配置默认值。
一个Profiles下面允许出现的元素
<project>
<profiles>
<profile>
<id>...</id>
<activation>...</activation>
<properties>...</properties>
<build>
<defaultGoal>...</defaultGoal>
<finalName>...</finalName>
<resources>...</resources>
<testResources>...</testResources>
<plugins>...</plugins>
</build>
<reporting>...</reporting>
<modules>...</modules>
<dependencies>...</dependencies>
<dependencyManagement>...</dependencyManagement>
<distributionManagement>...</distributionManagement>
<repositories>...</repositories>
<pluginRepositories>...</pluginRepositories>
</profile>
</profiles>
</project>
profile的位置
可以在以下位置声明profile:
- pom.xml:很显然,pom.xml中声明的profile只对当前项目有效。
- 用户settings.xml:用户目录下.m2/settings.xml中的profile对本机上该用户所有的Maven项目有效。
- 全局settings.xml:Maven安装目录下conf/settings.xml中的profile对本机上所有的Maven项目有效。
- profiles.xml(Maven 2):还可以在项目根目录下使用一个额外的profiles.xml文件来声明profile,不过该特性已经在Maven 3中被移除。建议用户将这类profile移到settings.xml中。
激活profile的方式
激活方式是有优先级的,优先级由高到低如下:
- 命令
- 默认项
命令激活
通过maven 的-P参数激活指定的profile,参数的值是profile的id,多个profile以逗号分割,如果不想激活某个默认的profile,就在它的id前加个!
示例:mvn clean package -Dmaven.test.skip=true -Ptest,dev,!prod
说明:-Dmaven.test.skip=true 表示跳过测试,可提高打包速度
隐式激活
不同类型的隐式激活方式可以组合使用,如根据同时指定根据操作系统类型和JDK版本来激活profile,只有但两个条件都匹配是才激活之。
默认激活
<profiles>
<profile>
<id>profileTest1</id>
<properties>
<hello>world</hello>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>profileTest2</id>
<properties>
<hello>andy</hello>
</properties>
</profile>
</profiles>
可以在profile中的activation元素中指定激活条件,当没有指定条件,然后指定activeByDefault为true的时候就表示当没有指定其他profile为激活状态时,该profile就默认会被激活。
所以当我们调用mvn package的时候上面的profileTest1将会被激活,但是当我们使用mvn package –P profileTest2的时候将激活profileTest2,而这个时候profileTest1将不会被激活。
根据环境激活
根据操作系统激活
<profiles>
<profile>
<activation>
<os>
<!-- 不必指定所有信息 -->
<name>linux</name>
<family>unix</family>
<arch>amd64</arch>
<version>3.19.0-30-generic</version>
</os>
</activation>
</profile>
</profiles>
关于OS值的更多信息可以参考:官网
根据JDK版本激活
<!-- 如果jdk的版本为1.8则激活该profile -->
<profiles>
<profile>
<activation>
<jdk>1.8</jdk>
</activation>
</profile>
</profiles>
也可以通过[1.6,1.8)匹配多个jdk版本,关于匹配模式的详细信息可以参考:官网
根据环境变量激活
<!-- 如果环境变量里有`debug`且它的值为`true`则激活 -->
<!-- 也可以不指定`<value>`元素, 则只有环境变量里有`debug`就激活 -->
<profiles>
<profile>
<activation>
<property>
<name>debug</name>
<value>true</value>
</property>
</activation>
</profile>
</profiles>
mvn -U clean package -Ddebug=true
通过判断文件是否存在激活
<profiles>
<profile>
<activation>
<file>
<missing>/path/to/missing/file</missing>
<exists>/path/to/exists/file</exists>
</file>
</activation>
</profile>
</profiles>
与SpringBoot整合
简介
maven的profile与SpringBoot的profile的区别
SpringBoot的profile
- 作用:运行的时候,把哪几个配置包含进来。
- 如果有多个环境,需要在运行jar包的时候通过参数来指定,比如:java -jar test-1.0.0-SNAPSHOT.jar --spring.profiles.active=dev
maven的profile
- 作用:
- 指定打包的时候只打包哪个或哪几个环境
- maven是打包的工具,它的profile可以指定打包的时候只打包哪个或哪几个环境。例如:我只想打包test,就只把resources下边test的配置打包进去。
- 因为profile下边可以写dependencies等标签,所以可以在不同环境中使用不同的依赖。
- 比如:开发了个新功能,这个新功能是在自己的包里添加的(存放在maven私库中)则在开发对应的profile中升级此依赖的版本;等开发结束后,将测试对应的profile也升级此依赖的版本;测试通过后,则将生产对应的profile也升级此依赖的版本。
- 指定打包的时候只打包哪个或哪几个环境
- 运行jar包的时候就不需要指定参数了。
实例
pom.xml
配置如下两项:
- profiles节点
- build -> resources节点
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo_SpringBoot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo_SpringBoot</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<!--配置多环境打包-->
<profiles>
<!--开发环境-->
<profile>
<id>dev</id>
<properties>
<!--自定义的属性-->
<profile.active>dev</profile.active>
</properties>
<activation>
<!--如果不指定,则默认使用dev开发环境配置-->
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<!-- 测试环境-->
<profile>
<id>test</id>
<properties>
<profile.active>test</profile.active>
</properties>
</profile>
<!--生产环境-->
<profile>
<id>prod</id>
<properties>
<profile.active>prod</profile.active>
</properties>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<!-- profile对资源的操作 -->
<resources>
<!--打包的时候先去掉所有的配置文件-->
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>application*.yml</exclude>
</excludes>
</resource>
<!--添加需要包含的文件-->
<resource>
<directory>src/main/resources</directory>
<!-- 是否替换yml或者properties里@xx@表示的maven properties属性值 -->
<filtering>true</filtering>
<!--在打包的时候,根据-P参数,加上需要的yml配置文件-->
<includes>
<include>application.yml</include>
<include>application-${profile.active}.yml</include>
<!--<include>**/application-${profile.active}.yml</include>-->
</includes>
</resource>
</resources>
</build>
</project>
这样配置之后,Idea的maven可以展示出profiles选项:
application.yml
spring:
profiles:
active: @profile.active@
.yml或者.properties通过@xxx@的方式引入pom.xml里边的内容。
比如,如果上一步中箭头处选择了test,则本处spring.profiles.active就是test
applicaiton-xxx.yml
里边可以先不写东西。
测试
选择prod,然后打包:
结果:(确实只将相应的配置文件打包进来了)
查看demo_SpringBoot-0.0.1-SNAPSHOT.jar
总结
- 用Idea先选profiles(比如test),然后再打包,相当于执行命令:mvn package -Ptest
- 本处如果我在Idea里边把profiles留空,什么都不选,打包之后的配置文件只有application.yml。按理说,我已经将dev作为默认配置了,应该有applicaiton.yml和application-dev.yml才对,本处暂没有解决。
其他网址
使用 Maven Profile 和 Filtering 打各种环境的包 - SegmentFault 思否
Maven学习总结(49)——Maven Profile详解_科技D人生-CSDN博客
Maven profile介绍_winwill2012的博客-CSDN博客
其他网址
SpringBoot不同profile过滤配置文件_JonathanHsia-CSDN博客
maven profile 和 spring boot profile的区别_看写写-CSDN博客
maven的profile标签的使用_HELLOW,文浩-CSDN博客_maven的profile标签