前言
這里主要說兩種情況:
- 子module依賴父module
- 子module依賴子module
子module依賴父module
子module依賴父module又分為兩種情況: dependencies 和 dependencyManagement
兩種情況下子module都要在<parent>
標簽中依賴父module
dependencies
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
子項目會從父項目中繼承dependencies中的所有依賴,即使在子項目中沒有引入。
dependencyManagement
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.3.4.RELEASE</version>
</dependency>
</dependencies>
</dependencyManagement>
注意需要在父項目中指定版本,並且需要在子項目中顯式的引用。
子項目pom:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
如果子項目中沒有指定版本,則按照父項目中指定的版本引入;如果子項目中指定了版本,則按照子項目中指定的版本引入。
子module依賴子module
假設B模塊依賴A模塊,則只需要在B的pom中引入A即可,不過需要注意一點,如果在A模塊中引入了spring-boot-maven-plugin插件,則需要一點特殊的配置:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<classifier>exec</classifier>
</configuration>
</plugin>
</plugins>
</build>
需要<configuration>
中的配置的原因是:
默認情況下,maven會先打一個普通的jar包,然后如果引入了該插件,則該插件會將普通的jar包重命名為a.jar.original, 然后重新打一個可執行的jar包,命名為a.jar,但是可執行的jar包被依賴的話是會報錯的。
為什么依賴可執行的jar包會報錯?
我大致解壓了一下兩個jar包,其實里面的class的路徑是不一樣的。