springboot創(chuàng)建多module項(xiàng)目的實(shí)例
使用springboot創(chuàng)建多module項(xiàng)目,以前也做過(guò)多次,一段時(shí)間不用又忘了,在這里做個(gè)記錄
| 項(xiàng)目名稱(chēng) | 作用 | 說(shuō)明 |
|---|---|---|
| demo-root | 根項(xiàng)目 | 父項(xiàng)目,只用來(lái)管理其他module,提供基礎(chǔ)的pom配置 |
| demo-api | 啟動(dòng)項(xiàng)目 | api項(xiàng)目,啟動(dòng)類(lèi)放在此項(xiàng)目中 |
| demo-common | 通用工具 | 只放一些通用代碼、工具類(lèi)等 |
項(xiàng)目名稱(chēng)作用說(shuō)明demo-root根項(xiàng)目父項(xiàng)目,只用來(lái)管理其他module,提供基礎(chǔ)的pom配置demo-api啟動(dòng)項(xiàng)目api項(xiàng)目,啟動(dòng)類(lèi)放在此項(xiàng)目中demo-common通用工具只放一些通用代碼、工具類(lèi)等
創(chuàng)建項(xiàng)目
創(chuàng)建demo-parent
創(chuàng)建過(guò)程一路next即可



項(xiàng)目建好后,刪掉src目錄,因?yàn)椴恍枰诟?xiàng)目中寫(xiě)代碼

創(chuàng)建demo-api
創(chuàng)建過(guò)程跟root項(xiàng)目基本一致,所不同的是,需要右鍵點(diǎn)擊根項(xiàng)目,然后在彈出菜單中選擇new–> module

以后的過(guò)程跟root項(xiàng)目一樣,略…
創(chuàng)建demo-common
過(guò)程跟demo-api一樣,略…
修改各個(gè)項(xiàng)目的pom文件
創(chuàng)建過(guò)程很簡(jiǎn)單,這一步才是重點(diǎn)
修改demo-root的pom.xml
<?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>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo-root</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo-root</name>
<description>Demo project for Spring Boot</description>
<!-- 配置打包類(lèi)型,不寫(xiě)默認(rèn)是jar,一般來(lái)說(shuō)所有的父級(jí)項(xiàng)目的packaging都為pom -->
<packaging>pom</packaging>
<!-- 添加管理的包 -->
<modules>
<module>demo-api</module>
<module>demo-common</module>
</modules>
<properties>
<!-- 設(shè)置項(xiàng)目編碼 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- maven 插件版本 -->
<maven.compiler.plugin.version>3.1</maven.compiler.plugin.version>
<java.version>1.8</java.version>
</properties>
<!-- 根據(jù)自己的需要添加依賴(lài)包,注意:root中添加的依賴(lài)包會(huì)繼承到root所管理的所有module中 -->
<dependencies>
<!-- SpringBoot 核心包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- SpringBoot Web容器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- SpringBoot 攔截器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<!-- spring-boot-devtools -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional> <!-- 表示依賴(lài)不會(huì)傳遞 -->
</dependency>
<!-- spring security 安全認(rèn)證 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!-- SpringBoot 測(cè)試 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- redis 緩存操作 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
</dependencies>
<!-- <build>-->
<!-- <plugins>-->
<!-- <plugin>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-maven-plugin</artifactId>-->
<!-- </plugin>-->
<!-- </plugins>-->
<!-- </build>-->
<!-- 重點(diǎn): spring-boot-maven-plugin能夠?qū)pring Boot應(yīng)用打包為可執(zhí)行的jar或war文件,root不需要,所以改成maven打包-->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.plugin.version}</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
修改demo-api的pom.xml
注意我們沒(méi)有指定打包類(lèi)型,所以默認(rèn)打成jar包
<?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改成我們自己的root -->
<!-- <parent>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-parent</artifactId>-->
<!-- <version>2.3.1.RELEASE</version>-->
<!-- <relativePath/> <!– lookup parent from repository –>-->
<!-- </parent>-->
<parent>
<groupId>com.example</groupId>
<artifactId>demo-root</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<!-- 可以繼承root -->
<!-- <groupId>com.example</groupId>-->
<artifactId>demo-api</artifactId>
<!-- 可以繼承root -->
<!-- <version>0.0.1-SNAPSHOT</version>-->
<name>demo-api</name>
<description>Demo project for Spring Boot</description>
<!-- 可以繼承root -->
<!-- <properties>-->
<!-- <java.version>1.8</java.version>-->
<!-- </properties>-->
<dependencies>
<!-- 添加自己的依賴(lài)包 -->
<dependency>
<groupId>com.example</groupId>
<artifactId>demo-common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<!-- 重點(diǎn) -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!-- 當(dāng)所依賴(lài)的其他模塊,有啟動(dòng)類(lèi)的時(shí)候,需要以下配置,指定唯一啟動(dòng)類(lèi)-->
<configuration>
<!-- 指定該Main Class為全局的唯一入口 -->
<mainClass>com.example.demoapi.DemoApiApplication</mainClass>
<layout>ZIP</layout>
</configuration>
<executions>
<execution>
<goals>
<!--可以把依賴(lài)的包都打包到生成的Jar包中-->
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
修改demo-common的pom.xml
<?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>
<!-- 跟demo-api一樣,干掉原來(lái)的,使用我們自己的 -->
<!-- <parent>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-parent</artifactId>-->
<!-- <version>2.3.1.RELEASE</version>-->
<!-- <relativePath/> <!– lookup parent from repository –>-->
<!-- </parent>-->
<parent>
<groupId>com.example</groupId>
<artifactId>demo-root</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>demo-common</artifactId>
<name>demo-common</name>
<description>Demo project for Spring Boot</description>
<properties>
<!-- 添加自己的 -->
<mybatis-plus.version>3.3.1</mybatis-plus.version>
</properties>
<dependencies>
<!-- 添加自己的 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis-plus.version}</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-extension</artifactId>
<version>${mybatis-plus.version}</version>
</dependency>
</dependencies>
<!-- 重點(diǎn):刪掉原來(lái)的,原因還是不需要可運(yùn)行 -->
<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>
</plugins>
</build>
</project>
至此所有配置都已經(jīng)完成,可以打包試試
[INFO] Reactor Summary: [INFO] [INFO] demo-root .......................................... SUCCESS [ 0.001 s] [INFO] demo-common ........................................ SUCCESS [ 1.599 s] [INFO] demo-api ........................................... SUCCESS [ 0.711 s] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.705 s [INFO] Finished at: 2020-06-13T09:58:53+08:00 [INFO] Final Memory: 47M/334M [INFO] ------------------------------------------------------------------------
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java Springboot 重要知識(shí)點(diǎn)整理匯總
Spring Boot作為微服務(wù)中最好的Java框架,本文主要為大家整理匯總了七個(gè)Spring Boot的重要知識(shí)點(diǎn),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2021-11-11
Java中JDK動(dòng)態(tài)代理的超詳細(xì)講解
JDK 的動(dòng)態(tài)代理是基于攔截器和反射來(lái)實(shí)現(xiàn)的,JDK代理是不需要第三方庫(kù)支持的,只需要JDK環(huán)境就可以進(jìn)行代理,下面這篇文章主要給大家介紹了關(guān)于Java中JDK動(dòng)態(tài)代理的超詳細(xì)講解,需要的朋友可以參考下2022-10-10
Java實(shí)現(xiàn)發(fā)送郵件并攜帶附件
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)發(fā)送郵件并攜帶附件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
fastJson泛型如何轉(zhuǎn)換的實(shí)現(xiàn)
這篇文章主要介紹了fastJson泛型如何轉(zhuǎn)換的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
java求最大公約數(shù)與最小公倍數(shù)的方法示例
這篇文章主要介紹了java求最大公約數(shù)與最小公倍數(shù)的方法,涉及java數(shù)值運(yùn)算的相關(guān)操作技巧,并附帶分析了eclipse環(huán)境下設(shè)置運(yùn)行輸入?yún)?shù)的相關(guān)操作技巧,需要的朋友可以參考下2017-11-11

