1. 程式人生 > >理解 maven 多模組專案依賴關係

理解 maven 多模組專案依賴關係

語言功底差,直接上程式碼。然後再解釋

1。父pom

<?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">


    <!-- 基本資訊 -->
    <description>SpringBoot 多模組構建示例</description>
    <modelVersion>4.0.0</modelVersion>
    <name>Multi_module</name>
    <packaging>pom</packaging>
    <!-- 專案資訊 -->
    <groupId>com.xie</groupId>
    <artifactId>Multi_module</artifactId>
    <version>1.0-SNAPSHOT</version>
    <!-- 繼承springboot提供的父工程 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.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>
    </properties>
    <!-- 宣告多個子模組-->
    <modules>
        <module>springboot-web</module>
        <module>springboot-dao</module>
        <module>springboot-jpa</module>
        <module>springboot-service1</module>
        <module>springboot-service2</module>
        <module>springboot-test</module>
    </modules>
    <dependencies>
        <!--data-jpa-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
    </dependencies>
    <!--版本說明,統一管理版本號-->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
            </dependency>
            <!--本地依賴-->
            <dependency>
                <groupId>com.xie</groupId>
                <artifactId>springboot-web</artifactId>
                <version>0.0.1-SNAPSHOT</version>
            </dependency>
            <dependency>
                <groupId>com.xie</groupId>
                <artifactId>springboot-jpa</artifactId>
                <version>0.0.1-SNAPSHOT</version>
            </dependency>
            <dependency>
                <groupId>com.xie</groupId>
                <artifactId>springboot-dao</artifactId>
                <version>0.0.1-SNAPSHOT</version>
            </dependency>
            <dependency>
                <groupId>com.xie</groupId>
                <artifactId>springboot-service1</artifactId>
                <version>0.0.1-SNAPSHOT</version>
            </dependency>
            <dependency>
                <groupId>com.xie</groupId>
                <artifactId>springboot-service2</artifactId>
                <version>0.0.1-SNAPSHOT</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

在父pom的 <dependencies> 裡引入的依賴子專案會繼承,也就說,只要在父pom的<dependencies>裡引入的東西,子專案就可以直接用

</dependencyManagement> 負責管理子專案的依賴版本。也就是說,在這裡引入的依賴,子專案不會繼承,子專案要用到的依賴還是要子專案自己去進行<dependencies>引入,但是,子專案在引用時是沒有版本號的,版本號在父pom的</dependencyManagement> 裡定義好了。

我們可以看一下springboot提供的父工程示例。

我們在引入下面這個東西的時候已經為我們定義好了上面的引用依賴版本。

所以我們在用到springboot提供的依賴時實際引用是這樣的
!沒有版本號


結語:

     我繼承了誰,我就可以直接用誰的東西;我依賴了誰,我也可以直接用誰的東西。