欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 健康 > 美食 > java_dependencies_父子模块引入依赖关系

java_dependencies_父子模块引入依赖关系

2025/6/13 21:43:11 来源:https://blog.csdn.net/shyjhyp11/article/details/148511323  浏览:    关键词:java_dependencies_父子模块引入依赖关系

问:
父模块中使用了 spring-boot-starter-web, 它的子模块,还需要引入吗?

答:
是否需要在子模块中 重复引入 spring-boot-starter-web,要根据实际使用方式来判断。下面是详细说明:


✅ 一、继承了父模块且不自定义依赖 —— 不用再引入

如果你的子模块的 pom.xml

  • 通过 <parent> 显式继承了父模块,
  • 且不使用 <dependencyManagement> 而是直接在父模块中声明了 <dependencies>

👉 那么:子模块默认会继承依赖可以不重复引入

示例:

🔹 父模块 pom.xml

<project><groupId>com.example</groupId><artifactId>parent</artifactId><version>1.0</version><packaging>pom</packaging><modules><module>module-a</module></modules><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency></dependencies>
</project>

🔹 子模块 module-a/pom.xml

<project><parent><groupId>com.example</groupId><artifactId>parent</artifactId><version>1.0</version></parent><artifactId>module-a</artifactId>
</project>

这种情况下不需要重复引入 spring-boot-starter-web


⚠️ 二、父模块用了 <dependencyManagement> —— 子模块需要显式引入依赖

如果父模块的 pom.xml 只在 <dependencyManagement> 中做了版本统一管理:

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

那子模块就必须手动引入依赖(但不需要再写 version)

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

✅ 三、推荐做法

父模块是否包含依赖方式子模块是否需要引入?推荐
<dependencies>❌ 不需要⭐ 简单直观
<dependencyManagement>✅ 需要引入 artifactId更灵活,推荐中大型项目使用

如你项目是 Spring Boot 多模块项目,推荐将公共依赖放到父模块 <dependencies> 中,简化子模块维护。

如果你不确定你的继承方式,可以贴上父子 pom.xml 的关键部分,我可以直接帮你判断。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词