• 【Maven】使用maven profile 动态激活不同环境、依赖打包部署


    使用maven profile 动态激活不同环境、依赖打包部署

    前言

        在开发过程中,我们的软件会面对不同的运行环境,比如开发环境、测试环境、生产环境,而我们的软件在不同的环境中,有的配置可能会不一样,比如数据源配置、日志文件配置、以及一些软件运行过程中的基本配置,那每次我们将软件部署到不同的环境时,都需要修改相应的配置文件,这样来回修改,很容易出错,而且浪费劳动力。

    maven提供了一种方便的解决这种问题的方案,就是profile功能。
      
    Maven:

    profile可以让我们定义一系列的配置信息,然后指定其激活条件。这样我们就可以定义多个profile,然后每个profile对应不同的激活条件和配置信息,从而达到不同环境使用不同配置信息的效果。

    profile定义的位置

    (1) 针对于特定项目的profile配置我们可以定义在该项目的pom.xml中。(下面举例是这种方式)

    (2) 针对于特定用户的profile配置,我们可以在用户的settings.xml文件中定义profile。该文件在用户家目录下的“.m2”目录下。

    (3) 全局的profile配置。全局的profile是定义在Maven安装目录下的“conf/settings.xml”文件中的。

    一、配置

    假设场景:
         1、项目需要根据需求,使用不同配置或不同依赖。
         2、项目A依赖项目B,项目A仅使用项目B中部分模块,不想引入项目B全部依赖

    springboot项目B pom.xml:

    <profiles>
     <!-- 测试环境 -->
        <profile>
            <id>test</id>
            
            <activation>
            	<!-- 默认激活 -->
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
    
     <!-- 开发环境 -->
        <profile>
            <id>dev</id>
            <properties>
               <profiles.active>dev</profiles.active>
            </properties>
            <activation>
                <!-- 设置默认激活这个配置 -->
                <activeByDefault>true</activeByDefault>
            </activation>
            <dependencies>
                <!-- redisson starter -->
    	        <dependency>
    	            <groupId>org.redisson</groupId>
    	            <artifactId>redisson-spring-boot-starter</artifactId>
    	            <version>3.13.1</version>
    	            <!-- compile代表此依赖在各个阶段都能导入,会向下传递 -->
    	            <!-- 默认的scope就是compile,因此此处可以不写 -->
    	            <scope>compile</scope>
    	        </dependency>
            </dependencies>
        </profile>
        
        <profile>
            <!-- 发布生产环境 -->
             <id>prod</id>
             <properties>
                 <profiles.active>prod</profiles.active>
             </properties>
         </profile>
         <profile>
             <!-- 测试环境 -->
             <id>test</id>
             <properties>
                 <profiles.active>test</profiles.active>
             </properties>
         </profile>
    </profiles>
    
    <dependencies>
        <!-- redisson starter -->
        <dependency>
            <groupId>org.redisson</groupId>
            <artifactId>redisson-spring-boot-starter</artifactId>
            <version>3.13.1</version>
            <!-- provided代表此依赖只在当前项目导入,不会向下传递 -->
            <scope>provided</scope>
        </dependency>
    </dependencies>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61

    说明:
    1)两处redisson依赖说明:

        profiles标签下的依赖,只有当相应的profile被激活时,才会导入依赖。scope为compile,使得依赖本项目B的 项目A会导入该redisson依赖。
        dependencies标签下的依赖,给本项目B正常使用。scope使用provided,表示用于当前项目编译使用,不向下传递,使得依赖本项目B的 项目A不会导入该redisson依赖。
    参考:maven scope 的作用

    2)默认激活

    true 有该标签的profile默认被激活。但是激活等级最低,如其他profile被激活时,该默认激活会失效。

        这里定义了三个环境,分别是dev(开发环境)、test(测试环境)、prod(发布环境),其中开发环境是默认激活的(activeByDefault为true),这样如果在不指定profile时默认是开发环境,也在package的时候显示指定你要选择哪个开发环境,详情见激活介绍。

    针对不同的环境,我们定义了不同的配置文件,文件目录如下:
    在这里插入图片描述
    多个环境的配置文件,命名规则为是application-环境名称.yml 或者 application-环境名称.properties

    如图所示,开发环境、测试环境、生产环境的配置文件分别默认放到src/main/resources目录下。如果要在resources文件下再分类新目录下,如src/main/resources/config,那么就要在pom文件中作特别配置,如下:

    <build>
            <!-- 定义了变量配置文件的地址 -->
            <filters>
                <filter>src/main/resources/config/application-${env}.properties</filter>
            </filters>
            <resources>
                <resource>
                    <directory>src/main/resources</directory>
                    <filtering>true</filtering>
                </resource>
            </resources>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    二、激活Profile

    点此查看maven更多激活方式

    1、使用命令行激活:

    示例:

    • 编译:mvn compile -P dev
    • 打包:mvn package -P dev
    • 部署(本地仓库):mvn install -P dev
    • 发布(远程仓库):mvn deploy -P dev
    • 查看当前激活的profile:mvn help:active-profiles
    • 参数说明:
      -P [parameter]:-P可以同时多个参数,如mvn deploy -P test,dev,test和dev都是上面自定义的profile的id值。

    2、使用IDEA Maven插件激活
    1)勾选想要激活的profile,可以多选。
    2)点击Lifecycle下相应的选项。
    在这里插入图片描述

    三、动态依赖 示例

    1、项目B打包发布时激活id为dev的profile:mvn deploy -P dev

    (1)注意在pom.xml中加入spring-boot-maven-plugin打包插件,这样项目的依赖会一起打包在jar中,否则即使激活了dev的profile,该profile下的依赖并不会被项目A中使用到。

    (2)因为是发布到远程仓库,需要添加相关配置。这里的远程仓库是我本地搭建的一个nexus私服,具体如何搭建使用请自行搜索。

    <groupId>com.wy.springboot</groupId>
    <artifactId>demo</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    
    <!--设置发布仓库id要与setting中的账号密码所在id一致-->
    <distributionManagement>
        <repository>
            <id>nexus</id>
            <name>Nexus Sites</name>
            <url>http://localhost:8081/repository/maven-snapshots/</url>
        </repository>
    </distributionManagement>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    2、在项目A中引用项目B:

    <dependency>
        <groupId>com.wy.springboot</groupId>
        <artifactId>demo</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </dependency>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    3、查看项目A中所导入的依赖:
    (1)点击打开项目的依赖图:
    在这里插入图片描述
    (2)在依赖图中搜索redis,发现成功引入。
    在这里插入图片描述

    项目A、B pom
    (1) 项目B完整pom.xml

    <?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.4.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.wy.springboot</groupId>
        <artifactId>demo</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <name>demo</name>
        <description>Demo project for Spring Boot</description>
    
        <properties>
            <java.version>1.8</java.version>
        </properties>
    
        <profiles>
    	    <profile>
    	        <id>test</id>
    	        <activation>
    	        	<!-- 默认激活 -->
    	            <activeByDefault>true</activeByDefault>
    	        </activation>
    	    </profile>
    	
    	    <profile>
    	        <id>dev</id>
    	        <dependencies>
    	            <!-- redisson starter -->
    		        <dependency>
    		            <groupId>org.redisson</groupId>
    		            <artifactId>redisson-spring-boot-starter</artifactId>
    		            <version>3.13.1</version>
    		            <!-- compile代表此依赖在各个阶段都能导入,会向下传递 -->
    		            <!-- 默认的scope就是compile,因此此处可以不写 -->
    		            <scope>compile</scope>
    		        </dependency>
    	        </dependencies>
    	    </profile>
    	</profiles>
    	
    	<dependencies>
    	    <!-- redisson starter -->
    	    <dependency>
    	        <groupId>org.redisson</groupId>
    	        <artifactId>redisson-spring-boot-starter</artifactId>
    	        <version>3.13.1</version>
    	        <!-- provided代表此依赖只在当前项目导入,不会向下传递 -->
    	        <scope>provided</scope>
    	    </dependency>
    	</dependencies>
    
        <!--设置发布仓库id要与setting中的账号密码所在id一致-->
        <distributionManagement>
            <repository>
                <id>nexus</id>
                <name>Nexus Sites</name>
                <url>http://localhost:8081/repository/maven-snapshots/</url>
            </repository>
        </distributionManagement>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76

    (2)项目A完整pom.xml

    <?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.4.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.wuyou.springboot</groupId>
        <artifactId>demo2</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <name>demo2</name>
        <description>Demo project for Spring Boot</description>
    
        <properties>
            <java.version>1.8</java.version>
        </properties>
    
        <repositories>
        	<!-- 用于拉取自己远程仓库依赖的配置 -->
            <repository>
                <id>nexus</id>
                <name>nexus</name>
                <url>http://localhost:8081/repository/maven-snapshots/</url>
                <releases>
                    <enabled>true</enabled>
                </releases>
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
            </repository>
        </repositories>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
                <exclusions>
                    <exclusion>
                        <groupId>org.junit.vintage</groupId>
                        <artifactId>junit-vintage-engine</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
    
            <dependency>
                <groupId>com.wy.springboot</groupId>
                <artifactId>demo</artifactId>
                <version>1.0.0-SNAPSHOT</version>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72

    一些其他参考:

    1.【IDEA】Maven踩坑:pom文件中的默认profiles不生效+IDEA中Maven的profiles使用说明
    2.pom.xml中maven profile的激活方式

  • 相关阅读:
    如何使用 arrayList.removeAll(Collection<?> c)?
    LVS+Keepalived+nfs 集群部署及实验
    基于YOLOv8的手机摄像头的自动检测系统
    [python 刷题] 875 Koko Eating Bananas
    基于C8051F380的流水灯设计
    民安智库(第三方满意度调研公司)购物商场消费者满意度研究报告
    电子宣传册制作攻略,打造完美视觉效果
    设计原则与思想:面向对象
    架构师如何做好需求分析
    python type hint
  • 原文地址:https://blog.csdn.net/weixin_43431218/article/details/127896887