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項目。
創建頂級maven項目1
這裏注意Projiect location,指向當前項目存放路徑
創建頂級maven項目2
創建完成以後,刪除 src目錄
創建頂級maven項目3
修改pom.xml文件,添加如下配置

  1. 將當前項目定義爲pom項目
    <packaging>pom</packaging>
    <name>swotxu</name>
  1. 註冊子項目,後面創建的二級子項目在父項目中註冊
    <modules>
        <module>swotxu-common</module>
       <!-- <module>swotxu-webbase</module>-->
    </modules>
  1. 依賴管理 – 有兩種依賴管理方式
    使用<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子項目1.1
由於繼承父maven項目,此時GroupIdVersion自動引用父項目的
創建二級Maven子項目1.2
注意子項目路徑
創建二級Maven子項目1.3
由於博主這裏演示三級父子項目繼承,創建完成以後,同樣刪除 src 目錄,項目結構如下:
創建二級Maven子項目1.4
修改一級項目swotxu/pom.xml文件,添加如下配置,將當前二級子項目註冊到父項目

    <modules>
        <module>swotxu-common</module>
    </modules>

修改二級項目common/pom.xml文件,添加如下配置

  1. 將當前項目定義爲pom項目
    <packaging>pom</packaging>
    <name>common</name>
  1. 註冊子項目,後面創建的三級子項目在父項目中註冊
    <modules>
        <module>swotxu-concurrency</module>
    </modules>
  1. 依賴管理 – 此次使用第二種依賴管理方式
<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項目。
創建三級springboot子項目1
創建三級springboot子項目2
這裏注意Content rootModule file location,包含三個層級關係
創建三級springboot子項目3
創建完成以後,項目結構如下:
創建三級springboot子項目4
修改二級項目common/pom.xml文件,添加如下配置,將當前三級子項目註冊到父項目

    <modules>
        <module>swotxu-concurrency</module>
    </modules>

修改三級項目concurrency/pom.xml文件,添加如下配置

  1. 指定當前項目的父項目爲common父項目
<parent>
	<groupId>com.swotxu</groupId>
	<artifactId>common</artifactId>
	<version>1.0-SNAPSHOT</version>
	<relativePath>../pom.xml</relativePath>
</parent>
  1. 將當前項目定義爲jar項目
    <packaging>jar</packaging>
    <name>conurrency</name>
  1. 依賴管理 – 直接繼承父項目依賴,子項目無需手動引用依賴
<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父子項目就創建完成了。
大家如果有什麼疑問,歡迎評論留言!別忘了收藏關注~

發佈了6 篇原創文章 · 獲贊 5 · 訪問量 774
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章