1、建立聚合父工程
1.首先使用 Spring Initializr 来快速建立好一个Maven工程。而后删除无关的文件,
只需保留pom.xml 文件。
复制代码
而后在 pom.xml 里面声明该父工程包含的子模块。(其它信息就不逐一讲述了,
诸如继承SpringBoot官方父工程以及统一依赖管理 请查看下面的注释说明)
注意:这里能够不统一管理依赖的版本号,在模内部本身定义,若有引入其余模块,也要指定
版本信息
<?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">
<!-- 基本信息 -->
<description>SpringBoot 多模块构建示例</description>
<modelVersion>4.0.0</modelVersion>
<name>springboot-integration</name>
<packaging>pom</packaging>
<!-- 项目说明:这里做为聚合工程的父工程 -->
<groupId>com.hehe</groupId>
<artifactId>springboot-integration</artifactId>
<version>1.0.0.RELEASE</version>
<!-- 继承说明:这里继承SpringBoot提供的父工程 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.7.RELEASE</version>
<relativePath/>
</parent>
<!-- 模块说明:这里声明多个子模块 -->
<modules>
<module>mm-web</module>
<module>mm-service</module>
<module>mm-repo</module>
<module>mm-entity</module>
</modules>
<!-- 版本说明:这里统一管理依赖的版本号 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.hehe</groupId>
<artifactId>mm-web</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.hehe</groupId>
<artifactId>mm-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.hehe</groupId>
<artifactId>mm-repo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.hehe</groupId>
<artifactId>mm-entity</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.42</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
复制代码
2、建立子模块(module)java
注:这里是使用IDEA来建立子模块,使用Eclipse的小伙伴可经过
Spring Initializr 构建,而后复制去进去父工程根目录便可。
1.对着父工程右键 - New - Module - > 输入 mm-web
2.对着父工程右键 - New - Module - > 输入 mm-service
3.对着父工程右键 - New - Module - > 输入 mm-repo
4.对着父工程右键 - New - Module - > 输入 mm-entity
1~4 步骤完成后,分别调整它们的pom.xml 以继承上面的父工程。
注意:Artifact id和Project name保持一致,Package name设置时把_用.分开
复制代码
例如mm-web模块的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.hehe</groupId>
<artifactId>mm-web</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>mm-web</name>
//---------------------------------------------添加
<!-- 继承本项目的父工程 -->
<parent>
<groupId>com.hehe</groupId>
<artifactId>springboot-integration</artifactId>
<version>1.0.0.RELEASE</version>
</parent>
//---------------------------------------------添加
<!-- Web模块相关依赖 -->
<dependencies>
<dependency>//-------------------------依赖其余模块
<groupId>com.hehe</groupId>
<artifactId>mm-service</artifactId>
</dependency>
<dependency>
<groupId>com.hehe</groupId>
<artifactId>mm-entity</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
复制代码
3、编写子模块代码mysql
4、运维部署(多模块打包)web
1. 添加打包插件
注意:多模块项目仅仅须要在启动类所在的模块添加打包插件便可!!
不要在父类添加打包插件,由于那样会致使所有子模块都使用
spring-boot-maven-plugin的方式来打包(例如BOOT-INF/com/hehe/xx),
而mm-web模块引入mm-xx 的jar 须要的是裸露的类文件,即目录格式为(/com/hehe/xx)。
复制代码
本案例的启动模块是 mm-web , 只需在它的pom.xml 添加打包插件(spring-boot-maven-plugin):spring
<!--多模块打包:只需在启动类所在模块的POM文件:指定打包插件 -->
<build>
<plugins>
<plugin>
<!--该插件主要用途:构建可执行的JAR -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
复制代码
4、总结sql
1.模块化编程参考博客
2.对于父工程apache
1. 能够删除src目录,.mvn目录
2. 配置要修改<packaging>jar</packaging> => <packaging>pom</packaging>
添加:
<!-- 模块说明:这里声明多个子模块 -->
<modules>
<module>wm-web</module>
<module>wm-service</module>
</modules>
删除部分:
<build>
<plugins>
<!--<plugin>-->
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-maven-plugin</artifactId>-->
<!--</plugin>-->
</plugins>
</build>
复制代码
3.对于子模块编程
替换父模块配置
<!--<parent>-->
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-starter-parent</artifactId>-->
<!--<version>2.1.0.RELEASE</version>-->
<!--<relativePath/> <!– lookup parent from repository –>-->
<!--</parent>-->
到
<!-- 继承本项目的父工程 -->
<parent>
<groupId>com.example</groupId>
<artifactId>module</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
设置依赖的其余模块
<!-- 继承本项目的父工程 -->
<dependency>
<groupId>com.example</groupId>
<artifactId>wm-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
对于提供公共服务的模块
须要注释:
<build>
<!--<plugins>-->
<!--<!–<plugin>–>-->
<!--<!–<groupId>org.springframework.boot</groupId>–>-->
<!--<!–<artifactId>spring-boot-maven-plugin</artifactId>–>-->
<!--<!–</plugin>–>-->
<!--</plugins>-->
</build>
对于模块间注入对象的,访问不到的
启动类添加:
启动类代码以下:
@SpringBootApplication
@ComponentScan(basePackages = {"com.cp.springboot.web","com.cp.springboot.domain.bean"})
public class WebApplication {
public static void main(String[] args) {
SpringApplication.run(WebApplication.class, args);
}
}
设置扫描包ComponentScan
复制代码
1.参考博客segmentfault