随笔 - 409  文章 - 0  评论 - 8  阅读 - 39万

maven多module项目的依赖管理

前言

这里主要说两种情况:

  1. 子module依赖父module
  2. 子module依赖子module

子module依赖父module

子module依赖父module又分为两种情况: dependencies 和 dependencyManagement

两种情况下子module都要在<parent>标签中依赖父module

dependencies

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

子项目会从父项目中继承dependencies中的所有依赖,即使在子项目中没有引入。

dependencyManagement

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <version>2.3.4.RELEASE</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

注意需要在父项目中指定版本,并且需要在子项目中显式的引用。

子项目pom:

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

如果子项目中没有指定版本,则按照父项目中指定的版本引入;如果子项目中指定了版本,则按照子项目中指定的版本引入。

子module依赖子module

假设B模块依赖A模块,则只需要在B的pom中引入A即可,不过需要注意一点,如果在A模块中引入了spring-boot-maven-plugin插件,则需要一点特殊的配置:

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <classifier>exec</classifier>
                </configuration>
            </plugin>
        </plugins>
    </build>

需要<configuration>中的配置的原因是:

默认情况下,maven会先打一个普通的jar包,然后如果引入了该插件,则该插件会将普通的jar包重命名为a.jar.original, 然后重新打一个可执行的jar包,命名为a.jar,但是可执行的jar包被依赖的话是会报错的。

为什么依赖可执行的jar包会报错?
我大致解压了一下两个jar包,其实里面的class的路径是不一样的。

posted on   斜月三星一太阳  阅读(1083)  评论(0编辑  收藏  举报
编辑推荐:
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
阅读排行:
· 手把手教你更优雅的享受 DeepSeek
· 腾讯元宝接入 DeepSeek R1 模型,支持深度思考 + 联网搜索,好用不卡机!
· AI工具推荐:领先的开源 AI 代码助手——Continue
· 探秘Transformer系列之(2)---总体架构
· V-Control:一个基于 .NET MAUI 的开箱即用的UI组件库
历史上的今天:
2019-10-10 一些好用的网站收集
< 2025年2月 >
26 27 28 29 30 31 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 1
2 3 4 5 6 7 8

点击右上角即可分享
微信分享提示