SpringBoot多模块项目

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

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

SpringBoot 多模块项目建立过程:mysql

  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>
相关文章
相关标签/搜索