使用maven創建單一項目的時候通常用不到聚合項目,創建spring cloud項目時候,由於下面都是一個一個微服務,每個服務對應一個項目,這就需要用到聚合項目,方便對依賴和項目之間的關系進行管理,使用idea創建一個maven項目。
Maven Project
創建父Maven Project
File -- New -- Project
Name -- maven GAV -- Next
Maven Home -- Override Settings File -- Finish
創建完成后,刪除父項目下的整個src目錄,父項目不做編碼,只保留一個pom文件管理依賴,選擇Enable Auto-import
引入父Project pom依賴
在pom文件中添加相關依賴
<?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>org.example</groupId>
<artifactId>springcloud</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<name>spring cloud</name>
<description>spring cloud study</description>
<!--統一配置管理-->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<mybatis.version>1.3.2</mybatis.version>
<druid.version>1.1.10</druid.version>
<spring-cloud.version>Hoxton.SR3</spring-cloud.version>
</properties>
<dependencyManagement>
<dependencies>
<!--Spring Cloud Hoxton.SR3-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--Mybatis 1.3.2-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis.version}</version>
</dependency>
<!--MySQL 8.0.18-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.18</version>
</dependency>
<!--Druid 1.1.10-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${druid.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.5.3.RELEASE</version>
<configuration>
<fork>true</fork>
<addResources>true</addResources>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
<dependencyManagement/>
和<pluginManagement/>
的作用是統一配置管理,子項目需要引入的公共模塊可以在父項目中聲明並配置版本,但是子項目還需要顯式引用相應的依賴,只是不用再引入<version/>
,如果子項目有特定的版本需要重新指定版本即可。
聚合項目父項目創建完成。
Maven Module
創建子Maven Module
父項目springcloud右鍵,New -- Modul -- Maven -- Next
Parent -- Name -- maven GAV -- Finish
cloud-account創建后的目錄結構,在pom中引入相應的依賴
引入子Module pom依賴
<?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>
<parent>
<groupId>com.cloud</groupId>
<artifactId>springcloud</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>cloud-order</artifactId>
<name>cloud-order</name>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--actuator-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--eureka client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--devtools-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--druid-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
</dependency>
<!--commons-->
<dependency>
<groupId>com.cloud</groupId>
<artifactId>cloud-commons</artifactId>
<version>1.0</version>
</dependency>
<!--jdbc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
創建完成后在父pom文件中會發現,自動添加了cloud-order module
<modules>
<module>cloud-order</module>
</modules>
而子Module中會顯示對應的parent
<parent>
<artifactId>springcloud</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
如果有其他module,可以以這種方式繼續添加。
總結:
- maven聚合項目,其依賴關系為聚合關系
- 父項目類型為pom,子項目為jar或者war,如果子項目下面繼續有子項目,子項目類型也為pom
- maven中的繼承關系,只有邏輯上父子關系,聚合項目不僅有邏輯上的包含,父項目會真實的包含子項目
- 父項目通過modules指明子模塊,子模塊通過parent表明繼承關系