SpringBoot+Maven 多模塊項目的構(gòu)建、運行、打包實戰(zhàn)
本篇文章主要介紹了SpringBoot+Maven 多模塊項目的構(gòu)建、運行、打包,分享給大家,具體如下:
項目使用的工具:
- IntelliJ IDEA
- JDK 1.8
- apache-maven-3.3.9
項目的目錄:
- 主項目 springboot-multi
- 子模塊 entity、dao、service、web
一、使用IDEA創(chuàng)建一個SpringBoot項目 : File -> new -> Project 項目名稱為springboot-multi
二、刪除項目中的src目錄,把pom.xml中的項目打包方式改為pom,如下:
<groupId>com.example</groupId> <artifactId>springboot-multi</artifactId> <version>0.0.1-SNAPSHOT</version> <!-- 此處改為pom --> <packaging>pom</packaging>
三、創(chuàng)建springboot-multi項目的子模塊,在項目上右鍵單擊,選擇:new -> Module。
四、創(chuàng)建四個子模塊后,刪除子模塊中 src/main/java、src/main/java下的所有文件(如果沒有文件跳過此操作),只保留web子模塊的SpringBoot的Application主啟動類。
五、主項目pom.xml (注意<modules>標(biāo)簽是否指定了子模塊)
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>springboot-multi</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- 此處改為pom -->
<packaging>pom</packaging>
<name>springboot-multi</name>
<description>Demo project for Spring Boot</description>
<modules>
<module>web</module>
<module>service</module>
<module>dao</module>
<module>entity</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.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>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<!--指定使用maven打包-->
<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> <!--默認關(guān)掉單元測試 -->
</configuration>
</plugin>
</plugins>
</build>
六、web子模塊pom.xml(依賴service、dao、entity子模塊)
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</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.example</groupId>
<artifactId>springboot-multi</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>service</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>dao</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>entity</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<!--spring boot打包的話需要指定一個唯一的入門-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!-- 指定該Main Class為全局的唯一入口 -->
<mainClass>com.example.WebApplication</mainClass>
<layout>ZIP</layout>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal><!--可以把依賴的包都打包到生成的Jar包中-->
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
七、service子模塊pom.xml(依賴 dao 、entity子模塊)
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</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.example</groupId>
<artifactId>springboot-multi</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>dao</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>entity</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
八、dao子模塊pom.xml (依賴entity子模塊)
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</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.example</groupId>
<artifactId>springboot-multi</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>entity</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
九、entity子模塊
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</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.example</groupId>
<artifactId>springboot-multi</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
十、pom.xml文件中需要注意的就是:
- 主項目的modules標(biāo)簽指定的子模塊是否正確
- 子模塊之間的依賴
- 子模塊的parent標(biāo)簽
十一、web子模塊的Application啟動類:
@RestController
@SpringBootApplication
public class WebApplication {
public static void main(String[] args) {
SpringApplication.run(WebApplication.class, args);
}
@RequestMapping(value = "/test",method = RequestMethod.GET)
public String test(){
return "test success";
}
}
十二、執(zhí)行main方法啟動項目,訪問localhost:8080/test,出現(xiàn)如下頁面表示項目搭建成功:

十三、項目打包命令: mvn clean package 或者 使用右邊工具欄的圖形化界面打包也可以:

十四、打包成功日志:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
哲學(xué)家就餐問題中的JAVA多線程學(xué)習(xí)
哲學(xué)家就餐問題是1965年由Dijkstra提出的一種線程同步的問題,下面我們就看一下JAVA多線程如何做2013-11-11
Mybatis-Plus中IdType.AUTO局部配置不生效的問題解決
本文主要介紹了Mybatis-Plus中IdType.AUTO局部配置不生效的問題解決,數(shù)據(jù)庫插入數(shù)據(jù)時,id的默認生成方式還是雪花算法,局部配置沒有生效,下面就來解決一下,感興趣的可以了解一下2023-09-09
Spring?Data?JPA實現(xiàn)查詢結(jié)果返回map或自定義的實體類
這篇文章主要介紹了Spring?Data?JPA實現(xiàn)查詢結(jié)果返回map或自定義的實體類,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12
詳解mybatis多對一關(guān)聯(lián)查詢的方式
這篇文章主要給大家介紹了關(guān)于mybatis多對一關(guān)聯(lián)查詢的相關(guān)資料,文中將關(guān)聯(lián)方式以及配置方式介紹的很詳細,需要的朋友可以參考下2021-06-06
SpringBoot項目中出現(xiàn)不同端口跨域問題的解決方法
這篇文章主要介紹了SpringBoot項目中出現(xiàn)不同端口跨域問題的解決方法,文中介紹了兩種解決方法,并給出了詳細的代碼供大家參考,具有一定的參考價值,需要的朋友可以參考下2024-03-03

