Maven多模块-Spring Boot项目

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

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

环境:

  1. JDK
  2. Maven(3.3.9)
  3. Intellij IDEA
1. 首先创建一个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下面,是为了避免配置被覆盖,导致读取不到相关配置的问题。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值