Maven构建SpringBoot多模块工程
2021-06-07 10:46:51
新建一个Maven工程,`pom.xml`文件如下
```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.example</groupId>
<artifactId>spring-boot-demo</artifactId>
<version>1.0-SNAPSHOT</version>
</project>
```
删除src目录,新建一个叫common的Module,common中的pom.xml内容如下
```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">
<parent>
<artifactId>spring-boot-demo</artifactId>
<groupId>com.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>common</artifactId>
</project>
```
idea自动在父级pom.xml中,增加了如下信息
```xml
<packaging>pom</packaging>
<modules>
<module>common</module>
</modules>
```
再建一个叫`foo-api`的子模块,依赖`common`,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">
<parent>
<artifactId>spring-boot-demo</artifactId>
<groupId>com.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>foo-api</artifactId>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
```
如果子模块,创建在二级目录,例如`foo/foo-api`,需要指定`relativePath`(如果不指定这个参数默认值为 `../pom.xml`)
```xml
<parent>
<artifactId>spring-boot-demo</artifactId>
<groupId>com.example</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>./../../pom.xml</relativePath>
</parent>
```
在最顶层的pom.xml中添加SpringBoot依赖
```xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.8.RELEASE</version>
<relativePath/>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
```
common模块中,添加一个类
```java
package com.example.common;
import com.fasterxml.jackson.annotation.JsonIgnore;
public class JsonResult<T> {
private String code;
private String message;
private T data;
public String getCode() {
return code;
}
public String getMessage() {
return message;
}
public T getData() {
return data;
}
//成功, 没有数据
public JsonResult() {
this.data = null;
this.code = "SUCCESS";
this.message = "";
}
//成功,有数据
public JsonResult(T data) {
this.data = data;
this.message = "";
this.code = "SUCCESS";
}
//成功或失败,决于status,不携带数据
public JsonResult(boolean status, String message) {
this.data = null;
this.message = message;
this.code = status ? "SUCCESS" : "ERROR";
}
public JsonResult(String code, String message) {
this.data = null;
this.message = message;
this.code = code;
}
public JsonResult(String code, String message, T data) {
this.data = data;
this.message = message;
this.code = code;
}
public JsonResult(boolean status, String message, T data) {
this.data = data;
this.message = message;
this.code = status ? "SUCCESS" : "ERROR";
}
@JsonIgnore
public boolean isSuccess() {
return code.equals("SUCCESS");
}
}
```
`foo-api`中新建个启动类
```java
package com.example;
import com.example.common.JsonResult;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@GetMapping("/hello")
public JsonResult hello() {
return new JsonResult();
}
}
```
浏览器访问`http://localhost:8080/hello`
因为依赖了common模块,所以common中的`JsonResult`类,在`foo-api`模块中可以使用
构建jar包,在根目录执行,就可以在模块的`target`目录中,输出jar包
```shell
$ mvn clean package
```
直接这样构建的jar包,不能执行。使用`spring-boot-maven-plugin`可以打出可执行jar包:
```xml
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
```
common模块不能独立运行,打spring boot的jar会报错,需要在common模块的pom中跳过`spring-boot-maven-plugin`模块,否则报错 `repackage failed: Unable to find main class`
```xml
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
```
(根目录中,只有`pom.xml`、`.gitignore`和对应的Module目录)
`.gitignore`文件
```ini
### cache ###
.DS_Store
.sass-cache
Thumbs.db
### build ###
target/
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### VS Code ###
.vscode/
```