0
点赞
收藏
分享

微信扫一扫

maven--profile--根据环境引入依赖(动态引入依赖)--方法/实例



简介

说明

        本文用示例介绍maven如何通过profile来根据环境引入依赖(动态引入依赖)。

需求

        让pom.xml支持两种环境:若是test环境,则不引入knife4j依赖;若是dev环境,则引入knife4j依赖。

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.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.knife</groupId>
<artifactId>demo_SpringBoot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo_SpringBoot</name>
<description>Demo project for Spring Boot</description>

<profiles>
<profile>
<id>test</id>
<activation>
<!-- 默认激活 -->
<activeByDefault>true</activeByDefault>
</activation>
</profile>

<profile>
<id>dev</id>
<dependencies>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
</dependencies>
</profile>
</profiles>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>

Idea中的显示

在侧边栏的“Maven”可以看到两个环境,默认是test

maven--profile--根据环境引入依赖(动态引入依赖)--方法/实例_maven

测试

测试1:默认的test环境

更新maven

maven--profile--根据环境引入依赖(动态引入依赖)--方法/实例_spring_02

可以发现,没有引入knife4j

测试2:使用dev环境

更新maven

maven--profile--根据环境引入依赖(动态引入依赖)--方法/实例_spring_03

可以发现,引入了knife4j


举报

相关推荐

0 条评论