maven多模块项目中,dependencyManagement引发的疑难杂症

起因

搭建好多模块的项目后,在父模块和子模块都添加了依赖,但是侧边栏依然没有这个

依赖并没有生效。

经过一番尝试后,发现各种离奇结果产生的原因有两个:

一是对dependencyManagement的理解不够深刻,而是idea的缓存可能会带来一些不可预期的结果。

dependencyManagement

dependecyManagement通常加在父项目上,用于依赖版本管理,但他只是定义依赖的版本,并不会真正的引入依赖。

子模块需要用到某个依赖的时候,就不需要再加版本了,这样一来就统一了子模块的依赖版本。

至于为啥子模块明明添加了依赖,却并没有依赖被引入,可能是因为我们的引入的依赖只是个空壳子。

例如,我们在父模块定义了依赖的版本,子模块引入依赖

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

发现并没有实质性的依赖被引入,原因在于这个依赖并不是真正的依赖,如下所示:

它是一个dependencyManagement,应该被用于父模块定义版本,不可以在子模块中使用

SpringBoot本身的代码放在了这个starter里面,我们应该引入的是这个starter。

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
                <version>2.3.6.RELEASE</version>
            </dependency>

所以在父模块应该加上上述依赖,子模块只需要引入starter即可。

所以最后父模块的部份依赖是

 <properties>
        <spring-boot.version>2.3.6.RELEASE</spring-boot.version>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <maven.resource.plugin>3.1.0</maven.resource.plugin>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.jar.plugin>3.2.0</maven.jar.plugin>
    </properties>

    <dependencyManagement>
        <dependencies>

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
                <version>${spring-boot.version}</version>
            </dependency>

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
            </dependency>

        </dependencies>
    </dependencyManagement>

子模块

<properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <maven.jar.plugin>3.2.0</maven.jar.plugin>
    </properties>

    <dependencies>
<!--        springboot-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

<!--        springboot test -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>

idea缓存带来的一些问题

亲测多模块的项目中,不管是微服务还是单体项目,只要使用了dependencyManagement,就有可能出现依赖更新失效的状况。

比如加了一个依赖后,刷新一下,侧边栏空空的,迟迟加不上。

去掉一个依赖后,侧边栏显示这个依赖还在。

如果确定依赖配置没有问题,可以关闭项目,删掉idea产生的所有临时文件

重启idea再看结果。

 

不过大部分情况应该是依赖导入的有问题,

如果子项目添加的依赖不生效,可以先检查一下添加的是不是是空壳子。

 

 

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
MavendependencyManagement元素用于集管理项目的依赖版本号,它可以确保所有子模块使用相同的依赖版本。但是,dependencyManagement只是声明依赖的版本,并不会实际引入依赖。因此,它不能透传依赖给子模块。 子模块需要显式地声明依赖,并指定版本号。如果子模块没有指定版本号,那么它将使用dependencyManagement声明的版本号。这样可以确保所有子模块使用相同的依赖版本,但是子模块仍然需要显式地声明依赖。 下面是一个示例,展示了如何在Maven项目使用dependencyManagement和透传依赖给子模块: ```xml <!-- 父模块的pom.xml --> <dependencyManagement> <dependencies> <dependency> <groupId>com.example</groupId> <artifactId>dependency1</artifactId> <version>1.0.0</version> </dependency> <dependency> <groupId>com.example</groupId> <artifactId>dependency2</artifactId> <version>2.0.0</version> </dependency> </dependencies> </dependencyManagement> <!-- 子模块的pom.xml --> <dependencies> <dependency> <groupId>com.example</groupId> <artifactId>dependency1</artifactId> </dependency> <dependency> <groupId>com.example</groupId> <artifactId>dependency2</artifactId> <version>2.0.0</version> </dependency> </dependencies> ``` 在上面的示例,父模块dependencyManagement声明了dependency1和dependency2的版本号。子模块只声明了dependency1,而dependency2使用了dependencyManagement声明的版本号。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值