IDEA 創建SpringBoot多級MAVEN父子項目
一、環境準備
開發環境準備(博主這裏使用JDK8、MAVEN3.5.3、IDEA2017x64)。
1、JDK安裝配置
參考我的另外一篇博客:
鏈接: JDK安裝配置-swotXu.
2、MAVEN安裝配置
參考我的另外一篇博客:
鏈接: MAVEN安裝配置-swotXu.
二、項目搭建
本次演示,博主創建三層MAVEN父子項目。
一級:mavne項目 swotxu
二級:maven項目 common/webbase
三級:springboot項目 concurrency
/webdemo
若創建兩級父子項目,則父項目爲maven項目,子項目爲springboot項目
1、創建頂級Maven項目
FIle -> new -> project
,選Maven,創建maven項目。
這裏注意Projiect location
,指向當前項目存放路徑
創建完成以後,刪除 src
目錄
修改pom.xml
文件,添加如下配置
- 將當前項目定義爲
pom
項目
<packaging>pom</packaging>
<name>swotxu</name>
- 註冊子項目,後面創建的二級子項目在父項目中註冊
<modules>
<module>swotxu-common</module>
<!-- <module>swotxu-webbase</module>-->
</modules>
- 依賴管理 – 有兩種依賴管理方式
使用<dependencyManagement>
進行依賴管理
父項目相當於一個依賴發佈工廠,父項目統一管理依賴版本。
子項目需要的依賴,需在子項目中手動指定引入,無需指定依賴版本,無法繼承父項目依賴直接使用
一級父項目使用此方式
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${springboot.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
使用<dependencies>
進行依賴管理
子項目無需手動指定依賴引入,會自動繼承父依賴直接使用。
二級子項目使用此方式
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${springboot.version}</version>
</dependency>
</dependencies>
附: 一級項目完整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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.swotxu</groupId>
<artifactId>swotxu</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- 將當前項目定義爲pom -->
<packaging>pom</packaging>
<name>swotxu</name>
<!--指定當前項目繼承spring-boot-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.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>
<springboot.version>2.2.1.RELEASE</springboot.version>
<mybatis.version>2.1.1</mybatis.version>
<lombok.version>1.18.10</lombok.version>
<junit.version>4.12</junit.version>
</properties>
<!--註冊子項目 後面創建二級子項目後,需在父項目中註冊-->
<modules>
<!--<module>swotxu-common</module>-->
<!--<module>swotxu-webbase</module>-->
</modules>
<!--
父項目引用依賴
注:父項目依賴引用有兩種方式
1、使用此方式進行依賴管理時,父項目相當於一個依賴發佈工廠。
子項目需要哪些依賴,需要手動指定引入,無法繼承父項目依賴直接使用
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${springboot.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
2、使用此方式經行依賴管理時,子項目無需手動指定依賴引入,
會自動繼承父依賴直接使用。
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${springboot.version}</version>
</dependency>
</dependencies>
-->
<dependencyManagement>
<dependencies>
<!-- web組件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${springboot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${springboot.version}</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<!-- 資源文件拷貝插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- java編譯插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2、創建二級Maven子項目
FIle -> new -> Module
,選Maven,創建maven項目。
由於繼承父maven項目,此時GroupId
和Version
自動引用父項目的
注意子項目路徑
由於博主這裏演示三級父子項目繼承,創建完成以後,同樣刪除 src
目錄,項目結構如下:
修改一級項目swotxu/pom.xml
文件,添加如下配置,將當前二級子項目註冊到父項目
<modules>
<module>swotxu-common</module>
</modules>
修改二級項目common/pom.xml
文件,添加如下配置
- 將當前項目定義爲
pom
項目
<packaging>pom</packaging>
<name>common</name>
- 註冊子項目,後面創建的三級子項目在父項目中註冊
<modules>
<module>swotxu-concurrency</module>
</modules>
- 依賴管理 – 此次使用第二種依賴管理方式
<dependencies>
<!-- web組件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
附: 二級子項目完整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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>swotxu</artifactId>
<groupId>com.swotxu</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>common</artifactId>
<packaging>pom</packaging>
<name>common</name>
<description>Common template</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<modules>
<!--<module>swotxu-concurrency</module>-->
<!--<module>swotxu-algorithm</module>-->
</modules>
<dependencies>
<!-- web組件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<!--
將這段放開,並指定入口,則打jar包,
如果註釋這一段,並在子項目web中將
<packaging>jar</packaging>改成war,則打成war包
-->
<build>
<plugins>
<plugin>
<!-- The plugin rewrites your manifest -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<!--可以把依賴的包都打包到生成的Jar包中-->
<goal>repackage</goal>
</goals>
<!--可以生成不含依賴包的不可執行Jar包-->
<!-- configuration>
<classifier>exec</classifier>
</configuration> -->
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
3、創建三級Springboot子項目
FIle -> new -> Module
,選String Initializr,創建springboot項目。
這裏注意Content root
和Module file location
,包含三個層級關係
創建完成以後,項目結構如下:
修改二級項目common/pom.xml
文件,添加如下配置,將當前三級子項目註冊到父項目
<modules>
<module>swotxu-concurrency</module>
</modules>
修改三級項目concurrency/pom.xml
文件,添加如下配置
- 指定當前項目的父項目爲
common
父項目
<parent>
<groupId>com.swotxu</groupId>
<artifactId>common</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
- 將當前項目定義爲
jar
項目
<packaging>jar</packaging>
<name>conurrency</name>
- 依賴管理 – 直接繼承父項目依賴,子項目無需手動引用依賴
<dependencies>
</dependencies>
附: 三級子項目完整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>com.swotxu</groupId>
<artifactId>common</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<groupId>com.swotxu</groupId>
<artifactId>concurrency</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>conurrency</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
</dependencies>
<!--讓多模塊化拆分之後還能打成完整的可執行jar包-->
<build>
<finalName>concurrency-demo</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.swotxu.concurrency.ConcurrencyApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
以上,三級父子項目基本搭建完成!
這裏貼另一個項目的結構圖供參考:
至此,多級Maven父子項目就創建完成了。
大家如果有什麼疑問,歡迎評論留言!別忘了收藏關注~