カスタム親を使用したSpringBoot依存関係管理
1. 概要
Spring Boot は、SpringBootアプリケーションを簡単に作成するための親POMを提供します。
ただし、継承元の親がすでにある場合は、親POMを使用することが必ずしも望ましいとは限りません。
このクイックチュートリアルでは、親スターターなしでBootを使用する方法を見ていきます。
2. Spring Boot親POMなし
ただし、実際には、デザインルールやその他の設定によって、別の親を使用するように制約される場合があります。
幸い、 Spring Boot は、親スターターから継承する代わりの方法を提供しますが、それでもいくつかの利点があります。
親POMを使用しない場合でも、 spring-boot-dependenciesアーティファクトと
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.4.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
次に、Springの依存関係を追加し、 SpringBootの機能を利用し始めることができます。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
3. 依存関係バージョンのオーバーライド
Bootによって管理されるものとは異なるバージョンを特定の依存関係に使用する場合は、 spring-boot-dependencies を宣言する前に、dependentencyManagementセクションで宣言する必要があります。
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>2.4.0</version>
</dependency>
</dependencies>
// ...
</dependencyManagement>
対照的に、dependentencyManagementタグの外側で依存関係のバージョンを宣言するだけでは機能しなくなります。
4. 結論
このクイックチュートリアルでは、親pom.xml。なしでSpring Bootを使用する方法を説明しました。
例のソースコードは、GitHubのにあります。