# SpringBoot+Maven多模塊項目(創建、依賴、打包可執行jar包部署測試)完整流程
原文鏈接: https://zhuanlan.zhihu.com/p/471117796
開發環境:IDEA,SprngBoot 2.0.4,Maven 2.19.1
工程結構:
父工程father
子模塊 dao (用于持久化數據跟數據庫交互)
子模塊 entity (實體類)
子模塊 service (處理業務邏輯)
子模塊 web (頁面交互接收、傳遞數據,唯一有啟動類的模塊)
關系: web依賴 service、dao、entity
service依賴 dao、entity
dao依賴 entity
entity誰都不依賴,獨立的
這里我用比較常見的工程結構舉例說明,有些公司的項目可能會把模塊分的很細,或者會有兩個程序入口,也就是兩個可以啟動的模塊!這個我在文章最后會做說明!縷清了思路其實沒那么復雜!
### 一、創建Maven多模塊項目
1.先建立外層父工程 File →new →project 選擇Spring Initializr Next下一步到以下頁面
2.接下來,把src整個刪掉,父工程不需要,因為父工程你就當它只有一個外殼就完了
3.接下來創建子模塊 工程上右鍵 → new → Module 選擇Spring Initaializr 下一步
4.重復以上動作,創建dao模塊,service模塊,web模塊
5.service模塊和entity模塊一樣什么都不需要引入
6.dao模塊和web模塊可以根據實際需求選擇引入mysql,mybatis,redis,web這些,
7.刪除每個子模塊中沒用的文件,.mvn、.gitignore、daoiml、mvnw、mvnw.cmd文件只留下pom.xml
8.刪除除了web模塊以外其它模塊中的Applicatin啟動項,和resources目錄下的application.properties配置文件
以上動作操作完成以后如果你發現你的子模塊變成了文件夾,沒關系,找到Maven Projects刷新一下就好了
整理過后的項目結構是這樣的

以上項目的基本結構就完成了,接下來建立各自依賴
### 二、依賴關系
打開父pom.xml修改打包方式jar為pom,注意:build內容也需要做替換,因為默認的spring-boot-maven-plugin這種方式,等到后期打包的時候他會一直提示你,你引入的依賴不存在!代碼如下
~~~text
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="Maven - Page Not Found" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="Maven - Page Not Found http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--父pom.xml-->
<groupId>com.miu</groupId>
<artifactId>father</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>father</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<!--聲明你有四個兒子 -->
<modules>
<module>entity</module>
<module>dao</module>
<module>service</module>
<module>web</module>
</modules>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<skipTests>true</skipTests> <!--默認關掉單元測試 -->
</configuration>
</plugin>
</plugins>
</build>
</project>
~~~
**這里有個坑需要注意,dao、service、entity這三個模塊的pom.xml文件中不需要build 內容,直接干掉**
entity 的 pom.xml 內容
~~~text
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="Maven - Page Not Found" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="Maven - Page Not Found http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.miu</groupId>
<artifactId>entity</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>entity</name>
<description>Demo project for Spring Boot</description>
<!--聲明父模塊-->
<parent>
<groupId>com.miu</groupId>
<artifactId>father</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
~~~
dao 的 pom.xml 內容
~~~text
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="Maven - Page Not Found" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="Maven - Page Not Found http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--dao 模塊 pom.xml-->
<groupId>com.miu</groupId>
<artifactId>dao</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>dao</name>
<description>Demo project for Spring Boot</description>
<!--聲明父模塊-->
<parent>
<groupId>com.miu</groupId>
<artifactId>father</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--dao 模塊 引入entity模塊-->
<dependency>
<groupId>com.miu</groupId>
<artifactId>entity</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
~~~
service 模塊的 pom.xml 內容
~~~text
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="Maven - Page Not Found" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="Maven - Page Not Found http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.miu</groupId>
<artifactId>service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>service</name>
<description>Demo project for Spring Boot</description>
<!--聲明父模塊-->
<parent>
<groupId>com.miu</groupId>
<artifactId>father</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--service模塊 引入entity模塊-->
<dependency>
<groupId>com.miu</groupId>
<artifactId>entity</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!--service模塊 引入dao模塊-->
<dependency>
<groupId>com.miu</groupId>
<artifactId>dao</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
~~~
web模塊的 pom.xml 內容
**注意build部分,因為web模塊作為程序的入口啟動,所以它需要打包,并且要指定Main Class**
~~~text
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="Maven - Page Not Found" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="Maven - Page Not Found http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.miu</groupId>
<artifactId>web</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>web</name>
<description>Demo project for Spring Boot</description>
<!--聲明父模塊-->
<parent>
<groupId>com.miu</groupId>
<artifactId>father</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--web模塊 引入entity模塊-->
<dependency>
<groupId>com.miu</groupId>
<artifactId>entity</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!--web模塊 引入service模塊-->
<dependency>
<groupId>com.miu</groupId>
<artifactId>service</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!--web模塊 引入dao模塊-->
<dependency>
<groupId>com.miu</groupId>
<artifactId>dao</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!-- 指定該Main Class為全局的唯一入口 -->
<mainClass>com.miu.web.WebApplication</mainClass>
<layout>ZIP</layout>
</configuration>
<executions>
<execution>
<goals>
<!--可以把依賴的包都打包到生成的Jar包中-->
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
~~~
到此為止所有的依賴全部完成!接下來就是測試!這里只用簡單的測試來實驗!
最后把web模塊中的application.properties文件補充一下就OK了,因為引入了mysql,redis等配置,所以數據源是要配的,不然運行起來會報錯找不到數據源!
~~~text
server.port=8080
#-----------------------------------數據庫配置----------------------------------------
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=123
#------------------------------------redis配置---------------------------------------
spring.redis.database=0
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=
spring.redis.jedis.pool.max-active=8
spring.redis.jedis.pool.max-idle=8
spring.redis.jedis.pool.max-wait=-1ms
spring.redis.jedis.pool.min-idle=0
spring.redis.timeout=10000ms
~~~
一切準備就緒,開始運行web模塊下的啟動類進行測試
### 三、打包可執行jar
看到上面的頁面就證明模塊之間的依賴沒有問題,調用正常,我這里是用簡單的創建對象的這種方式來操作的,實際開發并不是這種操作,大部分都是通過 @Autowired 注解 來實現的注入,這里我就不做演示了,只要模塊之間調用沒問題,剩下的就是鋪代碼的事了,接下來還有最后一個打包問題,為什么要啰嗦那么多還要說打包問題呢,因為我建議在項目架構之初,除了搭框架以外,最好是在最開始的時候就測試一下打包,尤其是這種多模塊項目之間各種依賴的這種工程的打包,如果等你代碼寫的鋪天蓋地的時候你在去想怎么打包,到時候有你頭疼的!如果你是按照我本章的流程一步步下來的話,那么你完全不用擔心打包問題,因為所有的pom.xml有已經配置好了,只需要動手運行 package打包動作就行了,第一次打包不需要clean,記住以后每次打包之前clean一下,關于為什么打jar包,不打war包這個問題,還有其它會遇到的問題,在文章最后會做說明!

雙擊運行package,看到BUILD SUCCESS 就證明打包成功了,如此簡單?告訴你就是這么簡單,前提是你的每一個模塊下的pom.xml要配置好,誰需要打包,誰不需要打包,誰依賴誰,父工程是否聲明了子模塊,子模塊是否聲明了父工程是誰,這些是重點!

接下來去找你工程目錄,web文件夾下的target文件夾,剛才打包好的jar文件,就放在這里了
然后我把這個jar文件上傳到我的測試服務器,使用 java -jar web-0.0.1-SNAPSHOT.jar 命令來測試運行打包的可執行jar文件到底行不行!
運行成功,輸入我測試服務器地址測試也沒問題,到此為止全部搞定
## 聚合工程舉一個簡單的例子:
整個工程你就當作一個公司,父工程(退休了什么也不干)只需要聲明有幾個兒子(子模塊)就完事了,
子模塊web聲明父工程是誰,就當他是大兒子,公司他管事,pom.xml文件需要打包,需要build配置,需要其它三個兄弟幫助
其它子模塊聲明父工程是誰,之間關系都是兄弟,不需要打包,哪里需要去哪里!
在此我說一下重點和需要注意的地方!
1.父pom.xml 打包方式,jar要更改為pom,build 需要更改
2.不需要打包的模塊pom.xml文件中不要寫,全刪掉,例如有些工程中的common模塊,utils模塊,entity模塊,service模 塊都不需要打包
3.聲明父工程時,填寫父工程位置../pom.xml
4.關于applicatin.properties配置文件,只需要在啟動的模塊中配置就可以了,
5.關于打包為什么打包jar包,不打war包,打war包目的是war包可以運行在tomcat下,但是SpringBoot是內置tomcat,如果你打war包,前提是干掉內置的tomcat,然后才能打包,各種麻煩,直接打包可執行jar包,使用java -jar 命令就可以完美的運行起來很方便!
6.真實開發中使用@Autowired 注解 來實現注入,而不是new對象這種方式,所以可能會產生注入以后報錯,是因為你的啟動類上沒有配置掃描,使用
@ComponentScan(basePackages = "你的路徑")注解來解決,如果你使用的持久層是Mybatis,那么你的mapper也需要掃描,在啟動類上使用
@MapperScan("你的mapper文件地址")注解來解決,算了還是貼個圖片吧

- Golang
- Beego框架
- Gin框架
- gin框架介紹
- 使用Gin web框架的知名開源線上項目
- go-admin-gin
- air 熱啟動
- 完整的form表單參數驗證語法
- Go 語言入門練手項目推薦
- Golang是基于多線程模型
- golang 一些概念
- Golang程序開發注意事項
- fatal error: all goroutines are asleep - deadlock
- defer
- Golang 的內建調試器
- go部署
- golang指針重要性
- 包(golang)
- Golang框架選型比較: goframe, beego, iris和gin
- GoFrame
- golang-admin-項目
- go module的使用方法及原理
- go-admin支持多框架的后臺系統(go-admin.cn)
- docker gocv
- go-fac
- MSYS2
- 企業開發框架系統推薦
- gorm
- go-zero
- 優秀系統
- GinSkeleton(gin web 及gin 知識)
- 一次 request -> response 的生命周期概述
- 路由與路由組以及gin源碼學習
- 中間件以及gin源碼學習
- golang項目部署
- 獨立部署golang
- 代理部署golang
- 容器部署golang
- golang交叉編譯
- goravel
- kardianos+gin 項目作為windows服務運行
- go env
- 適用在Windows、Linux和macOS環境下打包Go應用程序的詳細步驟和命令
- Redis
- Dochub
- Docker部署開發go環境
- Docker部署運行go環境
- dochub說明
- Vue
- i18n
- vue3
- vue3基本知識
- element-plus 表格單選
- vue3后臺模板
- Thinkphp
- Casbin權限控制中間件
- 容器、依賴注入、門面、事件、中間件
- tp6問答
- 偽靜態
- thinkphp-queue
- think-throttle
- thinkphp隊列queue的一些使用說明,queue:work和queue:listen的區別
- ThinkPHP6之模型事件的觸發條件
- thinkphp-swoole
- save、update、insert 的區別
- Socket
- workerman
- 介紹
- 從ThinkPHP6移植到Webman的一些技術和經驗(干貨)
- swoole
- swoole介紹
- hyperf
- hf官網
- Swoft
- swoft官網
- easyswoole
- easyswoole官網地址
- EASYSWOOLE 聊天室DEMO
- socket問答
- MySQL
- 聚簇索引與非聚簇索引
- Mysql使用max獲取最大值細節
- 主從復制
- 隨機生成20萬User表的數據
- MySQL進階-----前綴索引、單例與聯合索引
- PHP
- 面向切面編程AOP
- php是單線程的一定程度上也可以看成是“多線程”
- PHP 線程,進程、并發、并行 的理解
- excel數據畫表格圖片
- php第三方包
- monolog/monolog
- league/glide
- 博客-知識網站
- php 常用bc函數
- PHP知識點的應用場景
- AOP(面向切面編程)
- 注解
- 依賴注入
- 事件機制
- phpspreadsheet導出數據和圖片到excel
- Hyperf
- mineAdmin
- 微服務
- nacos注冊服務
- simps-mqtt連接客戶端simps
- Linux
- 切換php版本
- Vim
- Laravel
- RabbitMQ
- thinkphp+rabbitmq
- 博客
- Webman框架
- 框架注意問題
- 關于內存泄漏
- 移動端自動化
- 懶人精靈
- 工具應用
- render
- gitlab Sourcetree
- ssh-agent失敗 錯誤代碼-1
- 資源網站
- Git
- wkhtmltopdf
- MSYS2 介紹
- powershell curl 使用教程
- NSSM(windows服務工具)
- MinGW64
- 知識擴展
- 對象存儲系統
- minio
- 雪花ID
- 請求body參數類型
- GraphQL
- js 深拷貝
- window 共享 centos文件夾
- 前端get/post 請求 特殊符號 “+”傳參數問題
- 什么是SCM系統?SCM系統與ERP系統有什么區別?
- nginx 日志格式統一為 json
- 特殊符號怎么打
- 收藏網址
- 收藏-golang
- 收藏-vue3
- 收藏-php
- 收藏-node
- 收藏-前端
- 規劃ITEM
- 旅游類
- 人臉識別
- dlib
- Docker&&部署
- Docker-compose
- Docker的網絡模式
- rancher
- DHorse
- Elasticsearch
- es與kibana都docke連接
- 4種數據同步到Elasticsearch方案
- GPT
- 推薦系統
- fastposter海報生成
- elasticsearch+logstash+kibana
- beego文檔系統-MinDoc
- jeecg開源平臺
- Java
- 打包部署
- spring boot
- 依賴
- Maven 相關 命令
- Gradle 相關命令
- mybatis
- mybatis.plus
- spring boot 模板引擎
- SpringBoot+Maven多模塊項目(創建、依賴、打包可執行jar包部署測試)完整流程
- Spring Cloud
- Sentinel
- nacos
- Apollo
- java推薦項目
- gradle
- Maven
- Nexus倉庫管理器
- Python
- Masonite框架
- scrapy
- Python2的pip2
- Python3 安裝 pip3
- 安全攻防
- 運維技術
- 騰訊云安全加固建議
- 免費freessl證書申請
- ruby
- homeland
- Protobuf
- GIT
- FFMPEG
- 命令說明
- 音頻
- ffmpeg合并多個MP4視頻
- NODEJS
- 開發npm包
- MongoDB
- php-docker-mongodb環境搭建
- mongo基本命令
- Docker安裝MongoDB最新版并連接
- 少兒編程官網
- UI推薦
- MQTT
- PHP連接mqtt
- EMQX服務端
- php搭建mqtt服務端