您好,登錄后才能下訂單哦!
本篇文章主要介紹了SpringBoot+Maven 多模塊項目的構建、運行、打包,分享給大家,具體如下:
項目使用的工具:
項目的目錄:
一、使用IDEA創建一個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>
三、創建springboot-multi項目的子模塊,在項目上右鍵單擊,選擇:new -> Module。
四、創建四個子模塊后,刪除子模塊中 src/main/java、src/main/java下的所有文件(如果沒有文件跳過此操作),只保留web子模塊的SpringBoot的Application主啟動類。
五、主項目pom.xml (注意<modules>標簽是否指定了子模塊)
<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> <!--默認關掉單元測試 --> </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文件中需要注意的就是:
十一、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"; } }
十二、執行main方法啟動項目,訪問localhost:8080/test,出現如下頁面表示項目搭建成功:
十三、項目打包命令: mvn clean package 或者 使用右邊工具欄的圖形化界面打包也可以:
十四、打包成功日志:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。