Maven多模块-Spring Boot项目

3,768 阅读3分钟

在开发过程中,为了方便我们可能会将所有的代码都放在一个项目中,所有人的工作内容都集中在一起。随着业务的快速发展,会导致整个项目越来越复杂和后期维护难等问题。而项目模块化可以更好的实现代码复用,简化项目结构,使逻辑更加清晰,也方便团队分工合作,后期扩展和维护。

本文将以一个个人博客项目为例,简单讲解如何搭建一个基于Spring BootMaven多模块项目。

环境:

  1. JDK
  2. Maven(3.3.9)
  3. Intellij IDEA

1. 首先创建一个Spring Boot父项目

创建Spring Boot项目
注意Type类型选择pom

项目结构如下图所示

项目结构
pom配置文件修改成如图所示。图中标出的两个文件夹是用来存放子项目,library用来存放依赖的项目,application用来存放应用,当然也可以选择直接放在根目录。

2. 创建子项目

右键父项目,选择New,然后选择Module,注意选择Maven类型项目

创建子项目
继续下一步

创建子项目
这里把blog-web项目放在了application文件夹,可以选择放在根目录,没有影响。

创建好直接项目结构如下

项目结构
由于这里当前项目放在application文件夹内,所以blog-webpom文件中的relativePath的配置如图所示。若子项目放在根目录,则配置略有不同,应该是../pom.xml

修改blog-webpom文件

<?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>spring-boot-multi-module</artifactId>
        <groupId>io.example</groupId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../../pom.xml</relativePath>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>blog-web</artifactId>

    <dependencies>
        <dependency>
            <groupId>io.example</groupId>
            <artifactId>blog-service</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <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>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

接下来依次创建servicedao模块,具体过程就不一一解释了,流程和上面叙述的相同,主要看下每个项目的pom文件的配置。

blog-servicepom文件

<?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>spring-boot-multi-module</artifactId>
        <groupId>io.example</groupId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../../pom.xml</relativePath>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>blog-service</artifactId>

    <dependencies>
        <dependency>
            <groupId>io.example</groupId>
            <artifactId>blog-dao</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
    </dependencies>

</project>

blog-daopom文件

<?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>spring-boot-multi-module</artifactId>
        <groupId>io.example</groupId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../../pom.xml</relativePath>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>blog-dao</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
    </dependencies>

</project>

项目成功运行!!!

成功运行

完整代码在GitHub上,有需要的同学可以自行下载。

注意:blog-daoapplication.yml文件放在classpath:config下面,是为了避免配置被覆盖,导致读取不到相关配置的问题。