Explain Codes LogoExplain Codes Logo

How do I tell Maven to use the latest version of a dependency?

java
maven-plugin
dependency-management
build-stability
Nikita BarsukovbyNikita Barsukov·Sep 8, 2024
TLDR

To secure the newest version of a Maven dependency, define a version range in your pom.xml, like [1.0,) to enclose all versions beyond 1.0. Steer clear of using LATEST or RELEASE as they may destabilize builds.

<dependency> <groupId>com.example</groupId> <artifactId>example-artifact</artifactId> <version>[1.0,)</version> <!-- Grabbing the newest version with arms wide open --> </dependency>

Leveraging Maven's versions plugin

Harness the prowess of the Maven versions plugin to manage version reliance. This useful tool harbors commands like versions:use-latest-versions which kindly update your version numbers to the latest available of their own accord. Employ the soothsaying abilities of versions:display-dependency-updates to gaze at potential future updates.

Build stability and reproducibility: a balancing act

While the allure of using latest versions is powerful, this might tip the scales of your build stability. Maven encourages you to name specific versions, ensuring reproducibility. This guarantees that no matter where you set up shop, your build will steadfastly remain the same, a promise that isn't kept with version ranges.

Continuous Integration: a journey of automation

Embark on your Continuous Integration (CI) journey and automate the process of updating versions. CI platforms can collaborate with Maven versions plugin and SCM tools to frequently check for updates and commit these changes, making your project stay in vogue without you breaking a sweat.

Unveiling Version Ranges

Version ranges provide a toolbox for handling a set of versions. However, using an unbounded range (e.g., [1.0,)) means Maven will leap at the newest version available, which might rope in unexpected changes or incompatibilities.

Befriending "latest" versions

Balancing between predictability and freshness when considering the most recent updates is pivotal. Maven 3 sidesteps LATEST and RELEASE to nix any ambiguity in your build. Using precise versions or calculated ranges ensures delivery of consistent outcomes.

Keeping plugins current and the family together

Meticulously check for plugin updates using versions:display-plugin-updates. For the multi-module families, versions:update-parent and versions:update-child-modules can enforce consistency amongst siblings of your project.

Customizing updates, one code at a time

You can also instruct the obedient versions plugin to update specific dependencies or to exclude certain others from updates through the <includes> and <excludes> configurations.

Matching versions within a range

With the versions:resolve-ranges goal, Maven can sort out versions within a range where you've defined version ranges, achieving harmony between exploring latest features and maintaining stability.

Maven Updates: A User's Manual

While automation and flexibility are tempting, responsibility reigns supreme:

  • Review Upgrades: Be suspicious. Trust, but verify each upgrade.
  • Automate Testing: Set up test-run bots to catch issues introduced by updates early.
  • Commit Thoughtfully: Involving CI to commit updates? Ensure a final human review before deployment.