Maven version management follows the Semantic Versioning specification, using a three-part version number format: MAJOR.MINOR.PATCH. Understanding Maven version management is crucial for dependency management and project release.
Version Number Format:
- MAJOR: Incompatible API changes
- MINOR: Backwards-compatible functionality additions
- PATCH: Backwards-compatible bug fixes
Version Number Examples:
1.0.0: First stable version1.2.3: Major version 1, minor version 2, patch version 32.0.0: Incompatible major version upgrade
Version Range Specifiers:
- Exact Version:
1.0.0, use only the specified version - Range Versions:
[1.0,2.0): Greater than or equal to 1.0 and less than 2.0(1.0,2.0]: Greater than 1.0 and less than or equal to 2.0[1.0,]: Greater than or equal to 1.0[,1.0]: Less than or equal to 1.0
- Wildcard Versions:
1.0.*or1.0: Any version of 1.0.x1.*or1: Any version of 1.x
- Latest Versions:
LATEST: Latest released versionRELEASE: Latest stable version
- Snapshot Versions:
1.0-SNAPSHOT, version in development
SNAPSHOT Versions:
- SNAPSHOT is a Maven-specific version identifier, indicating a version in development
- Each time Maven builds, it checks the remote repository for updated SNAPSHOT versions
- SNAPSHOT versions should not be used in production environments
- When releasing, SNAPSHOT should be replaced with a formal version number
Version Management Best Practices:
- Use dependencyManagement to uniformly manage versions:
xml<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.3.20</version> </dependency> </dependencies> </dependencyManagement>
- Use BOM (Bill of Materials) to manage versions:
xml<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.7.0</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
- Use properties to define version numbers:
xml<properties> <spring.version>5.3.20</spring.version> <mybatis.version>3.5.9</mybatis.version> </properties>
Version Conflict Resolution:
- Shortest Path First: Choose the version with the shortest dependency path
- Declaration Order First: Choose the version declared first in pom.xml
- Force Specified Version: Directly declare the required version to override transitive dependencies
Version Release Process:
- Use SNAPSHOT version during development
- After testing passes, remove SNAPSHOT identifier
- Use
mvn release:prepareto prepare release - Use
mvn release:performto execute release - Publish to repository for use by other projects
Version Check Commands:
mvn versions:display-dependency-updates: View updatable dependenciesmvn versions:display-plugin-updates: View updatable pluginsmvn versions:display-property-updates: View updatable propertiesmvn versions:use-latest-releases: Update to latest stable versions
Notes:
- Avoid using LATEST and RELEASE, which may cause build instability
- Do not use SNAPSHOT versions in production environments
- Regularly update dependency versions to fix security vulnerabilities
- Be cautious when using version ranges to avoid introducing incompatible updates
- Lock dependency versions in CI/CD processes to ensure reproducible builds
Reasonable version management can improve project stability and maintainability.