1.IDEA开发之子模块无法继承父模块的依赖

目录

[1.1 出现的问题](#1.1 出现的问题)

[1.2 父模块的Pom.xml配置文件](#1.2 父模块的Pom.xml配置文件)

[1.3 子模块的Pom.xml配置文件](#1.3 子模块的Pom.xml配置文件)

[1.4 思考:究竟哪里出现了问题?](#1.4 思考:究竟哪里出现了问题?)


1.1 出现的问题

在开发Spring引入数据库外部配置文件,发现我开发的父模块Spring以及子模块spring6-ioc-xml出现了无法实现依赖继承的情况,如图1所示

图1 子模块无法继承父模块

1.2 父模块的Pom.xml配置文件

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
    </properties>
    <groupId>com.haozihua</groupId>
    <artifactId>Spring6</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
<!--    <modules>-->
<!--        <module>spring6-ioc-xml</module>-->
<!--    </modules>-->
    <dependencies>
        <!--spring context依赖-->
        <!--当你引入Spring Context依赖之后,表示将Spring的基础依赖引入了-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>6.0.2</version>
        </dependency>

        <!--junit5测试-->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.6.3</version>
        </dependency>
        <!--log4j2的依赖-->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.19.0</version>
        </dependency>

        <!--spring context依赖-->
        <!--当你引入Spring Context依赖之后,表示将Spring的基础依赖引入了-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>6.0.3</version>
        </dependency>

        <!-- MySQL驱动 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.31</version>
        </dependency>

        <!-- 数据源 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.2.15</version>
        </dependency>
    </dependencies>
</project>

1.3 子模块的Pom.xml配置文件

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

<!--    <parent>-->
<!--        <groupId>com.haozihua</groupId>-->
<!--        <artifactId>Spring6</artifactId>-->
<!--        <version>1.0-SNAPSHOT</version>-->
<!--    </parent>-->
    <groupId>com.haozihua</groupId>
    <artifactId>spring6-ioc-xml</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
    </properties>


</project>

1.4 思考:究竟哪里出现了问题?

思路:既然Pom.xml文件负责Maven的配置,说明父模块和子模块一定出现了某些问题,没有联系,于是查询一番资料后,我发现

父模块需要使用

<modules>

<module>spring6-ioc-xml</module>

</modules>标签才能使得父模块和子模块称为父子关系,如图2所示

图2 加入了modules标签的Maven

看上去还是很正常的,对吗,但是依赖还没有被引入。

这是因为子模块也需要对应的标签引入父模块的依赖

比如接下来的标签

复制代码
<parent>
    <groupId>com.haozihua</groupId>
    <artifactId>Spring6</artifactId>
    <version>1.0-SNAPSHOT</version>
</parent>

这个标签非常重要,即使把父模块的标签删掉了,还能显示父子依赖,而且子模块能够引入父模块的依赖,非常重要

1.5 总结

在Maven开发中,当发现父子模块无法实现依赖继承,说明Pom.xml文件出现问题,此时,需要在子模块中添加如下标签

XML 复制代码
<parent>
<groupId>com.haozihua</groupId>
<artifactId>Spring6</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

仅仅是为了显示父子模块的依赖,而不需要引入父模块的依赖,只需要在父模块的Pom.xml文件中添加

XML 复制代码
<modules>
    <module>spring6-ioc-xml</module>
</modules>

如下标签即可。

依赖可以正常使用了!

相关推荐
IT_Octopus13 小时前
IntelliJ 本地日志路径自定义:`-DLOG_PATH=./logs` 为什么总“不听话“
java·log4j·intellij-idea
withoutfear18 小时前
IntelliJ IDEA卡顿内存分配不足和索引被频繁触发问题解决办法
java·ide·intellij-idea
sunshine22 girl2 天前
Java学习一 环境配置3 Idea中的maven仓库,pom.xml文件配置
学习·maven·intellij-idea
kqz20143 天前
Android Studio Commit 面板临时文件.gitignore
ide·android studio·intellij-idea
卓怡学长3 天前
w236基于springboot应用型本科院校共享在线考试系统
java·spring boot·spring·intellij-idea
程序员阿明4 天前
idea把插件啥的不默认放在C盘
java·ide·intellij-idea
LaughingZhu4 天前
Product Hunt 每日热榜 | 2026-09-08
java·ide·深度学习·百度·intellij-idea
李少兄4 天前
记一次 IDEA 打开 Vue 项目后目录“闪现即消失“ 问题排查
java·vue.js·intellij-idea
拽着尾巴的鱼儿4 天前
Idea:Cannot Run Git 无法运行git
java·git·intellij-idea
编程_大白4 天前
IDEA配置SQL方言
java·sql·intellij-idea