springboot多模块开发
项目结构
- springboot
- demo-web //Controller在的那一层
- pom.xml
- demo-common
- pom.xml
- demo-system
- pom.xml
- pom.xml
- demo-web //Controller在的那一层
依赖关系
上图是项目的依赖关系,由于demo-web中也用到了demo-common的对象,实际上,上图的demo-system等于demo-common加上demo-system,所以也可以下图表示
打包和运行
在demo-springboot-multi的pom.xml文件目录下进行打包,
打完包后在demo-web目录下的target目录下,输入
java -jar demo-web-0.0.1.jar
项目即可运行
springboot - 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.demo</groupId>
<artifactId>demo-springboot-multi</artifactId>
<version>0.0.1</version>
<name>demo-springboot-multi</name>
<description>Demo project for Spring Boot</description>
<packaging>pom</packaging>
<modules>
<module>demo-system</module>
<module>demo-web</module>
<module>demo-common</module>
</modules>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<java.version>1.6</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<skipTests>true</skipTests>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.5.4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.demo</groupId>
<artifactId>demo-common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.demo</groupId>
<artifactId>demo-system</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.demo</groupId>
<artifactId>demo-web</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.5.3</version>
</plugin>
</plugins>
</build>
</project>
demo-web - 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>demo-springboot-multi</artifactId>
<groupId>com.demo</groupId>
<version>0.0.1</version>
</parent>
<artifactId>demo-web</artifactId>
<version>0.0.1</version>
<name>demo-web</name>
<description>Demo project for Spring Boot</description>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.demo</groupId>
<artifactId>demo-system</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.5.3</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
demo-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>demo-springboot-multi</artifactId>
<groupId>com.demo</groupId>
<version>0.0.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>demo-common</artifactId>
<dependencies>
</dependencies>
</project>
demo-system - 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>demo-springboot-multi</artifactId>
<groupId>com.demo</groupId>
<version>0.0.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>demo-system</artifactId>
<version>0.0.1</version>
<dependencies>
<!-- 子模块-->
<dependency>
<groupId>com.demo</groupId>
<artifactId>demo-common</artifactId>
<version>0.0.1</version>
</dependency>
<!-- 子模块-->
</dependencies>
</project>