IntelliJ IDEA 搭建 Spring Boot Maven 多模块项目
1. 创建父项目
1.1 父项目创建完需手动删除 src 目录
1.2 父级项目 multiple-project 配置 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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mutiple</groupId>
<artifactId>mutiple-project</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<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>
<spring-boot.version>1.5.19.RELEASE</spring-boot.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.19.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<!--Spring IO 控制依赖版本适配-->
<dependency>
<groupId>io.spring.platform</groupId>
<artifactId>platform-bom</artifactId>
<version>Brussels-SR16</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--Spring Boot 控制依赖版本适配-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
<version>1.9.22</version>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity3</artifactId>
<version>3.0.4.RELEASE</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
2. 创建子级 Web 模块, 一般用于业务入口
2.1 删除模块内 webapp 目录
2.2 java, resources 如没有需要手动生成,之后需手动设置为 IntelliJ IDEA 认的之源目录
3. 子级模块 multiple-web
3.1 配置 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>mutiple-project</artifactId>
<groupId>com.mutiple</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mutiple</groupId>
<artifactId>mutiple-web</artifactId>
<packaging>jar</packaging>
<name>mutiple-web Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity3</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>mutiple-web</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.2 resources 目录内创建环境文件 application.properties
#HTML Templete
spring.thymeleaf.mode=LEGACYHTML5
spring.thymeleaf.cache=false
spring.thymeleaf.check-template=true
spring.thymeleaf.check-template-location=true
spring.thymeleaf.prefix=classpath:public/
spring.thymeleaf.content-type=text/html
spring.thymeleaf.suffix=.html
spring.thymeleaf.encoding=UTF-8
3.3 resources 目录内创建目录 public 再生成 test.html 文件 (用于测试)
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-4.dtd">
<html xmlns:th="http://www.thymeleaf.org">
<head></head>
<body>
<div>
<h4>Hello World!!</h4>
</div>
</body>
</html>
3.4 创建 Controller
package com.mutiple;
import com.mutiple.service.DateUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
@Controller
public class TestController {
@RequestMapping("/test")
public String test(Model model) {
return "test";
}
}
3.5 创建启动文件
package com.mutiple;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@ComponentScan(basePackages = {"com.mutiple"})
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
3.6 启动项目
4. 创建子级 核心模块, 一般主要逻辑都写到此模块 如:Service,mapper,DAO等
4.1 模块内创建工具类 (用于测试)
package com.mutiple.service;
public class DateUtil {
public static long getUnixTime10() {
return System.currentTimeMillis() / 1000L;
}
}
4.2 配置 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>mutiple-project</artifactId>
<groupId>com.mutiple</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mutiple</groupId>
<artifactId>mutiple-service</artifactId>
<name>mutiple-service</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>mutiple-service</finalName>
</build>
</project>
5. 业务入口模块导入 核心代码模块
5.1 在 multiple-web 的 pom.xml 写入以下
<dependency>
<groupId>com.mutiple</groupId>
<artifactId>mutiple-service</artifactId>
<version>${project.parent.version}</version>
</dependency>
5.2 在 multiple-web 的 Controller 写入以下
@RequestMapping("/test")
public String test(Model model) {
model.addAttribute("time", DateUtil.getUnixTime10());
return "test";
}
5.3 在 multiple-web 的 test.html 写入以下
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-4.dtd">
<html xmlns:th="http://www.thymeleaf.org">
<head></head>
<body>
<div>
<h4>Hello World!!</h4>
<h6>时间戳: <strong th:text="${time}"></strong></h6>
</div>
</body>
</html>
5.4 最后重启
如果您觉得有帮助,欢迎点赞哦 ~ 谢谢!!