SpringBoot+Maven多模块项目(创建、依赖、打包可执行jar包部署测试)完整流程

时间:2019-03-18
本文章向大家介绍SpringBoot+Maven多模块项目(创建、依赖、打包可执行jar包部署测试)完整流程,主要包括SpringBoot+Maven多模块项目(创建、依赖、打包可执行jar包部署测试)完整流程使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

开发环境:IDEA,
                  SprngBoot 2.0.4,
                  Maven 2.19.1
工程结构:
                             父工程father

                                                   子模块  dao      (用于持久化数据跟数据库交互)

                                                   子模块  entity    (实体类)

                                                   子模块  service (处理业务逻辑)

                                                   子模块  web       (页面交互接收、传递数据,唯一有启动类的模块)

                                关系:         web依赖 service、dao、entity

                                                    service依赖 dao、entity

                                                    dao依赖 entity

                                                    entity谁都不依赖,独立的

这里我用比较常见的工程结构举例说明,有些公司的项目可能会把模块分的很细,或者会有两个程序入口,也就是两个可以启动的模块!这个我在文章最后会做说明!缕清了思路其实没那么复杂!

一,创建Maven多模块项目

先建立外层父工程         File →new →project  选择Spring Initializr          Next下一步到以下页面

工程结构如下

接下来,把src整个删掉,父工程不需要,因为父工程你就当它只有一个外壳就完了

接下来创建子模块  工程上右键 → new → Module  选择Spring Initaializr  下一步

重复以上动作,创建dao模块,service模块,web模块

service模块和entity模块一样什么都不需要引入

dao模块和web模块可以根据实际需求选择引入mysql,mybatis,redis,web这些,我把我的贴出来

删除每个子模块中没用的文件,.mvn、.gitignore、daoiml、mvnw、mvnw.cmd文件只留下pom.xml

删除除了web模块以外其它模块中的Applicatin启动项,和resources目录下的application.properties配置文件

以上动作操作完成以后如果你发现你的子模块变成了文件夹,没关系,找到Maven Projects刷新一下就好了

整理过后的项目结构是这样的

以上项目的基本结构就完成了,接下来建立各自依赖

二、依赖关系

打开父pom.xml修改打包方式jar为pom,注意:build内容也需要做替换,因为默认的spring-boot-maven-plugin这种方式,等到后期打包的时候他会一直提示你,你引入的依赖不存在!代码如下

<?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>
    <!--父pom.xml-->
    <groupId>com.miu</groupId>
    <artifactId>father</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>
 
    <name>father</name>
    <description>Demo project for Spring Boot</description>
 
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
 
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>
 
    <!--声明你有四个儿子 -->
    <modules>
        <module>entity</module>
        <module>dao</module>
        <module>service</module>
        <module>web</module>
    </modules>
 
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
 
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
 
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19.1</version>
                <configuration>
                    <skipTests>true</skipTests>    <!--默认关掉单元测试 -->
                </configuration>
            </plugin>
        </plugins>
    </build>
 
</project>

这里有个坑需要注意,dao、service、entity这三个模块的pom.xml文件中不需要build 内容,直接干掉

entity 的 pom.xml 内容

<?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.miu</groupId>
    <artifactId>entity</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>entity</name>
    <description>Demo project for Spring Boot</description>
    <!--声明父模块-->
    <parent>
        <groupId>com.miu</groupId>
        <artifactId>father</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

dao 的 pom.xml 内容

<?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>
    <!--dao 模块 pom.xml-->
    <groupId>com.miu</groupId>
    <artifactId>dao</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>dao</name>
    <description>Demo project for Spring Boot</description>
    <!--声明父模块-->
    <parent>
        <groupId>com.miu</groupId>
        <artifactId>father</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--dao 模块 引入entity模块-->
        <dependency>
            <groupId>com.miu</groupId>
            <artifactId>entity</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

service 模块的 pom.xml 内容

<?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.miu</groupId>
    <artifactId>service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
 
    <name>service</name>
    <description>Demo project for Spring Boot</description>
    <!--声明父模块-->
    <parent>
        <groupId>com.miu</groupId>
        <artifactId>father</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--service模块 引入entity模块-->
        <dependency>
            <groupId>com.miu</groupId>
            <artifactId>entity</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <!--service模块 引入dao模块-->
        <dependency>
            <groupId>com.miu</groupId>
            <artifactId>dao</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

web模块的 pom.xml 内容

 注意build部分,因为web模块作为程序的入口启动,所以它需要打包,并且要指定Main Class

<?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.miu</groupId>
    <artifactId>web</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>web</name>
    <description>Demo project for Spring Boot</description>
    <!--声明父模块-->
    <parent>
        <groupId>com.miu</groupId>
        <artifactId>father</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--web模块 引入entity模块-->
        <dependency>
            <groupId>com.miu</groupId>
            <artifactId>entity</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <!--web模块 引入service模块-->
        <dependency>
            <groupId>com.miu</groupId>
            <artifactId>service</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <!--web模块 引入dao模块-->
        <dependency>
            <groupId>com.miu</groupId>
            <artifactId>dao</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin