塊項(xiàng)SpringBoot+Maven多模目(創(chuàng)建、依賴、打包可執(zhí)行jar包部署測(cè)試)完整流程

轉(zhuǎn)載自:https://blog.csdn.net/baidu_41885330/article/details/81875395

總結(jié)的很好!謝謝原博主,感謝分享

開發(fā)環(huán)境:IDEA,

SprngBoot 2.0.4,

Maven 2.19.1

工程結(jié)構(gòu):

? ? ? ? ? ? ? ? ? ? ? ? ? ?? 父工程father

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? 子模塊? dao ? ?? (用于持久化數(shù)據(jù)跟數(shù)據(jù)庫交互)

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? 子模塊? entity ?? (實(shí)體類)

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? 子模塊? service (處理業(yè)務(wù)邏輯)

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? 子模塊? web ? ? ? (頁面交互接收、傳遞數(shù)據(jù),唯一有啟動(dòng)類的模塊)

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 關(guān)系: ? ? ? ? web依賴 service、dao、entity

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? service依賴 dao、entity

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? dao依賴 entity

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? entity誰都不依賴,獨(dú)立的

這里我用比較常見的工程結(jié)構(gòu)舉例說明,有些公司的項(xiàng)目可能會(huì)把模塊分的很細(xì),或者會(huì)有兩個(gè)程序入口,也就是兩個(gè)可以啟動(dòng)的模塊!這個(gè)我在文章最后會(huì)做說明!縷清了思路其實(shí)沒那么復(fù)雜!

一,創(chuàng)建Maven多模塊項(xiàng)目

先建立外層父工程 ? ? ? ? File →new →project? 選擇Spring Initializr ? ? ? ?? Next下一步到以下頁面

工程結(jié)構(gòu)如下

接下來,把src整個(gè)刪掉,父工程不需要,因?yàn)楦腹こ棠憔彤?dāng)它只有一個(gè)外殼就完了

接下來創(chuàng)建子模塊? 工程上右鍵 → new → Module? 選擇Spring Initaializr? 下一步

重復(fù)以上動(dòng)作,創(chuàng)建dao模塊,service模塊,web模塊

service模塊和entity模塊一樣什么都不需要引入

dao模塊和web模塊可以根據(jù)實(shí)際需求選擇引入mysql,mybatis,redis,web這些,我把我的貼出來

刪除每個(gè)子模塊中沒用的文件,.mvn、.gitignore、daoiml、mvnw、mvnw.cmd文件只留下pom.xml

刪除除了web模塊以外其它模塊中的Applicatin啟動(dòng)項(xiàng),和resources目錄下的application.properties配置文件

以上動(dòng)作操作完成以后如果你發(fā)現(xiàn)你的子模塊變成了文件夾,沒關(guān)系,找到Maven Projects刷新一下就好了

整理過后的項(xiàng)目結(jié)構(gòu)是這樣的

以上項(xiàng)目的基本結(jié)構(gòu)就完成了,接下來建立各自依賴

二、依賴關(guān)系

打開父pom.xml修改打包方式j(luò)ar為pom,注意:build內(nèi)容也需要做替換,因?yàn)槟J(rèn)的spring-boot-maven-plugin這種方式,等到后期打包的時(shí)候他會(huì)一直提示你,你引入的依賴不存在!代碼如下



xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

? ? <!--父pom.xml-->

? ? <groupId>com.miu</groupId>

? ? <artifactId>father</artifactId>

0.0.1-SNAPSHOT

? ? <packaging>pom</packaging>

? ? <name>father</name>

Demo projectforSpring Boot

? ? <parent>

? ? ? ? <groupId>org.springframework.boot</groupId>

? ? ? ? <artifactId>spring-boot-starter-parent</artifactId>

2.0.4.RELEASE

? ? ? ? <relativePath/> <!-- lookup parent from repository -->

? ? </parent>

? ? <properties>

UTF-8

UTF-8

1.8

? ? </properties>

? ? <!--聲明你有四個(gè)兒子 -->

? ? <modules>

entity

dao

service

web

? ? </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>

3.1

? ? ? ? ? ? ? ? <configuration>

? ? ? ? ? ? ? ? ? ? <source>${java.version}</source>

? ? ? ? ? ? ? ? ? ? <target>${java.version}</target>

? ? ? ? ? ? ? ? </configuration>

? ? ? ? ? ? </plugin>

? ? ? ? ? ? <plugin>

? ? ? ? ? ? ? ? <groupId>org.apache.maven.plugins</groupId>

? ? ? ? ? ? ? ? <artifactId>maven-surefire-plugin</artifactId>

2.19.1

? ? ? ? ? ? ? ? <configuration>

true? ?

? ? ? ? ? ? ? ? </configuration>

? ? ? ? ? ? </plugin>

? ? ? ? </plugins>

? ? </build>

</project>

這里有個(gè)坑需要注意,dao、service、entity這三個(gè)模塊的pom.xml文件中不需要build 內(nèi)容,直接干掉

entity 的 pom.xml 內(nèi)容


xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

? ? <groupId>com.miu</groupId>

? ? <artifactId>entity</artifactId>

0.0.1-SNAPSHOT

? ? <packaging>jar</packaging>

? ? <name>entity</name>

Demo projectforSpring Boot

? ? <!--聲明父模塊-->

? ? <parent>

? ? ? ? <groupId>com.miu</groupId>

? ? ? ? <artifactId>father</artifactId>

0.0.1-SNAPSHOT

? ? ? ? <relativePath>../pom.xml</relativePath>

? ? </parent>

? ? <properties>

UTF-8

UTF-8

1.8

? ? </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 內(nèi)容


xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

? ? <!--dao 模塊 pom.xml-->

? ? <groupId>com.miu</groupId>

? ? <artifactId>dao</artifactId>

0.0.1-SNAPSHOT

? ? <packaging>jar</packaging>

? ? <name>dao</name>

Demo projectforSpring Boot

? ? <!--聲明父模塊-->

? ? <parent>

? ? ? ? <groupId>com.miu</groupId>

? ? ? ? <artifactId>father</artifactId>

0.0.1-SNAPSHOT

? ? ? ? <relativePath>../pom.xml</relativePath>

? ? </parent>

? ? <properties>

UTF-8

UTF-8

1.8

? ? </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>

1.3.2

? ? ? ? </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>

0.0.1-SNAPSHOT

? ? ? ? </dependency>

? ? </dependencies>

</project>

service 模塊的 pom.xml 內(nèi)容


xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

? ? <groupId>com.miu</groupId>

? ? <artifactId>service</artifactId>

0.0.1-SNAPSHOT

? ? <packaging>jar</packaging>

? ? <name>service</name>

Demo projectforSpring Boot

? ? <!--聲明父模塊-->

? ? <parent>

? ? ? ? <groupId>com.miu</groupId>

? ? ? ? <artifactId>father</artifactId>

0.0.1-SNAPSHOT

? ? ? ? <relativePath>../pom.xml</relativePath>

? ? </parent>

? ? <properties>

UTF-8

UTF-8

1.8

? ? </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>

0.0.1-SNAPSHOT

? ? ? ? </dependency>

? ? ? ? <!--service模塊 引入dao模塊-->

? ? ? ? <dependency>

? ? ? ? ? ? <groupId>com.miu</groupId>

? ? ? ? ? ? <artifactId>dao</artifactId>

0.0.1-SNAPSHOT

? ? ? ? </dependency>

? ? </dependencies>

</project>

web模塊的 pom.xml 內(nèi)容

?注意build部分,因?yàn)閣eb模塊作為程序的入口啟動(dòng),所以它需要打包,并且要指定Main Class


xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

? ? <groupId>com.miu</groupId>

? ? <artifactId>web</artifactId>

0.0.1-SNAPSHOT

? ? <packaging>jar</packaging>

? ? <name>web</name>

Demo projectforSpring Boot

? ? <!--聲明父模塊-->

? ? <parent>

? ? ? ? <groupId>com.miu</groupId>

? ? ? ? <artifactId>father</artifactId>

0.0.1-SNAPSHOT

? ? ? ? <relativePath>../pom.xml</relativePath>

? ? </parent>

? ? <properties>

UTF-8

UTF-8

1.8

? ? </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>

1.3.2

? ? ? ? </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>

0.0.1-SNAPSHOT

? ? ? ? </dependency>

? ? ? ? <!--web模塊 引入service模塊-->

? ? ? ? <dependency>

? ? ? ? ? ? <groupId>com.miu</groupId>

? ? ? ? ? ? <artifactId>service</artifactId>

0.0.1-SNAPSHOT

? ? ? ? </dependency>

? ? ? ? <!--web模塊 引入dao模塊-->

? ? ? ? <dependency>

? ? ? ? ? ? <groupId>com.miu</groupId>

? ? ? ? ? ? <artifactId>dao</artifactId>

0.0.1-SNAPSHOT

? ? ? ? </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>

? ? ? ? ? ? ? ? ? ? ? ? ? ? <goal>repackage</goal><!--可以把依賴的包都打包到生成的Jar包中-->

? ? ? ? ? ? ? ? ? ? ? ? </goals>

? ? ? ? ? ? ? ? ? ? </execution>

? ? ? ? ? ? ? ? </executions>

? ? ? ? ? ? </plugin>

? ? ? ? </plugins>

? ? </build>

</project>

到此為止所有的依賴全部完成!接下來就是測(cè)試!這里只用簡(jiǎn)單的測(cè)試來實(shí)驗(yàn)!

三、代碼測(cè)試

entity模塊中創(chuàng)建? EntiyTest類

dao模塊中創(chuàng)建? DaoTest類

service模塊中創(chuàng)建ServiceTest類


Web模塊中創(chuàng)建WebTest類

最后把web模塊中的application.properties文件補(bǔ)充一下就OK了,因?yàn)橐肓薽ysql,redis等配置,所以數(shù)據(jù)源是要配的,不然運(yùn)行起來會(huì)報(bào)錯(cuò)找不到數(shù)據(jù)源!

server.port=8080

#-----------------------------------數(shù)據(jù)庫配置----------------------------------------

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

? ? ?? 一切準(zhǔn)備就緒,開始運(yùn)行web模塊下的啟動(dòng)類進(jìn)行測(cè)試

四、打包可執(zhí)行jar

看到上面的頁面就證明模塊之間的依賴沒有問題,調(diào)用正常,我這里是用簡(jiǎn)單的創(chuàng)建對(duì)象的這種方式來操作的,實(shí)際開發(fā)并不是這種操作,大部分都是通過 @Autowired 注解 來實(shí)現(xiàn)的注入,這里我就不做演示了,只要模塊之間調(diào)用沒問題,剩下的就是鋪代碼的事了,接下來還有最后一個(gè)打包問題,為什么要啰嗦那么多還要說打包問題呢,因?yàn)槲医ㄗh在項(xiàng)目架構(gòu)之初,除了搭框架以外,最好是在最開始的時(shí)候就測(cè)試一下打包,尤其是這種多模塊項(xiàng)目之間各種依賴的這種工程的打包,如果等你代碼寫的鋪天蓋地的時(shí)候你在去想怎么打包,到時(shí)候有你頭疼的!如果你是按照我本章的流程一步步下來的話,那么你完全不用擔(dān)心打包問題,因?yàn)樗械膒om.xml有已經(jīng)配置好了,只需要?jiǎng)邮诌\(yùn)行 package打包動(dòng)作就行了,第一次打包不需要clean,記住以后每次打包之前clean一下,關(guān)于為什么打jar包,不打war包這個(gè)問題,還有其它會(huì)遇到的問題,在文章最后會(huì)做說明!

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

接下來去找你工程目錄,web文件夾下的target文件夾,剛才打包好的jar文件,就放在這里了

然后我把這個(gè)jar文件上傳到我的測(cè)試服務(wù)器,使用 java -jar? web-0.0.1-SNAPSHOT.jar 命令來測(cè)試運(yùn)行打包的可執(zhí)行jar文件到底行不行!

運(yùn)行成功,輸入我測(cè)試服務(wù)器地址測(cè)試也沒問題,到此為止全部搞定

聚合工程舉一個(gè)簡(jiǎn)單的例子,

整個(gè)工程你就當(dāng)作一個(gè)公司,父工程(退休了什么也不干)只需要聲明有幾個(gè)兒子(子模塊)就完事了,

子模塊web聲明父工程是誰,就當(dāng)他是大兒子,公司他管事,pom.xml文件需要打包,需要build配置,需要其它三個(gè)兄弟幫助

其它子模塊聲明父工程是誰,之間關(guān)系都是兄弟,不需要打包,哪里需要去哪里!

在此我說一下重點(diǎn)和需要注意的地方!

1.父pom.xml 打包方式,jar要更改為pom,build 需要更改

2.不需要打包的模塊pom.xml文件中不要寫<build>,全刪掉,例如有些工程中的common模塊,utils模塊,entity模塊,service模? 塊都不需要打包

3.聲明父工程時(shí),填寫父工程位置<relativePath>../pom.xml</relativePath>

4.關(guān)于applicatin.properties配置文件,只需要在啟動(dòng)的模塊中配置就可以了,

5.關(guān)于打包為什么打包jar包,不打war包,打war包目的是war包可以運(yùn)行在tomcat下,但是SpringBoot是內(nèi)置tomcat,如果你打war包,前提是干掉內(nèi)置的tomcat,然后才能打包,各種麻煩,直接打包可執(zhí)行jar包,使用java -jar 命令就可以完美的運(yùn)行起來很方便!

6.真實(shí)開發(fā)中使用@Autowired 注解 來實(shí)現(xiàn)注入,而不是new對(duì)象這種方式,所以可能會(huì)產(chǎn)生注入以后報(bào)錯(cuò),是因?yàn)槟愕膯?dòng)類上沒有配置掃描,使用

@ComponentScan(basePackages = "你的路徑")注解來解決,如果你使用的持久層是Mybatis,那么你的mapper也需要掃描,在啟動(dòng)類上使用

@MapperScan("你的mapper文件地址")注解來解決,算了還是貼個(gè)圖片吧

不羅嗦了,就到這里吧,一個(gè)文章寫了兩個(gè)小時(shí)可見我的良苦用心,(關(guān)鍵是我被這個(gè)多模塊打包問題困擾了好長(zhǎng)時(shí)間,網(wǎng)上各種找解決辦法,說的天花亂墜,狗屁不通,服的五體投地

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容