SpringCloud之整體聚合父工程Project
1.New Project


- 創建父工程pom檔案報錯

- 解決方法為匯入依賴,暫時不用做這一步,下面還會涉及到
<!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-project-info-reports-plugin -->
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.1.1</version>
</dependency>
2.字符編碼

3.注解生效激活

4.java編譯版本選8

5.File Type過濾


6.刪掉src

7.跳過單元測驗

8.指定專案打包型別
專案的打包型別有三種:
pom、jar、war
- 這里指定為
pom
<!--這個pom檔案作為父pom檔案,起到依賴版本控制的作用,其他module工程繼承該pom,-->
<packaging>pom</packaging>
9.Maven工程落地細節
這里一邊介紹
Maven中的DependencyManagement和Dependencies關系,一邊完善pom檔案的撰寫
簡要介紹
Maven使用dependencyManagement元素來提供了一種管理依賴版本號的方式,通常會在一個組織或者專案的最頂層的父POM中看到dependencyManagement元素,- 使用
pom.xml中的dependencyManagement元素能讓所有在子專案中參考一個依賴而不用顯式的列出版本號,Maven會沿著父子層次向上走,直到找到一個擁有dependencyManagement元素的專案,然后它就會使用這個dependencyManagement元素中指定的版本號,
示例
- 首先可以插入
properties節點,在下面指定整個工程需要使用到的jar包版本,(當然也可以直接在Dependence節點中直接指定版本號,相比之下,使用properties能夠更加清晰的管理jar包版本)
<!--統一管理jar包版本-->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<junit.version>4.13.1</junit.version>
<lombok.version>1.18.18</lombok.version>
<log4j.version>1.2.17</log4j.version>
<mysql.version>5.1.49</mysql.version>
<druid.version>1.2.4</druid.version>
<mybatis.spring.boot.version>2.1.4</mybatis.spring.boot.version>
</properties>
- 在
DependencyManagement節點添加所需依賴坐標,這里只是一個示例,
<!--jar包的總管,子模塊繼承之后,提供作用:鎖定版本+子module不用寫groupId和version-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis.spring.boot.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
- 在子專案中的
pom檔案填寫依賴,可以看到,不需要填寫version標簽
<!--junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
好處
- 如果有多個子專案都參考同一樣依賴,則可以避免在每個使用的子專案里都宣告一個版本號,這樣當想升級或切換到另一個版本時,只需要在頂層父容器里更新,而不需要一個一個子專案的修改;另外如果某個子專案需要另外的一個版本,只需要宣告
version就可,也就是"一處修改,處處報錯"(手動滑稽)
注意
dependencyManagement里只是宣告依賴,并不實作引入,因此子專案需要顯示的宣告需要用的依賴,- 如果不在子專案中宣告依賴,是不會從父項自中繼承下來的,只有在子專案中寫了該依賴項,并且沒有指定具體版本,才會從父專案中繼承該項,并且
version和scope都讀取自父pom, - 如果子專案中指定了版本號,那么會使用子專案中指定的
jar版本,
完整代碼
<?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>
<groupId>com.phz.springcloud</groupId>
<artifactId>SpringCloudDemo</artifactId>
<version>1.0-SNAPSHOT</version>
<!--這個pom檔案作為父pom檔案,起到依賴版本控制的作用,其他module工程繼承該pom,-->
<packaging>pom</packaging>
<!--統一管理jar包版本-->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<junit.version>4.13.1</junit.version>
<lombok.version>1.18.18</lombok.version>
<log4j.version>1.2.17</log4j.version>
<mysql.version>5.7.31</mysql.version>
<druid.version>1.2.4</druid.version>
<mybatis.spring.boot.version>2.1.4</mybatis.spring.boot.version>
</properties>
<!--jar包的總管,子模塊繼承之后,提供作用:鎖定版本+子module不用寫groupId和version-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.1.1</version>
</dependency>
<!--spring boot 2.4.2 cloud i版推薦2.4.2版本-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.4.2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--spring cloud 2020.0.1 也就是i版-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2020.0.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--spring cloud alibaba 2.2.1.RELEASE 目前最新的版本-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
<scope>runtime</scope>
</dependency>
<!-- druid-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis.spring.boot.version}</version>
</dependency>
<!--junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
<!--log4j-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
小提示,如果發現已經匯入了
lombok依賴,但是注解仍然失效的話,那應該是沒有安裝lombok插件的原因,idea插件市場直接搜索安裝即可,但是本人是經常遇到插件市場連不了網的問題,那就上git倉庫選擇對應版本的lombok下載安裝就可以了,點擊跳轉


轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/255669.html
標籤:其他
上一篇:Linux內核同步機制之基本概念
下一篇:訊息佇列之-RabbitMQ
