在基於Spring Boot腳手架創建項目時,通常會直接將spring-boot-starter-parent
作爲<parent>
,如下示例:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.10</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
再添加其他相關stater
組件依賴時可以不用再明確指定版本號了,非常方便:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
但是,當項目中存在多個Maven模塊時,通常項目中的<parent>
節點是自定義的,並不能直接設置爲spring-boot-starter-parent
,但是又需要便於使用Spring Boot腳手架,此時該如何處理呢?
實際上,跟蹤spring-boot-starter-parent
的pom文件配置後發現,它將<parent>
設置爲了spring-boot-dependencies
。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.7.10</version>
</parent>
真正的組件依賴是在spring-boot-dependencies
中使用<dependencyManagement>
進行管理的。
參考Spring Boot自身的組件依賴管理辦法,針對在日常開發中既要便於使用Spring Boot腳手架,又不能直接將項目<parent>
設置爲spring-boot-starter-parent
的場景,有2種解決辦法:
方式一:將父項目的<parent>
設置爲spring-boot-starter-parent
,然後在子項目中添加starter
組件的依賴。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.10</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<packaging>pom</packaging>
<modules>
<module>xxx</module>
</modules>
方式二:在父項目中使用<dependencyManagement>
管理spring-boot-dependencies
依賴,這種方式就使用不到spring-boot-starter-parent
中的一些基礎配置了。
當父項目不設置<parent>
時,可以通過<dependencyManagement>
管理spring-boot-dependencies
依賴,然後在子項目中添加starter
組件的依賴。
<packaging>pom</packaging>
<modules>
<module>xxx</module>
</modules>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.7.10</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
上述2種方式的區別在於:使用第一種方式可以直接使用Spring Boot腳手架的全部基礎配置,更加便捷。
例如:使用第一種方式在子項目中使用spring-boot-maven-plugin
插件打包時只需要添加依賴即可,無需其他任何配置就可以將項目打包爲可執行jar包。
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
在使用第二種方式時使用spring-boot-maven-plugin
插件打包爲可執行jar包還需要額外的配置,並且必須明確指定插件版本號。
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!-- 明確指定插件版本號,不指定會報錯 -->
<version>2.7.10</version>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<configuration>
<!-- 明確指定啓動類 -->
<mainClass>org.test.springboot.implementation.App</mainClass>
</configuration>
</plugin>
</plugins>
</build>
當然,使用Spring Boot框架也可以不使用其任何基礎配置,直接在依賴管理中明確指定各種starter
組件的版本即可。
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.7.10</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.7.10</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.7.10</version>
<scope>test</scope>
</dependency>
</dependencies>
但是這顯然是一件喫累不討好的事情,不是萬不得已不要使用這種方式!
【參考】
你真的理解 Spring Boot 項目中的 parent 嗎?
不使用 parent 方式創建SpringBoot項目篇
spring boot 項目不使用 spring-boot-starter-parent 構建,使用自己項目的parent構造