「 從0到1學習微服務SpringCloud 」09 補充篇-maven父子模塊項目

系列文章(更新ing):

「 從0到1學習微服務SpringCloud 」06 統一配置中心Spring Cloud Config
「 從0到1學習微服務SpringCloud 」07 RabbitMq的基本使用
「 從0到1學習微服務SpringCloud 」08 構建消息驅動微服務的框架 Spring Cloud Streamjava

做爲微服務的項目,若是將每一個項目都拆成一個完整的項目,很難開發,那得打開多少個idea。應該將它們歸到一個項目裏,使用maven父子模塊項目的形式,以下圖git

image

以前咱們項目的拆成單個是錯誤,不過問題不大,能夠將它們再合起來。github

maven父子模塊項目

1.新建一個maven項目,做爲父項目,把多餘的東西刪掉,只留下.idea和pom.xmlweb

2.將現有的項目做爲子模塊加到項目裏spring

image

3.剛加進來的項目會與父項目處於同級,將子模塊拖到父項目裏框架

image

4.構建父子模塊
主要是經過modules和parent標籤實現的
1.在父項目的pom中添加modules標籤,將子模塊加進來maven

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

2.在子模塊的pom中添加parent標籤,並加入父項目的座標ide

<parent>
        <!-- 如下這些東西被稱爲 座標 -->
        <groupId>com.zhangwk</groupId>
        <artifactId>springcloud-parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
</parent>

3.一些共用的東西能夠放在父項目的pom文件中,由於子項目的pom會繼承父項目的pomspring-boot

(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步,將咱們以前的項目都加進來吧

image

已將代碼上傳到github

https://github.com/zhangwenka...

若是以爲不錯,分享給你的朋友!

image

image

 THANDKS

  • End -

一個立志成大腿而天天努力奮鬥的年輕人

伴學習伴成長,成長之路你並不孤單!

掃描二維碼,關注公衆號

相關文章
相關標籤/搜索