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

本文介绍了如何在Maven的pom.xml中使用profile根据环境(test或dev)动态引入依赖。当处于test环境时,不引入knife4j依赖;而切换到dev环境时,则会引入该依赖。在IDEA中,可以通过Maven面板观察并切换环境来验证依赖的引入情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文网址:maven--profile--根据环境引入依赖(动态引入依赖)--方法/实例_IT利刃出鞘的博客-CSDN博客

简介

说明

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

需求

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

技术资料与交流 ==> 分享靠谱资料:Java真实面试题汇总、简历模板、PDF书籍、PPT模板等;分享Java实用技术;帮助优化简历;。详见:资料与交流

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

测试

测试1:默认的test环境

更新maven

可以发现,没有引入knife4j

测试2:使用dev环境

更新maven

可以发现,引入了knife4j

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT利刃出鞘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值