前言
系统复杂了,抽离单一职责的模块几乎是必须的;若需维护多个项目,抽离公用包上传私有仓库管理也几乎是必须的。其优点无需赘述,以下将记录操作过程。
1. 多模块拆分
在.NET 中由于其统一性,实现上更自然一点。Spring Boot 通过 Maven 构建多模块工程也不麻烦,假如我的项目中包含以下几个包:
我需要将他们分别拆分成独立模块,首先要修改的是根目录下的 pom.xml,packaging 类型改为 pom,并添加 modules 节点:
<?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.youclk.multi-package</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<modules>
<module>api</module>
<module>service</module>
<module>dao</module>
</modules>
<packaging>pom</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.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>
<lombok>1.16.20</lombok>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok}</version>
</dependency>
</dependencies>
</project>
之后新建一个个 Module,将对应的代码移植过去:
需要注意的是在启动模块的 pom.xml 中需要指定启动类:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.youclk.multipackage.api.MultiApplication</mainClass>
<layout>ZIP</layout>
</configuration>
</plugin>
</plugins>
</build>
统一升级版本命令:mvn versions:set -DnewVersion=0.0.1-SNAPSHOT
&#x