Links#
https://docs.spring.io/spring-boot/maven-plugin/
https://docs.spring.io/spring-boot/appendix/dependency-versions/index.html
https://docs.spring.io/spring-boot/reference/using/build-systems.html
1. Important Points#
starter:
Spring Boot 提供的一组 dependency shortcut,例如 web、data-jpa、test
spring-boot-starter-parent:
parent POM, provides dependency versions and plugin defaults
spring-boot-dependencies:
BOM, only provides dependency versions
2. Use Starter Parent#
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.1</version>
<relativePath/>
</parent>
<groupId>com.example</groupId>
<artifactId>order-api</artifactId>
<version>1.0.0-SNAPSHOT</version>
<properties>
<java.version>21</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
use when:
application can inherit Spring Boot parent directly
3. Use BOM Without Parent#
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>3.3.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
use when:
company parent POM must be inherited
Spring Boot only provides dependency versions
4. Starters#
| Starter |
Use |
spring-boot-starter-web |
Spring MVC / embedded Tomcat |
spring-boot-starter-webflux |
reactive HTTP |
spring-boot-starter-data-jpa |
JPA / Hibernate |
spring-boot-starter-validation |
Bean Validation |
spring-boot-starter-actuator |
health, metrics, management endpoints |
spring-boot-starter-test |
test stack |
5. Spring Boot Maven Plugin#
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
./mvnw spring-boot:run
./mvnw clean package
java -jar target/order-api-1.0.0-SNAPSHOT.jar
repackage:
turns normal jar into executable Spring Boot jar
6. Profiles#
./mvnw -Pprod clean package
java -jar app.jar --spring.profiles.active=prod
Maven profile:
changes build behavior
Spring profile:
changes application runtime configuration
7. Override Boot Managed Version#
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.17.2</version>
</dependency>
</dependencies>
</dependencyManagement>
use only when:
security fix or compatibility requirement is clear
tests verify Spring Boot compatibility
8. Multi Module Pattern#
root pom:
packaging=pom
imports spring-boot-dependencies
owns pluginManagement
application module:
depends on internal library modules
enables spring-boot-maven-plugin
library module:
normal jar
usually should not enable repackage