理解 maven 多模組專案依賴關係
阿新 • • 發佈:2018-12-31
語言功底差,直接上程式碼。然後再解釋
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提供的父工程示例。