【前言】
项目虽然是Eureka、OpenFeign 进行服务注册和服务调用,但是每个模块都是一个单独的SpringBoot,启动每个模块都需要单独启动一个idea,觉得这个过于繁琐,现在想把项目变成一个聚合项目,只需要启动一个idea即可。
【过程】
1、新建maven聚合父工程
快速创建一个maven工程,删除无关文件,只保留pom.xml
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"><!-- 基本信息 --><description>SpringBoot 多模块构建示例</description><modelVersion>4.0.0</modelVersion><name>project-aggregation</name><packaging>pom</packaging><!-- 项目说明:这里作为聚合工程的父工程 --><groupId>com.pu</groupId><artifactId>project-aggregation</artifactId><version>1.0-SNAPSHOT</version><!-- 继承说明:这里继承SpringBoot提供的父工程 --><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.5.RELEASE</version><relativePath/></parent><!-- 模块说明:这里声明多个子模块 --><modules><module>helloworld</module><module>mybatistest</module></modules><!-- 在properties中统一控制依赖包的版本,更清晰--><properties><java.version>1.8</java.version><fastjson.version>1.2.32</fastjson.version><spring-cloud.version>Greenwich.SR1</spring-cloud.version><feign.version>2.1.1.RELEASE</feign.version><eureka-client.version>2.1.1.RELEASE</eureka-client.version><zkclient.version>0.10</zkclient.version><poi.version>3.13</poi.version><lombok.version>1.18.6</lombok.version></properties><!--在此处统一加,子项目pom就可以不用引入了--><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><!-- lombok --><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml-schemas</artifactId></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId></dependency></dependencies><!--dependencyManagement用于管理依赖版本号--><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><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId><version>${eureka-client.version}</version></dependency><!--feign --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId><version>${feign.version}</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>${fastjson.version}</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>${poi.version}</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml-schemas</artifactId><version>${poi.version}</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>${poi.version}</version></dependency><!-- lombok --><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>${lombok.version}</version></dependency></dependencies></dependencyManagement><!--该插件作用是打一个可运行的包,必须要写在需要打包的项目里。这里的父模块不需要打包运行,所以删掉该插件。--><!--<build>--><!--<plugins>--><!--<plugin>--><!--<groupId>org.springframework.boot</groupId>--><!--<artifactId>spring-boot-maven-plugin</artifactId>--><!--</plugin>--><!--</plugins>--><!--</build>--></project>
2.建子模块module项目helloworld
父工程右键–》new–>module
<?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.pu</groupId><artifactId>helloworld</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>helloworld</name><description>Demo project for Spring Boot</description><!-- 继承本项目的父工程 --><parent><groupId>com.pu</groupId><artifactId>project-aggregation</artifactId><version>1.0-SNAPSHOT</version><relativePath>../pom.xml</relativePath><!-- lookup parent from repository --></parent><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
【细节】
(1)在进行子模块创建的时候,下面的relativePath需要执行父POM的地址才能依赖上
<parent><groupId>com.example</groupId><artifactId>intellnm</artifactId><version>0.0.1-SNAPSHOT</version><relativePath></relativePath></parent>
正确的应该是
<parent><groupId>com.example</groupId><artifactId>intellnm</artifactId><version>0.0.1-SNAPSHOT</version><relativePath>../pom.xml</relativePath></parent>
这样就可以避免出现下面这个问题了。
Could not find artifact com.example:intellnm:pom:0.0.1-SWAPSHOT and ‘parent.relativePath’ points at no local POM line 11, column 10 -> [Help 2]