springboot項(xiàng)目父子多模塊打包方式
springboot項(xiàng)目父子多模塊打jar包
1.項(xiàng)目工程架構(gòu)

- Student:父工程Maven
- main:子模塊,也是啟動(dòng)類(lèi)MainApplication所在的模塊,main依賴(lài)其他模塊
- common、core、student等:子模塊。除main模塊之外,依賴(lài)部分或不依賴(lài)的其他模塊
注意事項(xiàng):父工程創(chuàng)建為Maven,啟動(dòng)類(lèi)所在模塊創(chuàng)建時(shí)一定要用SpringBoot項(xiàng)目,其他子模塊創(chuàng)建時(shí)Maven、SpringBoot都可以。
2.各個(gè)模塊pom文件配置
父工程Student
<?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.example</groupId>
<artifactId>Student</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<name>Student</name>
<modules>
<module>main</module>
<module>student</module>
<module>struct</module>
<module>core</module>
<module>common</module>
</modules>
</project>
注:在父工程的pom文件中一定不能有spring-boot-maven-plugin插件,否者這個(gè)插件就將傳遞到每一個(gè)子模塊中,打包會(huì)出錯(cuò);
main啟動(dòng)類(lèi)模塊
<?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修改為父工程信息 -->
<parent>
<groupId>com.example</groupId>
<artifactId>Student</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.example</groupId>
<artifactId>main</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>main</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>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
</dependency>
<!-- ************************************************************************ -->
<!-- 子模塊依賴(lài) -->
<dependency>
<groupId>com.example</groupId>
<artifactId>core</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>student</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<finalName>main</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!-- 指定該Main Class為全局的唯一入口 -->
<mainClass>com.example.main.MainApplication</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal><!--可以把依賴(lài)的包都打包到生成的Jar包中-->
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
注:在該子模塊的pom文件中必須引入spring-boot-maven-plugin打包插件,并且要配置將所依賴(lài)的jar包全都打到這一包中,如果在其他模塊中引入了spring-boot-maven-plugin將會(huì)每個(gè)模塊都單獨(dú)打成一個(gè)包,無(wú)法拉取到啟動(dòng)類(lèi)所在包中;
core子模塊pom文件,其他子模塊類(lèi)似
<?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為父工程信息 -->
<parent>
<artifactId>Student</artifactId>
<groupId>com.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>core</artifactId>
<dependencies>
<!-- dependency依賴(lài)區(qū) -->
</dependencies>
</project>
3.執(zhí)行生成jar包和部署過(guò)程
通過(guò)maven菜單,在父工程下package(或mvn package命令)

這里需要注意的是雖然我們是將所有的jar都搭到main這個(gè)jar中,但是我們執(zhí)行clean和package卻是要對(duì)父工程進(jìn)行;

部署:將打好的jar包上傳到服務(wù)器的任意路徑上,執(zhí)行 java -jar main.jar(前提需要安裝jdk并配置環(huán)境變量,并且版本和項(xiàng)目構(gòu)建的版本一致)
注意:在linux環(huán)境下(通過(guò)xshell連接)運(yùn)行"java -jar xxx.jar",項(xiàng)目成功運(yùn)行后關(guān)閉窗口或者ctrl+z,會(huì)直接關(guān)閉掉項(xiàng)目,所以需要通過(guò)nohup和&命令配合:“nohup java -jar xxx.jar &”,在目錄下會(huì)生成nohup.out日志文件查看:"tail -f nohup.out"就可以查看項(xiàng)目日志了。
springboot項(xiàng)目父子多模塊打war包
修改main啟動(dòng)類(lèi)模塊pom
<!-- 打包方式為war包 -->
<packaging>war</packaging>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- 移除嵌入式tomcat插件 -->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
/exclusions>
</dependency>
<!-- war包插件 -->
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.3</version>
<configuration>
<!-- 把class打包jar作為附件 -->
<attachClasses>true</attachClasses>
</configuration>
</plugin>
修改springboot啟動(dòng)類(lèi),啟動(dòng)類(lèi)繼承SpringBootServletInitializer,重寫(xiě)configure方法,讓Servlet容器啟動(dòng)時(shí)啟動(dòng)Spring Boot
@SpringBootApplication
@ComponentScan("com.example.**")
@MapperScan("com.example.**.mapper")
public class MainApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
/**
* 繼承SpringBootServletInitializer ,覆蓋configure(),把啟動(dòng)類(lèi)Application注冊(cè)進(jìn)去。
* 外部web應(yīng)用服務(wù)器構(gòu)建Web Application Context的時(shí)候,會(huì)把啟動(dòng)類(lèi)添加進(jìn)去
* @param builder
* @return
*/
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(MainApplication.class);
}
}
其他說(shuō)明,當(dāng)前環(huán)境及工具版本:
springboot版本2.4.5
jdk版本1.8.0
tomcat版本apache-tomcat-8.5.45
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- SpringBoot多模塊掃描包問(wèn)題及解決
- SpringBoot多模塊自動(dòng)配置失效問(wèn)題的解決方案
- SpringBoot多模塊如何統(tǒng)一管理
- 多模塊的springboot項(xiàng)目發(fā)布指定模塊的腳本方式
- idea創(chuàng)建Springboot多模塊項(xiàng)目(聚合項(xiàng)目)
- SpringBoot多模塊搭建的實(shí)現(xiàn)示例
- springboot的maven多模塊混淆jar包的實(shí)現(xiàn)方法
- SpringBoot多模塊打包部署Docker的項(xiàng)目實(shí)戰(zhàn)
- springboot結(jié)合maven實(shí)現(xiàn)多模塊打包
- SpringBoot單體多模塊項(xiàng)目環(huán)境搭建
相關(guān)文章
spring boot使用@Async異步注解的實(shí)現(xiàn)原理+源碼
通常我們都是采用多線程的方式來(lái)實(shí)現(xiàn)上述業(yè)務(wù)功能,但spring 提供更優(yōu)雅的方式來(lái)實(shí)現(xiàn)上述功能,就是@Async 異步注解,在方法上添加@Async,spring就會(huì)借助AOP,異步執(zhí)行方法,接下來(lái)通過(guò)本文給大家介紹spring boot異步注解的相關(guān)知識(shí),一起看看吧2021-06-06
Spring Boot實(shí)現(xiàn)Undertow服務(wù)器同時(shí)支持HTTP2、HTTPS的方法
這篇文章考慮如何讓Spring Boot應(yīng)用程序同時(shí)支持HTTP和HTTPS兩種協(xié)議。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-12-12
Spring?Boot統(tǒng)一處理全局異常的實(shí)戰(zhàn)教程
最近在做項(xiàng)目時(shí)需要對(duì)異常進(jìn)行全局統(tǒng)一處理,所以下面這篇文章主要給大家介紹了關(guān)于Spring?Boot統(tǒng)一處理全局異常的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-12-12
JAVA 集成 PF4J 插件框架的應(yīng)用場(chǎng)景分析
PF4J是一個(gè)強(qiáng)大的Java插件框架,允許開(kāi)發(fā)者將應(yīng)用程序分解為可擴(kuò)展的模塊,本文介紹了PF4J的基本概念和如何在Java項(xiàng)目中集成,本文給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧2025-03-03
SpringCloud+Redis實(shí)現(xiàn)Api接口限流防止惡意刷接口
接口限流是為了保護(hù)系統(tǒng)和服務(wù),防止因?yàn)檫^(guò)多的請(qǐng)求而崩潰,本文主要介紹了SpringCloud+Redis實(shí)現(xiàn)Api接口限流防止惡意刷接口,具有一定的參考價(jià)值,感興趣的可以了解一下2024-03-03
Java線程中的Thread.yield()詳細(xì)解析
這篇文章主要介紹了Java線程中的Thread.yield()詳細(xì)解析,yield()讓當(dāng)前線程從運(yùn)行狀態(tài)?轉(zhuǎn)為?就緒狀態(tài),以允許具有相同優(yōu)先級(jí)的其他線程獲得運(yùn)行機(jī)會(huì),需要的朋友可以參考下2023-11-11
使用Java模擬鼠標(biāo)和鍵盤(pán)的詳細(xì)操作步驟
這篇文章主要介紹了使用Java模擬鼠標(biāo)和鍵盤(pán)的詳細(xì)操作步驟,要運(yùn)行上面提供的Java程序,您需要遵循幾個(gè)步驟來(lái)設(shè)置Java環(huán)境、編寫(xiě)程序代碼,并執(zhí)行該程序,文中有相關(guān)的代碼示例,需要的朋友可以參考下2024-05-05
SpringBoot默認(rèn)使用HikariDataSource數(shù)據(jù)源方式
這篇文章主要介紹了SpringBoot默認(rèn)使用HikariDataSource數(shù)據(jù)源方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10

