Maven profile 粗谈
profile作用
- 允许Maven根据不同的环境激活不同的profile来采用不同的配置, 可以实现多环境配置文件的动态切换
- 当然也可以同时激活多个profile
profile子标签
- 首先我们看下pom.xml中profile可承载的子标签有哪些
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<profiles>
<profile>
<!--profile标识符-->
<id>dev</id>
<!--激活的条件逻辑-->
<activation>
<!--指定该profile默认激活-->
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<!--profiles.active是自定义的字段(名字随便起),自定义字段可以有多个-->
<profiles.active>dev</profiles.active>
<testname>sigmund</testname>
</properties>
<dependencies></dependencies>
<dependencyManagement></dependencyManagement>
<distributionManagement></distributionManagement>
<build></build>
<modules></modules>
<reports></reports>
<reporting></reporting>
<repositories></repositories>
<pluginRepositories></pluginRepositories>
</profile>
</profiles>
</project>
前置条件
<?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.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.diy.sigmund</groupId>
<artifactId>mybatis-oracle</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>mybatis-oracle</name>
<description>Demo project for Spring Boot</description>
...
测试dependency
<profiles>
<profile>
<id>test1</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<dependencies>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
</dependencies>
</profile>
<profile>
<id>test2</id>
<dependencies>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>
</dependencies>
</profile>
</profiles>
-
执行
clean install -D maven.test.skip=true -P test1
可以看到mybatis-oracle-0.0.1-SNAPSHOT.war里面WEB-INF\lib
commons-fileupload版本是1.3.3
-
执行
clean install -D maven.test.skip=true -P test2
可以看到mybatis-oracle-0.0.1-SNAPSHOT.war里面WEB-INF\lib
commons-fileupload版本是1.4
-
执行
clean install -D maven.test.skip=true
可以看到mybatis-oracle-0.0.1-SNAPSHOT.war里面WEB-INF\lib
commons-fileupload版本是1.3.3
- 因为test1的profile定义了标签
<activeByDefault>true</activeByDefault>
- 因为test1的profile定义了标签
-
执行
clean install -D maven.test.skip=true -P !test1
可以看到mybatis-oracle-0.0.1-SNAPSHOT.war里面WEB-INF\lib
没有commons-fileupload
-D maven.test.skip=true 表示打包时跳过单元测试
在profile前使用
!
符号,表示不激活该profile
IDEA使用Before launch功能的坑
-
注意看View | Tool Windows | Maven 小窗口
-
当你在pom.xml中定义了profile后,该Maven窗口会读取配置并显示你的profile到Profiles文件夹下
-
在2019.2商业版中profile会有3种状态,勾选(亮色)状态,勾选(暗色)状态,未勾选状态,修改状态需要
import Changes
-
这几种状态均会影响IDEA使用Before launch功能
勾选(亮色)状态:表示是你主动勾选的
勾选(暗色)状态:表示是默认激活的,配置了标签
<activeByDefault>true</activeByDefault>
未勾选状态:表示是你主动取消勾选的
举例-2019.2商业版
有一个Spring Boot运行配置MybatisOracleApplication
在Before launch添加了clean install -D maven.test.skip=true
- test1,test2均不勾选,窗口显示
clean install -D maven.test.skip=true -P !test1
- 仅勾选test1,窗口显示
clean install -D maven.test.skip=true -P test1
- 仅勾选test2,窗口显示
clean install -D maven.test.skip=true -P test2,!test1
- test1,test2均勾选,窗口显示
clean install -D maven.test.skip=true -P test1,test2