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>

如下标签即可。

依赖可以正常使用了!

相关推荐
YANshangqian1 小时前
DISMTools(镜像管理工具)
intellij-idea
计算机毕设指导61 小时前
基于微信小程序的校园物品租赁与二手交易系统【源码文末联系】
spring boot·mysql·微信小程序·小程序·tomcat·maven·intellij-idea
2501_916766546 小时前
idea多模块项目运行设置
java·intellij-idea
weixin_462446236 小时前
【原创实践】使用 Docker 部署 IntelliJ IDEA(linuxserver/intellij-idea)完整指南
docker·容器·intellij-idea
想做后端的小C6 小时前
Mac 环境下设置 idea 中的 tomcat 运行配置
macos·tomcat·intellij-idea
kong790692810 小时前
Java-Intellij IDEA 自动导包设置
java·ide·intellij-idea
shoubepatien15 小时前
JAVA -- 08
java·后端·intellij-idea
青木川崎18 小时前
Mac使用idea连接svn报错svn: E230001: Server SSL certificate verification failed
svn·intellij-idea·ssl
AA陈超21 小时前
JetBrains Rider ‘IntelliJ‘ 快捷键映射表
ide·intellij-idea·idea·intellij idea
shoubepatien21 小时前
JAVA -- 07
java·后端·intellij-idea