SpringBoot多模块项目

本文介绍如何使用SpringBoot创建多模块项目,并通过Maven管理依赖。文章详细说明了项目结构设计,包括功能模块划分及Web模块配置。此外,还提供了pom.xml配置示例,帮助读者更好地理解整个项目的构建过程。

SpringBoot 可以快速创建 Web 应用,在平时工作中一般由多个人分工合作,此时使用 SpringBoot 多模块再合适不过了,多模块通过 Maven 的父子依赖实现分工和聚合。
多模块项目打包不能使用spring-boot-maven-plugin 构建插件,应该给终端项目使用,因为这个插件的 repackage 目标会处理 jar 包,导致依赖它的模块无法使用它。在 parent 项目中使用它会导致每个子项目都执行了该目标,进而出现编译失败。

项目github地址: https://github.com/AaronSheng/SpringBoot-Modules

SpringBoot 多模块项目创建过程:

  1. 创建 Maven 项目

    创建新的 maven 项目,并删除 src 目录。
     
  2. 创建功能模块

    domain, dao, service, utils 都是 springboot-modules 的子模块,它们之间有互相依赖。其中 dao 依赖 domain,service 依赖 dao 和 utils。


     
  3. 创建 Web 模块

    创建 webapp 模块作为 web 模块,web 依赖 service,packaging 设置为 war,打成的包名为 web.war。


    web 模块下创建 controller 和 启动类 Application。Application 作为 SpringBoot 的启动类,并继承了 SpringBootServletInitializer 类,从而可以打成 war 包。Application 一般放在包的最外层。

     
  4. main 函数启动和打包
    1) main函数启动


    2) 打包



    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>SpringBoot-Modules</groupId>
        <artifactId>SpringBoot-Modules</artifactId>
        <packaging>pom</packaging>
        <version>1.0-SNAPSHOT</version>
    
        <modules>
            <module>util</module>
            <module>dao</module>
            <module>config</module>
            <module>domain</module>
            <module>service</module>
            <module>web</module>
        </modules>
    
        <properties>
            <java.version>1.8</java.version>
        </properties>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.4.3.RELEASE</version>
        </parent>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <exclusions>
                    <exclusion>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-starter-logging</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
                <scope>provided</scope>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-log4j2</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-jdbc</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-redis</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-orm</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-aop</artifactId>
            </dependency>
    
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
            </dependency>
    
            <!-- hibernate-->
            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-entitymanager</artifactId>
                <version>4.3.6.Final</version>
            </dependency>
            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-core</artifactId>
                <version>4.3.6.Final</version>
            </dependency>
    
            <!-- druid + mysql -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>1.0.13</version>
            </dependency>
    
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>1.2.7</version>
            </dependency>
    
            <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-web</artifactId>
                <version>2.6.2</version>
            </dependency>
        </dependencies>
    
        <profiles>
            <profile>
                <id>dev</id>
                <activation>
                    <activeByDefault>true</activeByDefault>
                </activation>
                <build>
                    <resources>
                        <resource>
                            <directory>web/src/main/resources/dev</directory>
                        </resource>
                        <resource>
                            <directory>src/main/resources</directory>
                        </resource>
                    </resources>
                </build>
            </profile>
            <profile>
                <id>test</id>
                <build>
                    <resources>
                        <resource>
                            <directory>web/src/main/resources/test</directory>
                        </resource>
                        <resource>
                            <directory>src/main/resources</directory>
                        </resource>
                    </resources>
                </build>
            </profile>
            <profile>
                <id>pro</id>
                <build>
                    <resources>
                        <resource>
                            <directory>web/src/main/resources/pro</directory>
                        </resource>
                        <resource>
                            <directory>src/main/resources</directory>
                        </resource>
                    </resources>
                </build>
            </profile>
        </profiles>
    
    </project>

     

转载于:https://my.oschina.net/u/2950586/blog/1329949

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值