springboot創(chuàng)建多module項目的實例
使用springboot創(chuàng)建多module項目,以前也做過多次,一段時間不用又忘了,在這里做個記錄
項目名稱 | 作用 | 說明 |
---|---|---|
demo-root | 根項目 | 父項目,只用來管理其他module,提供基礎的pom配置 |
demo-api | 啟動項目 | api項目,啟動類放在此項目中 |
demo-common | 通用工具 | 只放一些通用代碼、工具類等 |
項目名稱作用說明demo-root根項目父項目,只用來管理其他module,提供基礎的pom配置demo-api啟動項目api項目,啟動類放在此項目中demo-common通用工具只放一些通用代碼、工具類等
創(chuàng)建項目
創(chuàng)建demo-parent
創(chuàng)建過程一路next即可
項目建好后,刪掉src目錄,因為不需要在根項目中寫代碼
創(chuàng)建demo-api
創(chuàng)建過程跟root項目基本一致,所不同的是,需要右鍵點擊根項目,然后在彈出菜單中選擇new–> module
以后的過程跟root項目一樣,略…
創(chuàng)建demo-common
過程跟demo-api一樣,略…
修改各個項目的pom文件
創(chuàng)建過程很簡單,這一步才是重點
修改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> <!-- 配置打包類型,不寫默認是jar,一般來說所有的父級項目的packaging都為pom --> <packaging>pom</packaging> <!-- 添加管理的包 --> <modules> <module>demo-api</module> <module>demo-common</module> </modules> <properties> <!-- 設置項目編碼 --> <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> <!-- 根據自己的需要添加依賴包,注意:root中添加的依賴包會繼承到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> <!-- 表示依賴不會傳遞 --> </dependency> <!-- spring security 安全認證 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <!-- SpringBoot 測試 --> <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>--> <!-- 重點: spring-boot-maven-plugin能夠將Spring Boot應用打包為可執(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
注意我們沒有指定打包類型,所以默認打成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> <!-- 添加自己的依賴包 --> <dependency> <groupId>com.example</groupId> <artifactId>demo-common</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> <!-- 重點 --> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <!-- 當所依賴的其他模塊,有啟動類的時候,需要以下配置,指定唯一啟動類--> <configuration> <!-- 指定該Main Class為全局的唯一入口 --> <mainClass>com.example.demoapi.DemoApiApplication</mainClass> <layout>ZIP</layout> </configuration> <executions> <execution> <goals> <!--可以把依賴的包都打包到生成的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一樣,干掉原來的,使用我們自己的 --> <!-- <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> <!-- 重點:刪掉原來的,原因還是不需要可運行 --> <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>
至此所有配置都已經完成,可以打包試試
[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] ------------------------------------------------------------------------
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。