Google

Jul 11, 2014

Difference between Maven snapshot and release versions

Q. What is the difference between snapshot versions and release versions?
A. The term "SNAPSHOT" means the build is a snapshot of your code at a given time, which means downloading 1.0-SNAPSHOT  today might give a different file than downloading it tomorrow or day after. When you are ready to release your project, you will change 1.0-SNAPSHOT to 1.0 in the pom.xml file. The 1.0 is the release version. After release, any new development work will stat using 1.1-SNAPSHOT and so on.

"SNAPSHOT" also means unstable version. Unlike regular versions, Maven checks for a new SNAPSHOT version in a remote repository at least once a day by default or if you use -U flag to every time you build. A team working on a cash-service module will release SNAPSHOT of its updated code every day to repository - let's say cash-service:1.0-SNAPSHOT replacing an older cash-service:1.0-SNAPSHOT jar. In case of released versions, if Maven once downloaded the version say cash-service:1.0, it will never try to download a newer 1.0 available in repository. In order to download the updated code, the cash-service version needs to be upgraded to 1.1.

When you release artifacts to nexus, you can have a snapshotRepository and a repository for the releases.

 <distributionManagement>
        <snapshotRepository>
            <id>mycompany-snapshots</id>
            <name>mycompany Snapshots</name>
            <url>http://myhost:8080/nexus/content/repositories/mycompany-snapshots</url>
            <uniqueVersion>true</uniqueVersion>
        </snapshotRepository>
        <repository>
            <id>mycompany-releases</id>
            <name>mycompany Releases</name>
            <url>http://myhost:8080/nexus/content/repositories/mycompany-releases</url>
        </repository>
    </distributionManagement>


Q. Why do you need a SNAPSHOT version?
A. If a team working on a cash-service jar module keeps uploading a new version every other day, the other teams that depend on this cash-service module

  1. Need to be regularly notified so that they can update their pom.xml file to use the newer version.
  2. In large projects, it is not easy to keep communicating these version changes. Using the older versions can cause unforeseen build issues

If you have a "SNAPSHOT" version that regularly gets overridden by the team that is making the regular changes and the team that depends on the  cash-service module will also get their local repositories updated and use the latest code. This will alleviate the above 2 problems.


Q. Do you have control over the snapshot versions?
A. Yes.

For example, a cash-service-1.0.jar library is considered as a stable version, and if Maven finds it in the local repository, it will use this one for the current build.

Now, if you need a cash-service-1.0-SNAPSHOT.jar library, Maven will know that this version is not stable and is subject to changes since it a SNAPSHOT. So, Maven will try to find a newer version in the remote repositories, even if a version of this library is found on the local repository. However, this check is made only once per day by default, but you can modify this update policy.

<repository>
    <id>central</id>
    <url>...</url>
    <snapshots>
        <enabled>true</enabled>
        <updatePolicy>always</updatePolicy>
    </snapshots>
</repository>

The other possible updatePolicy values are daily (i.e. default), interval:30 (every 30 minutes), and never.

  • always: Maven will check for a newer SNAPSHOT version on every build.
  • daily: Once a day (this is the default unless you use -U flag with mvn clean package -U)
  • interval:XXX: an interval in minutes (XXX)
  • never: Maven will never try to retrieve another version from the remote repository. It will do that only if it doesn't exist locally. The SNAPSHOT version will be handled as the stable version.




You can also turn off this by setting enabled to false.

Even though Maven automatically fetches the latest SNAPSHOT on a daily basis by default, you can force maven to download latest snapshot build using -U switch to any maven command.

 mvn clean package -U


Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home