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

一、需求

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

1)两处redisson依赖说明:

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

2)默认激活:
<activeByDefault>true</activeByDefault> 有该标签的profile默认被激活。但是激活等级最低,如其他profile被激活时,该默认激活会失效。

三、激活profile

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下相应的选项。
在这里插入图片描述
相关区别参考地址:

3、更多激活profile的方式:

四、示例

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>

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

<dependency>
    <groupId>com.wy.springboot</groupId>
    <artifactId>demo</artifactId>
    <version>1.0.0-SNAPSHOT</version>
</dependency>

3、查看项目A中所导入的依赖:
(1)点击打开项目的依赖图:
在这里插入图片描述
(2)在依赖图中搜索redis,发现成功引入。
在这里插入图片描述
至此,示例演示结束。当然,profile下不仅可以使用不同依赖,还可以做其他配置。
可以参考: maven profile动态选择配置文件

4、相关配置:
(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>

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

五、参考地址

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值