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

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


NOTE:

The mentioned LATEST and RELEASE metaversions have been dropped for plugin dependencies in Maven 3 "for the sake of reproducible builds", over 6 years ago.(They still work perfectly fine for regular dependencies.)For plugin dependencies please refer to this Maven 3 compliant solution.


If you always want to use the newest version, Maven has two keywords you can use as an alternative to version ranges. You should use these options with care as you are no longer in control of the plugins/dependencies you are using.

When you depend on a plugin or a dependency, you can use the a version value of LATEST or RELEASE. LATEST refers to the latest released or snapshot version of a particular artifact, the most recently deployed artifact in a particular repository. RELEASE refers to the last non-snapshot release in the repository. In general, it is not a best practice to design software which depends on a non-specific version of an artifact. If you are developing software, you might want to use RELEASE or LATEST as a convenience so that you don't have to update version numbers when a new release of a third-party library is released. When you release software, you should always make sure that your project depends on specific versions to reduce the chances of your build or your project being affected by a software release not under your control. Use LATEST and RELEASE with caution, if at all.

See the POM Syntax section of the Maven book for more details. Or see this doc on Dependency Version Ranges, where:

  • A square bracket ( [ & ] ) means "closed" (inclusive).
  • A parenthesis ( ( & ) ) means "open" (exclusive).

Here's an example illustrating the various options. In the Maven repository, com.foo:my-foo has the following metadata:

<?xml version="1.0" encoding="UTF-8"?><metadata>  <groupId>com.foo</groupId>  <artifactId>my-foo</artifactId>  <version>2.0.0</version>  <versioning>    <release>1.1.1</release>    <versions>      <version>1.0</version>      <version>1.0.1</version>      <version>1.1</version>      <version>1.1.1</version>      <version>2.0.0</version>    </versions>    <lastUpdated>20090722140000</lastUpdated>  </versioning></metadata>

If a dependency on that artifact is required, you have the following options (other version ranges can be specified of course, just showing the relevant ones here):

Declare an exact version (will always resolve to 1.0.1):

<version>[1.0.1]</version>

Declare an explicit version (will always resolve to 1.0.1 unless a collision occurs, when Maven will select a matching version):

<version>1.0.1</version>

Declare a version range for all 1.x (will currently resolve to 1.1.1):

<version>[1.0.0,2.0.0)</version>

Declare an open-ended version range (will resolve to 2.0.0):

<version>[1.0.0,)</version>

Declare the version as LATEST (will resolve to 2.0.0) (removed from maven 3.x)

<version>LATEST</version>

Declare the version as RELEASE (will resolve to 1.1.1) (removed from maven 3.x):

<version>RELEASE</version>

Note that by default your own deployments will update the "latest" entry in the Maven metadata, but to update the "release" entry, you need to activate the "release-profile" from the Maven super POM. You can do this with either "-Prelease-profile" or "-DperformRelease=true"


It's worth emphasising that any approach that allows Maven to pick the dependency versions (LATEST, RELEASE, and version ranges) can leave you open to build time issues, as later versions can have different behaviour (for example the dependency plugin has previously switched a default value from true to false, with confusing results).

It is therefore generally a good idea to define exact versions in releases. As Tim's answer points out, the maven-versions-plugin is a handy tool for updating dependency versions, particularly the versions:use-latest-versions and versions:use-latest-releases goals.


Now I know this topic is old, but reading the question and the OP supplied answer it seems the Maven Versions Plugin might have actually been a better answer to his question:

In particular the following goals could be of use:

  • versions:use-latest-versions searches the pom for all versionswhich have been a newer version andreplaces them with the latestversion.
  • versions:use-latest-releases searches the pom for all non-SNAPSHOTversions which have been a newerrelease and replaces them with thelatest release version.
  • versions:update-properties updates properties defined in aproject so that they correspond tothe latest available version ofspecific dependencies. This can beuseful if a suite of dependenciesmust all be locked to one version.

The following other goals are also provided:

  • versions:display-dependency-updates scans a project's dependencies andproduces a report of thosedependencies which have newerversions available.
  • versions:display-plugin-updates scans a project's plugins andproduces a report of those pluginswhich have newer versions available.
  • versions:update-parent updates the parent section of a project sothat it references the newestavailable version. For example, ifyou use a corporate root POM, thisgoal can be helpful if you need toensure you are using the latestversion of the corporate root POM.
  • versions:update-child-modules updates the parent section of thechild modules of a project so theversion matches the version of thecurrent project. For example, if youhave an aggregator pom that is alsothe parent for the projects that itaggregates and the children andparent versions get out of sync, thismojo can help fix the versions of thechild modules. (Note you may need toinvoke Maven with the -N option inorder to run this goal if yourproject is broken so badly that itcannot build because of the versionmis-match).
  • versions:lock-snapshots searches the pom for all -SNAPSHOTversions and replaces them with thecurrent timestamp version of that-SNAPSHOT, e.g. -20090327.172306-4
  • versions:unlock-snapshots searches the pom for all timestamplocked snapshot versions and replacesthem with -SNAPSHOT.
  • versions:resolve-ranges finds dependencies using version ranges andresolves the range to the specificversion being used.
  • versions:use-releases searches the pom for all -SNAPSHOT versionswhich have been released and replacesthem with the corresponding releaseversion.
  • versions:use-next-releases searches the pom for all non-SNAPSHOTversions which have been a newerrelease and replaces them with thenext release version.
  • versions:use-next-versions searches the pom for all versionswhich have been a newer version andreplaces them with the next version.
  • versions:commit removes the pom.xml.versionsBackup files. Formsone half of the built-in "Poor Man'sSCM".
  • versions:revert restores the pom.xml files from thepom.xml.versionsBackup files. Formsone half of the built-in "Poor Man'sSCM".

Just thought I'd include it for any future reference.


Please take a look at this page (section "Dependency Version Ranges"). What you might want to do is something like

<version>[1.2.3,)</version>

These version ranges are implemented in Maven2.