乐闻世界logo
搜索文章和话题

How does Maven version management work? How to manage dependency versions?

2月18日 21:35

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 version
  • 1.2.3: Major version 1, minor version 2, patch version 3
  • 2.0.0: Incompatible major version upgrade

Version Range Specifiers:

  1. Exact Version: 1.0.0, use only the specified version
  2. 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
  3. Wildcard Versions:
    • 1.0.* or 1.0: Any version of 1.0.x
    • 1.* or 1: Any version of 1.x
  4. Latest Versions:
    • LATEST: Latest released version
    • RELEASE: Latest stable version
  5. 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:

  1. 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>
  1. 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>
  1. 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:

  1. Shortest Path First: Choose the version with the shortest dependency path
  2. Declaration Order First: Choose the version declared first in pom.xml
  3. Force Specified Version: Directly declare the required version to override transitive dependencies

Version Release Process:

  1. Use SNAPSHOT version during development
  2. After testing passes, remove SNAPSHOT identifier
  3. Use mvn release:prepare to prepare release
  4. Use mvn release:perform to execute release
  5. Publish to repository for use by other projects

Version Check Commands:

  • mvn versions:display-dependency-updates: View updatable dependencies
  • mvn versions:display-plugin-updates: View updatable plugins
  • mvn versions:display-property-updates: View updatable properties
  • mvn 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.

标签:Maven