SpringBoot創(chuàng)建maven多模塊項目實戰(zhàn)代碼
工作中一直都是一個人奮戰(zhàn)一人一個項目,使用maven管理,看這個也挺好,但是總感覺沒有充分發(fā)揮maven的功能,于是研究了一下這個,網(wǎng)上關于這個的文章很多,雖然不是很好,但我從中收獲了很多,在這集百家所長,寫一份實戰(zhàn)記錄,大家跟著我一塊做吧!
聲明:構建多模塊不是最難的,難點是如果把多模塊打包成一個執(zhí)行jar。
SpringBoot官方推崇的是富jar,也就是jar文件啟動項目,所以如果在這里打war包我不具體介紹,如果需要的朋友可以給我留言,我回復。
建議clone項目后,在看教程(有不足的地方希望大家保函,指出來,我們一起學習改進)
github:https://github.com/lxchinesszz/multi-boluome.git
構建工程
1.首先第一步,在github上創(chuàng)建一個公共項目項目名 multi-boluome

2.把倉庫同步到本地,使用Intellij idea打開,把普通項目轉(zhuǎn)換為maven項目【右鍵:Add Frameworks Support】


3.刪除除了pom文件之外的文件也就是src刪除

4.然后新建File->New->module以此創(chuàng)建(此時會看到pom文件的變化)
- web
- dao
- domain
- service

==提示:一定要把外面的pom文件中的pom==
5.引入SpringBoot依賴 這個我在外面寫的(這個根據(jù)個人)
外面的pom文件內(nè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">
<modelVersion>4.0.0</modelVersion>
<groupId>blm.server</groupId>
<artifactId>multi-boluome</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>web</module>
<module>service</module>
<module>dao</module>
<module>domain</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.3.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>
<kotlin.version>1.0.6</kotlin.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<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>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!--引入mock框架-->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.10.19</version>
</dependency>
<!--rabbitmq-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<!-- The plugin rewrites your manifest -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.3.0.RELEASE</version>
<configuration><!-- 指定該Main Class為全局的唯一入口 -->
<mainClass>iflyer.IflyerApplication</mainClass>
<layout>ZIP</layout>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal><!--可以把依賴的包都打包到生成的Jar包中-->
</goals>
<!--可以生成不含依賴包的不可執(zhí)行Jar包-->
<!-- configuration>
<classifier>exec</classifier>
</configuration> -->
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
6.開始編寫domain層(這里我用mongodb數(shù)據(jù)庫)
7.dao層我要用到數(shù)據(jù)庫,所以在resource中添加配置信息
8.service層中我有用到freemarker的模板引擎,所以添加配置信息
9.web層編寫啟動類,main方法,main方法要放到目錄外層,根據(jù)約定哦!
10.每個層及都有自己的依賴
eg:
- dao層依賴domain
- service依賴dao和domain
- web層依賴service、dao、domain
這個關系層次一定要告訴,編輯器,如下設置
右鍵:Open Module Settings打開
idea修改依賴

11.run一下啟動類吧!工程可以啟動了
如果出現(xiàn)一下錯誤
Error:java: Annotation processing is not supported for module cycles. Please ensure that all modules
說明依賴關系錯了,繼續(xù)看第10步驟吧。
打包發(fā)布jar文件
1.在啟動類中修改pom文件(也就是web層的)
<build>
<!-- 為jar包取名 -->
<finalName>blm-start</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.3.0.RELEASE</version>
</plugin>
</plugins>
</build>
2.在外層pom中構建插件
<build>
<plugins>
<plugin>
<!-- The plugin rewrites your manifest -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.3.0.RELEASE</version>
<configuration><!-- 指定該Main Class為全局的唯一入口 -->
<mainClass>com.Application</mainClass>
<layout>ZIP</layout>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal><!--可以把依賴的包都打包到生成的Jar包中-->
</goals>
<!--可以生成不含依賴包的不可執(zhí)行Jar包-->
<!-- configuration>
<classifier>exec</classifier>
</configuration> -->
</execution>
</executions>
</plugin>
</plugins>
</build>
3.打包吧,mvn package —Dmaven.test.skip=true 跳過測試
[INFO] multi-boluome ...................................... SUCCESS [ 1.707 s] [INFO] domain ............................................. SUCCESS [ 2.463 s] [INFO] dao ................................................ SUCCESS [ 0.592 s] [INFO] service ............................................ SUCCESS [ 0.606 s] [INFO] web ................................................ SUCCESS [ 1.135 s] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 7.290 s [INFO] Finished at: 2017-01-20T17:05:14+08:00 [INFO] Final Memory: 42M/265M [INFO] ------------------------------------------------------------------------ KK-MINI:multi-boluome liuxin$ INFO] Building jar: /Users/liuxin/git/模仿項目/multi-boluome/web/target/blm-start.jar 構建文件在這個目錄下
very Good!開始飛吧
==提醒:所有模塊里面的父節(jié)點都是一樣的哦,不然會報錯 unknow.version==
WARNING] ‘parent.relativePath' of POM blm.server:domain:[unknown-version] 類似
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
BeanDefinitionRegistryPostProcessor如何動態(tài)注冊Bean到Spring
這篇文章主要介紹了BeanDefinitionRegistryPostProcessor如何動態(tài)注冊Bean到Spring,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
Java并發(fā)編程之ConcurrentLinkedQueue隊列詳情
這篇文章主要介紹了Java并發(fā)編程之ConcurrentLinkedQueue隊列詳情,ConcurrentLinkedQueue?內(nèi)部的隊列使用單向鏈表方式實現(xiàn),下文更多相關內(nèi)容敘述需要的小伙伴可以參考一下2022-04-04
Javaweb開發(fā)中通過Servlet生成驗證碼圖片
這篇文章主要為大家詳細介紹了Javaweb開發(fā)中通過Servlet生成驗證碼圖片的相關資料,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-05-05
關于struts返回對象json格式數(shù)據(jù)的方法
以下為大家介紹,關于struts返回對象json格式數(shù)據(jù)的方法,希望對有需要的朋友有所幫助。2013-04-04
SpringBoot2 整合FreeMarker實現(xiàn)頁面靜態(tài)化示例詳解
這篇文章主要介紹了SpringBoot2 整合FreeMarker實現(xiàn)頁面靜態(tài)化示例詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-07-07
spring+hibernate 兩種整合方式配置文件的方法
本篇文章主要介紹了spring+hibernate 兩種整合方式配置文件的方法,主要有兩種方式 1、注解方式 2、xml方式實現(xiàn),有興趣的可以了解一下。2017-04-04
Springboot?中的?Filter?實現(xiàn)超大響應?JSON?數(shù)據(jù)壓縮的方法
這篇文章主要介紹了Springboot?中的?Filter?實現(xiàn)超大響應?JSON?數(shù)據(jù)壓縮,定義GzipFilter對輸出進行攔截,定義 Controller該 Controller 非常簡單,主要讀取一個大文本文件,作為輸出的內(nèi)容,本文通過實例代碼給大家介紹的非常詳細,需要的朋友可以參考下2022-10-10

