SpringBoot 可以快速创建 Web 应用,在平时工作中一般由多个人分工合作,此时使用 SpringBoot 多模块再合适不过了,多模块通过 Maven 的父子依赖实现分工和聚合。
多模块项目打包不能使用spring-boot-maven-plugin 构建插件,应该给终端项目使用,因为这个插件的 repackage 目标会处理 jar 包,导致依赖它的模块无法使用它。在 parent 项目中使用它会导致每个子项目都执行了该目标,进而出现编译失败。
项目github地址: https://github.com/AaronSheng/SpringBoot-Modules
SpringBoot 多模块项目创建过程:
- 创建 Maven 项目
创建新的 maven 项目,并删除 src 目录。
- 创建功能模块
domain, dao, service, utils 都是 springboot-modules 的子模块,它们之间有互相依赖。其中 dao 依赖 domain,service 依赖 dao 和 utils。
- 创建 Web 模块
创建 webapp 模块作为 web 模块,web 依赖 service,packaging 设置为 war,打成的包名为 web.war。
web 模块下创建 controller 和 启动类 Application。Application 作为 SpringBoot 的启动类,并继承了 SpringBootServletInitializer 类,从而可以打成 war 包。Application 一般放在包的最外层。
- 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>