The combination of Maven and Spring Boot is a standard practice in modern Java development. Spring Boot provides spring-boot-starter-parent as a parent POM, simplifying Maven configuration and providing default dependency management and plugin configuration.
Role of Spring Boot Starter Parent:
- Uniformly manage dependency versions to avoid version conflicts
- Provide default compiler configuration
- Configure resource filtering and encoding
- Provide default plugin configuration
- Simplify packaging and deployment processes
Using Spring Boot Starter Parent:
xml<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.0</version> <relativePath/> </parent>
Basic POM Configuration for Spring Boot Project:
xml<?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"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.0</version> <relativePath/> </parent> <groupId>com.example</groupId> <artifactId>spring-boot-app</artifactId> <version>1.0.0</version> <name>Spring Boot Application</name> <properties> <java.version>11</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </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-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
Spring Boot Maven Plugin: Spring Boot Maven Plugin is a key plugin for packaging Spring Boot applications:
xml<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>com.example.Application</mainClass> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
Common Spring Boot Starter Dependencies:
spring-boot-starter-web: Web application developmentspring-boot-starter-data-jpa: JPA data accessspring-boot-starter-data-mongodb: MongoDB data accessspring-boot-starter-security: Security authenticationspring-boot-starter-test: Test supportspring-boot-starter-actuator: Monitoring and management
Multi-environment Configuration: Use Profile to manage different environments:
xml<profiles> <profile> <id>dev</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <spring.profiles.active>dev</spring.profiles.active> </properties> </profile> <profile> <id>prod</id> <properties> <spring.profiles.active>prod</spring.profiles.active> </properties> </profile> </profiles>
Resource Filtering Configuration:
xml<build> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> </build>
Packaging and Running:
bash# Package mvn clean package # Run java -jar target/spring-boot-app-1.0.0.jar # Package skipping tests mvn clean package -DskipTests
Best Practices:
- Use spring-boot-starter-parent to simplify configuration
- Reasonably select Starter dependencies to avoid introducing unnecessary dependencies
- Use Profile to manage multi-environment configurations
- Configure resource filtering to dynamically replace configurations
- Use spring-boot-maven-plugin to package executable JAR
- Regularly update Spring Boot version to get security fixes
- Integrate build and deployment in CI/CD processes
Common Issue Resolution:
- Dependency Conflicts: Use
mvn dependency:treeto analyze dependency relationships - Packaging Failure: Check plugin configuration and dependency versions
- Configuration Not Taking Effect: Confirm resource filtering and Profile configuration
- Startup Failure: Check main class configuration and dependency integrity
The combination of Maven and Spring Boot can significantly simplify the development and deployment processes of Java projects.