其他
Spring Boot 多模块项目实践(附打包方法)
点击上方 Java后端,选择 设为星标
作者 | yizhiwazi
链接 | www.jianshu.com/p/59ceea4f029d
序言:
比起传统复杂的单体工程,使用Maven的多模块配置,可以帮助项目划分模块,鼓励重用,防止POM变得过于庞大,方便某个模块的构建,而不用每次都构建整个项目,并且使得针对某个模块的特殊控制更为方便。
接下来,本文将重点阐述SpringBoot在Maven环境的多模块构建过程。
本项目传送门:
https://github.com/yizhiwazi/springboot-socks/tree/master/springboot-integration
一、创建聚合父工程
1.首先使用 Spring Initializr 来快速创建好一个Maven工程。然后删除无关的文件,只需保留pom.xml 文件。
2.然后在 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>
二、创建子模块(module)
注:这里是使用IDEA来创建子模块,使用Eclipse的小伙伴可通过 Spring Initializr 构建,然后复制去进去父工程根目录即可。
对着父工程右键 - New - Module - > 输入 mm-web 对着父工程右键 - New - Module - > 输入 mm-service 对着父工程右键 - New - Module - > 输入 mm-repo 对着父工程右键 - New - Module - > 输入 mm-entity
<?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>
三、编写子模块代码
1.控制层(mm-web)
@SpringBootApplication
public class MmWebApplication {
public static void main(String[] args) {
SpringApplication.run(MmWebApplication.class, args);
}
}
@RestController
@RequestMapping("/user/*")
public class UserController {
@Autowired
UserService userService;
@GetMapping("list")
public R list() {
try {
return R.isOk().data(userService.list());
} catch (Exception e) {
return R.isFail(e);
}
}
}
spring:
datasource:
url: jdbc:mysql://localhost:3306/socks?useSSL=false
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
2.业务层(mm-service)
@Service
public class UserServiceImpl implements UserService {
@Autowired
UserRepository userRepository;
@Override
public List<User> list() {
return userRepository.findAll();
}
}
3.数据层(mm-repo)
public interface UserRepository extends JpaRepository<User,String> {
}
4.mm-entity (实体模型层)
public class R<T> implements Serializable {
private static final long serialVersionUID = -4577255781088498763L;
private static final int OK = 0;
private static final int FAIL = 1;
private static final int UNAUTHORIZED = 2;
private T data; //服务端数据
private int status = OK; //状态码
private String msg = ""; //描述信息
//APIS
public static R isOk(){
return new R();
}
public static R isFail(){
return new R().status(FAIL);
}
public static R isFail(Throwable e){
return isFail().msg(e);
}
public R msg(Throwable e){
this.setMsg(e.toString());
return this;
}
public R data(T data){
this.setData(data);
return this;
}
public R status(int status){
this.setStatus(status);
return this;
}
//Constructors
public R() {
}
//Getter&Setters
}
@Entity
@Table(name = "T_USER")
public class User {
@Id
@Column(name = "USERID")
private String userId;
@Column(name = "USERNAME")
private String username;
@Column(name = "PASSWORD")
private String password;
//Getter&Setters
}
三、运行项目
四、运维部署(多模块打包)
1.添加打包插件
<!--多模块打包:只需在启动类所在模块的POM文件:指定打包插件 -->
<build>
<plugins>
<plugin>
<!--该插件主要用途:构建可执行的JAR -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
2.打包工程
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] spring-boot-integration ............................ SUCCESS [ 0.000 s]
[INFO] mm-entity .......................................... SUCCESS [ 1.915 s]
[INFO] mm-repo ............................................ SUCCESS [ 0.235 s]
[INFO] mm-service ......................................... SUCCESS [ 0.218 s]
[INFO] mm-web ............................................. SUCCESS [ 0.891 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.798 s
[INFO] Finished at: 2017-10-18T17:17:02+08:00
[INFO] Final Memory: 35M/300M
[INFO] ------------------------------------------------------------------------
3.启动项目
xx\mm-web\target>java -jar mm-web-0.0.1-SNAPSHOT.jar
【END】