SpringBoot构建多模块项目

项目结构

service-parent
=====service-1
=========pom.xml
=====service-2
=========pom.xml
pom.xml
创建之前先创建三个目录service-parent,service-1,service-2.service-1和service-2在目录service-parent下。

创建service-parent模块

1.选择新建项目Create new Project
SpringBoot构建多模块项目

2.选择Maven构建项目 SpringBoot构建多模块项目

3.填写GroupId:service-demo , ArtifactId:service-parent
SpringBoot构建多模块项目
4. 填写项目名称 Project name,并选择存放的目录。

SpringBoot构建多模块项目

5.生成的目录结构

SpringBoot构建多模块项目

6.创建子模块

在项目service-parent右键选择新建模块,如之前填写相关信息。可以看到该模块的父模块为service-demo:service-parent
SpringBoot构建多模块项目
SpringBoot构建多模块项目

最后再创建模块service-2.最后的项目结构。
SpringBoot构建多模块项目

pom配置文件

service-parent
可以看到引入了子模块

<?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>service-demo</groupId>
    <artifactId>service-parent</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>

    <modules>
        <module>service-1</module>
        <module>service-2</module>
    </modules>
</project>

service-1和service-2
可以看到声明了父模块service-parent

<?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">
    <parent>
        <artifactId>service-parent</artifactId>
        <groupId>service-demo</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>service-1</artifactId>


</project>

如果service-1需要依赖service-2模块,service-1中的pom.xml添加dependency依赖即可

 <dependencies>
   <dependency>
        <groupId>service-demo</groupId>
        <artifactId>service-2</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
</dependencies>

如上的多模块子项目,如果有什么公共的依赖,只要在service-parent父模块中引入依赖,那么子模块会继承父模块的依赖,就不需要重复引入。