idea maven dependencies报红_「 从0到1学习微服务SpringCloud 」09 补充篇-maven父子模块项目...

2b1efc7fecf05db26d2a33c9f7afff50.png

系列文章(更新ing):

「 从0到1学习微服务SpringCloud 」06 统一配置中心Spring Cloud Config 「 从0到1学习微服务SpringCloud 」07 RabbitMq的基本使用 「 从0到1学习微服务SpringCloud 」08 构建消息驱动微服务的框架 Spring Cloud Stream

作为微服务的项目,如果将每个项目都拆成一个完整的项目,很难开发,那得打开多少个idea。应该将它们归到一个项目里,使用maven父子模块项目的形式,如下图

6ee0a78c36209cb73c73d396bb95fd33.png

之前我们项目的拆成单个是错误,不过问题不大,可以将它们再合起来。

maven父子模块项目

1.新建一个maven项目,作为父项目,把多余的东西删掉,只留下.idea和pom.xml

2.将现有的项目作为子模块加到项目里

abcd1b86d91a7d045da9a09946abd463.png

3.刚加进来的项目会与父项目处于同级,将子模块拖到父项目里

f4e1b8b5d789e75509739a5292123d3f.png

4.构建父子模块 主要是通过modules和parent标签实现的 1.在父项目的pom中添加modules标签,将子模块加进来

<modules>
        <module>eureka-server</module>
 </modules>

2.在子模块的pom中添加parent标签,并加入父项目的坐标

<parent>
        <!-- 以下这些东西被称为 坐标 -->
        <groupId>com.zhangwk</groupId>
        <artifactId>springcloud-parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
</parent>

3.一些共用的东西可以放在父项目的pom文件中,因为子项目的pom会继承父项目的pom

(1)将子项目中的properties标签移到父项目中,从而做到jar包的统一版本管理

<properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
</properties>

(2)添加以下标签,用于管理springcloud相关依赖的版本管理

<!-- 子模块只继承版本,需要声明依赖 -->    
<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

<!-- 子模块可以完全继承 -->    
<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.20</version>
            <scope>provided</scope>
        </dependency>

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

这里需要注意的是,如果使用了dependencyManagement标签,里面的依赖只能用于声明jar的版本,在子模块中需要再声明一次该依赖,无需声明版本。

若只存在dependencies标签,子模块可以继承里面的依赖,无需再次声明。

4.第一个子模块就加进来了,重复以上1,2,3步,将我们之前的项目都加进来吧

61baf7ff79234e47d82fd3590eefc224.png

已将代码上传到github

https://github.com/zhangwenkang0/springcloud-learning-from-0-to-1

如果觉得不错,分享给你的朋友!

ab134bf44da70ff01f9c5af38bae7cac.png

f21dcbebfd0615ffe017d0a3e5d2e504.gif

THANDKS

  • End -

一个立志成大腿而每天努力奋斗的年轻人

伴学习伴成长,成长之路你并不孤单!

f4f9d42fec3175ed232969f3e5245b65.png
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值