How to Tell Maven to Use the Latest Version of a Dependency

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.

How to get the latest RELEASE version of dependencies in maven 3?

As Glains said in comment, this link is fine for MAVEN 3 How do I tell Maven to use the latest version of a dependency?

Even the RELEASE keyword.

[1.5.0-RC1,1.6.0)

How to always get the latest version of an specific dependency in maven 3.x?

You need to have two separate runs of Maven:

  1. Update the version with the versions maven plugin on command line.
  2. Run something like mvn clean verify to build the project.

You cannot change the version while building the project.

BTW: You configured Java 5. Are you really sure you want this?

Maven3- How to tell maven to use latest versions using verions-maven-plugin?

This is how I achieved what I am looking for(Always take latest versions of dependencies):

I have setup a pre-step to "invoke top level maven targets" in jenkins and triggers the command:

mvn versions:use-latest-versions -DallowSnapshots=true -DexcludeReactor=false

on the parent pom
This will update the dependency versions in the parent POM and all its child POM's.
Thank you khmarbaise for trying to help me. I appreciate your time.

mvn command for updating version in dependency and one project that uses the dependency

You can just go to the two POM files you mentioned and change the version number in the file.

I would first change the version of the core, then run a build mvn clean install (if this done on your local machine) or mvn clean deploy (if you want to sent it to your company repository) and then change the version number of core in the other project.

Note that SNAPSHOT versions are for development. When you want to release something, create a release version, e.g. through the Maven release plugin.

How to find a Maven dependency version declaration imported from a BOM

I was trying to use mvn help:effective-pom -Dverbose from this other answer, but it didn't work for me because it was using maven-help-plugin:2.2, and -Dverbose was introduced in 3.2.0.

Instead, you should thus force the version in the command line:

mvn org.apache.maven.plugins:maven-help-plugin:3.2.0:effective-pom -Dverbose=true -Doutput=effective-pom.xml

(better to use -Doutput because the file can be big and it is not convenient to read it in a terminal)

Is there a way to tell maven to always use the latest _stable_ version of a dependency?

You can use RELEASE value in version element for your dependency to make Maven use the latest released version. However this is not the best practice, because it can break build reproduceability.

Also, Maven don't make logical differences between versions like 12.0.1 and 13.0-rc1. From Maven's point of view both of them are released versions and basically what you're trying to do is breaking Maven releases ideology in several ways.

So, instead of versioning artifacts like 13.0-rc1, you should do a regular releases and use special repositories and artifact promotion process as par of your release. So, you could have a release-candidates repository that can be used during testing and once test pass you'll promote those artifacts to a final release repository. But if you need to make changes, you'll just update released version, so 13.0, 13.0.1, etc...

Maven - Always download the latest version of dependency or plugin

you can declear your dependency version like <version>[1.0.0,)</version>

it will resolve after 1.0.0 version for your dependency.



Related Topics



Leave a reply



Submit