Maven profile 粗谈

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>
  1. 执行clean install -D maven.test.skip=true -P test1

    可以看到mybatis-oracle-0.0.1-SNAPSHOT.war里面WEB-INF\lib

    commons-fileupload版本是1.3.3

  2. 执行clean install -D maven.test.skip=true -P test2

    可以看到mybatis-oracle-0.0.1-SNAPSHOT.war里面WEB-INF\lib

    commons-fileupload版本是1.4

  3. 执行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>
  4. 执行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功能的坑

  1. 注意看View | Tool Windows | Maven 小窗口

  2. 当你在pom.xml中定义了profile后,该Maven窗口会读取配置并显示你的profile到Profiles文件夹下

  3. 在2019.2商业版中profile会有3种状态,勾选(亮色)状态,勾选(暗色)状态,未勾选状态,修改状态需要import Changes

  4. 这几种状态均会影响IDEA使用Before launch功能

    勾选(亮色)状态:表示是你主动勾选的

    勾选(暗色)状态:表示是默认激活的,配置了标签<activeByDefault>true</activeByDefault>

    未勾选状态:表示是你主动取消勾选的

举例-2019.2商业版

有一个Spring Boot运行配置MybatisOracleApplication

在Before launch添加了clean install -D maven.test.skip=true

  1. test1,test2均不勾选,窗口显示clean install -D maven.test.skip=true -P !test1
  2. 仅勾选test1,窗口显示clean install -D maven.test.skip=true -P test1
  3. 仅勾选test2,窗口显示clean install -D maven.test.skip=true -P test2,!test1
  4. test1,test2均勾选,窗口显示clean install -D maven.test.skip=true -P test1,test2
资料参考
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值