33

Let's say I have one project with the following POM:

<groupId>com.mine</groupId>
<artifactId>coreJar</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>

And then in another project I always want to reference the latest SNAPSHOT:

<dependencies> 
    <dependency>
        <groupId>com.mine</groupId>
        <artifactId>coreJar</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
    ...
<dependencies> 

But instead of 0.0.1-SNAPSHOT, I want it to always grab the latest SNAPSHOT version. In the past you could use LATEST, but this has since been deprecated (for reasonable reasons).

I do understand you can specify versions, such as:

[1.5,)

But I could never get it to work with a "-SNAPSHOT":

[0.0.1,)-SNAPSHOT // Doesn't work!

The question then is how do I get maven to grab the latest SNAPSHOT in my other project?

Stephane Grenier
  • 15,527
  • 38
  • 117
  • 192
  • Have a look here: http://stackoverflow.com/questions/30571/how-do-i-tell-maven-to-use-the-latest-version-of-a-dependency#answer-1172371 – dma_k Mar 01 '10 at 22:31

7 Answers7

45

Another option (which I use) is to include the following in your pom.xml. updatePolicy tag will force maven to always use latest snapshot from this repo.

<repositories>
    <repository>
        <id>you-snapshots</id>
        <url>http://host/nexus/repos/snapshots</url>
        <snapshots>
            <updatePolicy>always</updatePolicy>
        </snapshots>
        <releases>
            <updatePolicy>always</updatePolicy>
        </releases>
    </repository>
</repositories>

p.s. I always configure all repos in pom.xml because we use several CI servers and it will be quite hard to configure all of them (I am lazy...)

Doc on settings.xml updatePolicy for reference. The frequency for downloading updates - can be "always", "daily" (default), "interval:XXX" (in minutes) or "never" (only if it doesn't exist locally).

Greg Domjan
  • 13,943
  • 6
  • 43
  • 59
Worker
  • 2,411
  • 6
  • 29
  • 55
16

Use

mvn -U, --update-snapshots

Forces a check for updated releases and snapshots on remote repository

Tim
  • 19,793
  • 8
  • 70
  • 95
  • I think the question is asking how to update to the latest SNAPSHOT, not just the latest 0.0.1-SNAPSHOT - which is what -U would do. – Martin Mar 01 '10 at 21:02
  • 3
    @Strawberry: I don't see any reasonable explanation of how `0.0.1-SNAPSHOT` can become `0.0.2-SNAPSHOT` or any other later version. – dma_k Mar 01 '10 at 22:18
15

A few words about dependency ranges and SNAPSHOT dependencies (quoting the Dependency Mediation and Conflict Resolution design document):

Incorporating SNAPSHOT versions into the specification

Resolution of dependency ranges should not resolve to a snapshot (development version) unless it is included as an explicit boundary. There is no need to compile against development code unless you are explicitly using a new feature, under which the snapshot will become the lower bound of your version specification. As releases are considered newer than the snapshot they belong to, they will be chosen over an old snapshot if found.

So, to answer your question, the only way to use a SNAPSHOT with dependency ranges is as boundary and you won't get higher SNAPSHOT versions automatically by design (which really makes sense).

Personally, I don't like to use dependency ranges because I find that it can lead to build reproducibility issues and makes the build more fragile. I do not recommend them.

Just in case, upgrading the SNAPSHOT version typically means that you are releasing some code and the maven release plugin provides support for that (see the Updating POM Versions).

Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
8

There is a Versions plugin for Maven which allows you to update your pom to the latest greatest SNAPSHOTS in visible repositories. It does this by inspecting your pom and comparing against remote repositories and then modifying as required.

It is a useful tool but I would definitely like to see an equivalent to the deprecated LATEST option. I find this kind of dependency particularly useful in continuous integration scenarios.

akokskis
  • 1,486
  • 15
  • 32
Martin
  • 7,089
  • 3
  • 28
  • 43
3

use mvn install -U u must use this to force maven to get the latest snapshots

1

It's

<version>[0.0-SNAPSHOT,)</version>
Dimath
  • 381
  • 1
  • 4
  • 13
0

In case you want to update your SNAPSHOT releases inside Eclipse (when using m2e / m2eclipse), right click the affected project, then select "Maven" -> "Update Project..." -> "OK" (with selected project causing problems).

Abdull
  • 26,371
  • 26
  • 130
  • 172
  • Why does Eclipse not consider the updatePolicy=always setting from settings.xml? Is it intended or a bug of the Maven Eclipse plugin? – schoenk Dec 02 '15 at 13:04