SpringBoot+Maven多模块项目(17)
工程结构:
父模块 multimodule-parent:
子模块 multimodule-core
子模块 multimodule-web(web,唯一有启动类的模块)
关系:
multimodule-web依赖 multimodule-core (需要把multimodule-core 包打到multimodule-web中)
一.创建Maven多模块项目
1.创建multimodule-parent模块
1.1创建父模块,用于管理各个模块
接下来,把src整个删掉,父工程不需要,因为父工程你就当它只有一个外壳就完了
pom.xml
<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.example.multimodule</groupId> <artifactId>multimodule-parent</artifactId> <version>1.0.0</version> <packaging>pom</packaging> <name>multimodule</name> <description>多模块项目父模块</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.3.RELEASE</version> </parent> <dependencies> <!-- https://mvnrepository.com/artifact/commons-io/commons-io --> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.6</version> </dependency> </dependencies> <modules> <module>multimodule-core</module> <module>multimodule-web</module> </modules> </project>
1.2创建子模块multimodule-core
pom.xml
<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> <groupId>com.example.multimodule</groupId> <artifactId>multimodule-parent</artifactId> <version>1.0.0</version> </parent> <artifactId>multimodule-core</artifactId> </project>
1.3 创建子模块multimodule-web
pom.xml
<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>
<groupId>com.example.multimodule</groupId>
<artifactId>multimodule-parent</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>multimodule-web</artifactId>
<packaging>jar</packaging>
<name>multimodule-web</name>
<description>multimodule-web依赖 multimodule-core </description>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.example.multimodule</groupId>
<artifactId>multimodule-core</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!-- 指定该Main Class为全局的唯一入口 -->
<mainClass>com.example.multimodule.web.WebApplication</mainClass>
<layout>ZIP</layout>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal><!--可以把依赖的包都打包到生成的Jar包中-->
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
二.代码测试
2.1multimodule-core模块下新建CoreTest类
package com.example.multimodule.core; public class CoreTest { public String show() { return "这是一个子模块core"; } }
2.2multimodule-web模块下新建WebApplication类
package com.example.multimodule.web; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class WebApplication { public static void main(String[] args) throws Exception { SpringApplication.run(WebApplication.class, args); } }
2.3multimodule-web模块下新建WebTestController类
package com.example.multimodule.web; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import com.example.multimodule.core.CoreTest; @RestController public class WebTestController { @GetMapping("/show") public String test() { CoreTest c=new CoreTest(); return c.show(); } }
三.打包运行测试
3.1Maven install
右键multimodule-core项目,选择Maven install
复制打包成功的multimodule-web-1.0.0.jar,把它拷贝到一个目录
3.2 java -jar 运行jar包
访问http://localhost:8080/show 地址(默认不带项目名),返回结果如下,证明最后的multimodule-web-1.0.0.jar包把要依赖的multimodule-core包,也打进去,并且整个springboot 项目打包成功.