Using the SNAPSHOT mechanism

The SNAPSHOT is a techique in Maven which allows to create a dependency on a library without setting the specific release number in the pom.xml.

Maven will automatically check the repository and download the latest version of the library. This comes very handy with libraries during the early stages of development, when the jar file might be rebuilt very often (and you don’t want to keep updating the pom but still ensure some version control).

Set the version number

Set the version number of the library (pom.xml) including SNAPSHOT as part of the number (e.g.<version>0.1-SNAPSHOT</version>).

DistributionManagement tag

Define the repository for your snapshot

In the project pom.xml add:

<distributionManagement>
  <snapshotRepository>
    <id>ssh-repository</id><name>Repository for Maven artifacts</name>
    <url>scp://dev.mycompany.com/maven/snapshots</url>
    <uniqueVersion>false</uniqueVersion>
   </snapshotRepository>
 </distributionManagement>

Notice the <uniqueVersion> tag: this can be used to obtain the following behaviour:

  • true: ensure the generated version number is unique, for instance each build generates mylib-0.2-SHAPSHOT-20090721.073600-1, mylib-0.2-SHAPSHOT-20090721.073600-2, etc..
  • false: version number is not unique, each build will generate mylib-0.2-SHAPSHOT

Use false to reuse disk space as each build overwrites the previous one.

Every time the goal deploy is run the new snapshot will be deployed to the repository (see also Internal Repository – Deploy artifacts with SSH)

Dependency in “client” applications

The applications which wish to use the library will only need to create a standard dependency in the pom.xml:

<dependency>
  <groupId>com.mycompany</groupId>
  <artifactId>my-lib</artifactId>
  <version>0.1-SNAPSHOT</version>
</dependency>

One Response to Using the SNAPSHOT mechanism

  1. Bob says:

    Just a note that unique version is removed in Maven 3.
    I followed the example you have with the latest Maven, and spent hours trying to figure out why it didn’t work as described in your blog post.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: