tags:

views:

354

answers:

4

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?

A: 

Use

mvn -U, --update-snapshots

Forces a check for updated releases and snapshots on remote repository

Tim
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.
Strawberry
@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
+1  A: 

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.

Strawberry
I find this kind of dependency particularly *harmful*.
Pascal Thivent
+3  A: 

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
@Pascal: Can you give an example of what is "dependency ranges as explicit boundary"? Is it `[0.0,1000.0]-SNAPSHOT` for example?
dma_k
@dma_k [1.0-SNAPSHOT,)
Pascal Thivent
A: 

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&lt;/url&gt;
        <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...)

MinimeDJ