比起傳統復雜的單體工程,使用Maven的多模塊配置,可以幫助項目划分模塊,鼓勵重用,防止POM變得過於龐大,方便某個模塊的構建,而不用每次都構建整個項目,並且使得針對某個模塊的特殊控制更為方便。接下來,本文將重點闡述SpringBoot在Maven環境的多模塊構建過程。
第一步:創建父工程
使用spring Tool Suite的童鞋可以直接new一個spring starter project;使用idea和eclipse童鞋可以通過插件使用 Spring Initializr ,快速創建springboot工程;
然后去掉,無關緊要的東西留下pom文件就好了,這樣父工程就創建好了;
第二步:給父工程添加pom文件配置:
<?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>boot-polymer</name>
<packaging>pom</packaging>
<!-- 項目說明:這里作為聚合工程的父工程 -->
<groupId>com.polymer</groupId>
<artifactId>boot-polymer</artifactId>
<version>1.0.0.RELEASE</version>
<!-- 繼承說明:這里繼承SpringBoot提供的父工程 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<!-- 模塊說明:這里聲明多個子模塊 -->
<modules>
<module>boot-polymer-web</module>
<module>boot-polymer-service</module>
<module>boot-polymer-repository</module>
<module>boot-polymer-entity</module>
<module>boot-polymer-common</module>
</modules>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.polymerization</groupId>
<artifactId>boot-polymer-web</artifactId>
<version>1.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>com.polymerization</groupId>
<artifactId>boot-polymer-service</artifactId>
<version>1.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>com.polymerization</groupId>
<artifactId>boot-polymer-repository</artifactId>
<version>1.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>com.polymerization</groupId>
<artifactId>boot-polymer-entity</artifactId>
<version>1.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>com.polymerization</groupId>
<artifactId>boot-polymer-common</artifactId>
<version>1.0.0.RELEASE</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
第三步;創建子工程
右擊父工程->new ->maven module->創建選擇jar->全部創建jar包工程,別創建war包否則你就別用springboot,不要問為什么
第四步:測試工程是否能啟動
首先在boot-polymer-web工程添加pom配置
<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>
<parent>
<groupId>com.polymer</groupId>
<artifactId>boot-polymer</artifactId>
<version>1.0.0.RELEASE</version>
</parent>
<artifactId>boot-polymer-web</artifactId>
<dependencies>
<dependency>
<groupId>com.polymer</groupId>
<artifactId>boot-polymer-service</artifactId>
</dependency>
<dependency>
<groupId>com.polymer</groupId>
<artifactId>boot-polymer-repository</artifactId>
</dependency>
<dependency>
<groupId>com.polymer</groupId>
<artifactId>boot-polymer-entity</artifactId>
</dependency>
<dependency>
<groupId>com.polymer</groupId>
<artifactId>boot-polymer-common</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
然后再web項目下新建一個啟動類:
package com.polymer.app; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; @RestController @SpringBootApplication //Spring Boot核心注解,用於開啟自動配置 public class StartApplication { //程序可以直接在此啟動 @RequestMapping("/") String index(){ return "ok"; } public static void main(String[] args) { SpringApplication.run(StartApplication.class, args); } public void addResourceHandlers(ResourceHandlerRegistry registry) { //配置靜態資源處理 registry.addResourceHandler("/**") .addResourceLocations("classpath:/META-INF/") .addResourceLocations("classpath:/hospitalpay"); } }
然后run as application——》
看到這個就表示啟動成功了;然后再去訪問一下
成功了;