JAVA搞不清楚系列 - Spring Boot Multi Module Porject
第一次碰到Multi Module其實應該是在做gRPC的專案的時候,後來才又重新認真研究了一下。
Spring Boot的Multi Module 可以幫助專案規劃分module,重複使用POM,以免引入變得過於龐大,且不用每次都構建整個專案,並且使得針對某個模組的特殊控制更為方便。
Gitlab連結
https://gitlab.com/jam68ty/spring-boot-multi-modules-semgrep
流程總覽
- 建立一個母moudule以及兩個子module
- 重構專案結構、產生jar檔
- 母module引入子moudule
- 寫一些測試的Service, Controller
- 此時應該要可以透過母module去控管整個專案共同需要的POM,而個別子module需要的dependency則在該module中引入即可
實作詳解
利用Maven Quickstart Archetype建立一個Parent Module
1
mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4
用Spring Initializr建立兩個Child Modules,並把這兩個專案都移到Parent Module下
a. jdjcore: 不引入任何dependency
b. jdjweb: 引入Spring-web完成後先
mvn clean install
母Module修改Parent Module的POM,引入兩個子Module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<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.test</groupId>
<artifactId>multi-module-project</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- 更改packaging的類型為pom-->
<packaging>pom</packaging>
<name>multi-module-project</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<java-version>1.8</java-version>
</properties>
<!-- 引入子modules-->
<modules>
<module>jdj-core</module>
<module>jdj-web</module>
</modules>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.3.6.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.3.6.RELEASE</version>
</dependency>
</dependencies>
</project>修改Child Module - jdjcore (包含Entity, DAO, Service)
POM
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<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.test</groupId>
<artifactId>multi-module-project</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- 更改packaging的類型為pom-->
<packaging>pom</packaging>
<name>multi-module-project</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<java-version>1.8</java-version>
</properties>
<!-- 引入子modules-->
<modules>
<module>jdj-core</module>
<module>jdj-web</module>
</modules>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.3.6.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.3.6.RELEASE</version>
</dependency>
</dependencies>
</project>DAO:
com.test.jdjcore.dao.CustomerRepository
1
2
3
4
5
6
7package com.test.jdjcore.dao;
import com.test.jdjcore.entity.Customer;
public interface CustomerRepository {
Customer getCustomerById(int id);
}Service:
com.test.jdjcore.service.CustomerService
1
2
3
4
5
6
7
8
9
10
11
12
13
14package com.test.jdjcore.service;
import com.test.jdjcore.dao.CustomerRepository;
import com.test.jdjcore.entity.Customer;
import org.springframework.stereotype.Service;
public class CustomerService implements CustomerRepository{
public Customer getCustomerById(int id) {
return new Customer(id, "name", "email");
}
}把JdjCoreApplication的
@SpringBootApplication
註解掉,並把main的內容也刪除
- 修改Child Module - jdjweb (包含Controller)
POM
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- 引入母modules-->
<parent>
<groupId>com.test</groupId>
<artifactId>multi-module-project</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>jdj-web</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>jdj-web</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.6.4</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.6.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.test</groupId>
<artifactId>jdj-core</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<finalName>javadev-web</finalName>
</build>
</project>Controller:
com.test.jdjweb.controller.CustomerController
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24package com.test.jdjweb.controller;
import com.test.jdjcore.dao.CustomerRepository;
import com.test.jdjcore.entity.Customer;
import com.test.jdjcore.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
public class CustomerController {
CustomerService customerService;
public Customer getCustomer(int id){
return customerService.getCustomerById(id);
}
}由於Multi-modules,
@Autowierd
可能會出錯,此時需要在JdjWebApplication.java
加上@ComponentScan({”com.test.jdjcore”, “com.test.jdjweb”})
結果
- Start Application: 對整個專案
mvn clean install
→mvn spring-boot:run jdj-web
- Test:
http://localhost:8080/customer/1