一、創(chuàng)建流程
1、首先創(chuàng)建springboot項目作為父項目
只留下pom.xml 文件,刪除src目錄及其他無用文件
?2、創(chuàng)建子項目
子項目可以是maven項目,也可以是springboot項目
3、父子項目關聯
?4、父項目中依賴管理
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- 繼承的springboot版本-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!-- 引入子模塊 -->
<modules>
<module>system</module>
<module>gateway</module>
<module>common</module>
</modules>
<groupId>com.matter</groupId>
<artifactId>record</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>syh_matter_record_server</name>
<description>syh_matter_record_server</description>
<packaging>pom</packaging>
<!-- 作用:公共依賴引入
使用場景:基本所有子模塊都用到的依賴-->
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!-- jdbc依賴包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
<!-- 版本管理 -->
<properties>
<java.version>1.8</java.version>
<spring.cloud.version>2021.0.1</spring.cloud.version>
<spring.cloud.alibaba.version>2021.0.1.0</spring.cloud.alibaba.version>
<mybatis.plus.version>3.4.3.4</mybatis.plus.version>
<fastjson2.version>2.0.9</fastjson2.version>
<pagehelper.version>1.4.3</pagehelper.version>
</properties>
<!-- 作用:依賴版本生命,不做依賴引入。使用dependencyManagement統(tǒng)一管理項目的版本號,確保應用的各個項目的依賴和版本一致,
不用每個模塊項目都弄一個版本號,不利于管理。 -->
<dependencyManagement>
<dependencies>
<!-- spring-loud -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring.cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- 阿里微服務 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring.cloud.alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- mybatis-plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis.plus.version}</version>
</dependency>
<!-- 阿里json解析 -->
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
<version>${fastjson2.version}</version>
</dependency>
<!-- 分頁插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>${pagehelper.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
?depedencyManagement標簽介紹
在maven的聚合工程中,父模塊的pom文件中,使用dependencyManagement統(tǒng)一管理項目的版本號,確保應用的各個項目的依賴和版本一致,不用每個模塊項目都弄一個版本號,不利于管理。
dependencyManagement里只是聲明依賴,并不自動實現引入,需要子項目聲明需要用的依賴。
- 如果不在子項目中聲明依賴,是不會從父項目中繼承下來的;
- 如果在子項目中寫了該依賴項,但是沒有指定具體版本,這時候才會從父項目中繼承相應依賴,以及相應的version和scope;
- 如果在子項目中寫了該依賴項,并且項目中指定了版本號,那么會使用子項目中指定的版本,不再使用父項目指定的依賴版本號。
父項目使用depedencyManagement聲明依賴,子工程引入依賴不需要聲明版本
5、公共模塊使用
第一步:創(chuàng)建公共模塊 common
第二步:父子關聯
第三步:其他模塊使用公共模塊?common
dependencies標簽引入公共模塊?common,然后就可以調用方法了
二、遇到的問題以及解決方法
1、公共模塊被其他模塊引入后使用,使用正常,打包報錯?程序包 com.xxx.common.utils不存在或者xxx找不到符號
原因:SpringBoot工程打包編譯時,會生成兩種jar包,一種是普通的jar,另一種是可執(zhí)行jar。默認情況下,這兩種jar的名稱相同,在不做配置的情況下,普通的jar先生成,可執(zhí)行jar后生成,造成可執(zhí)行jar會覆蓋普通的jar。而service工程無法依賴common-utils工程的可執(zhí)行jar,所以編譯失?。撼绦虬淮嬖?。
解決:在common-utils工程中,pom.xml文件中加入配置即可:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<classifier>exec</classifier>
</configuration>
</plugin>
</plugins>
</build>
2、Maven多模塊項目無法加載其他模塊(公共模塊)下的Bean
比如公共模塊中有redis序列化配置,以及redis工具類需要需要注入
?在需要使用的模塊中的啟動類添加 @ComponentScan注解
// 值為一個數組,表示掃描的路徑,遞歸掃描到最后
@ComponentScan(basePackages = {"com.example"})
?這里看到我們兩個模塊剛好前綴一樣,所以只配置一個路徑即可
?文章來源:http://www.zghlxwxcb.cn/news/detail-673956.html
?文章來源地址http://www.zghlxwxcb.cn/news/detail-673956.html
到了這里,關于Maven聚合項目(微服務項目)創(chuàng)建流程,以及pom詳解的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!