Understanding SNAPSHOT dependencies

In Maven, a SNAPSHOT version is a version of the project/dependency that has not been released. This is indicated by suffixing SNAPSHOT to the version number. Here's an example:

<version>1.0-SNAPSHOT</version>

You will notice that the project we created using the Maven archetype quickstart had a SNAPSHOT version.

The version number specified before -SNAPSHOT is the version that the released project/dependency is expected to have. So, 1.0-SNAPSHOT indicates 1.0 is not released yet.

As the SNAPSHOT version indicates software under development, Maven deals with these dependencies differently.

How to do it...

One would rarely use the SNAPSHOT version of an external dependency. If you are developing a multi-module project in your organization, chances are you will use SNAPSHOT versions of other modules required in your project.

Let us try the following contrived example:

  1. Open one of the projects that we have created.
  2. Add the following dependency:
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.1.0.BUILD-SNAPSHOT</version>
    </dependency>
  3. Add the following code to specify the repository where the dependency is available:
    <repositories>
        <repository>
            <id>repository.spring.snapshot</id>
            <name>Spring Snapshot Repository</name>
            <url>http://repo.spring.io/snapshot</url>
        </repository>
    </repositories>
  4. Run the following command:
    C:projectsapache-maven-cookbookproject-with-snapshot-dependencies>mvn verify
  5. Observe the following results:
    [INFO] Scanning for projects...
    [INFO]
    [INFO] ------------------------------------------------------------------------
    [INFO] Building Project with SNAPSHOT dependencies 1.0-SNAPSHOT
    [INFO] ------------------------------------------------------------------------
    Downloading:http://repo.spring.io/snapshot/org/springframework/spring-context/4.1.2.BUILD-SNAPSHOT/maven-metadata.xml
    Downloaded:http://repo.spring.io/snapshot/org/springframework/spring-context/4.1.2.BUILD-SNAPSHOT/maven-metadata.xml (3 KB at 1.7 KB/sec)
    Downloading:http://repo.spring.io/snapshot/org/springframework/spring-context/4.1.2.BUILD-SNAPSHOT/spring-context-4.1.2.BUILD-20141107.161556-92.pom
    Downloaded:http://repo.spring.io/snapshot/org/springframework/spring-context/4.1.2.BUILD-SNAPSHOT/spring-context-4.1.2.BUILD-20141107.161556-92.pom (5 KB at 6.8 KB/sec)
    

How it works...

The first thing you would have seen is the need to define a specific repository to download the dependencies. These dependencies are not available in the usual repositories. They reside separately in repositories called snapshot repositories. In the preceding example, we specified the snapshot repository where the desired dependencies were available.

The second thing you would notice are the filenames. Each artifact that is being downloaded is appended with 20141107.161556-92. This is a unique identifier for each SNAPSHOT version in the repository. This value changes each time a new SNAPSHOT version is available in the repository.

Maven treats SNAPSHOT versions differently from release versions.

For a release version, Maven checks if the artifact is available in the local repository that is already downloaded. If so, it does not attempt to fetch the same from the remote repositories.

For SNAPSHOT versions, even if the artifact is available locally, it checks the SNAPSHOT version for updates in the remote repository based on the update policy that can be configured.

By default, the update interval is once a day. This means, if Maven downloads a SNAPSHOT dependency at noon today, it will check for an update to it at noon tomorrow and not before that, irrespective of how many times you build the project.

The update interval can be specified in the repository section of the pom or settings file as follows:

<updatePolicy>always<updatePolicy>

The choices are always, daily (default), interval:X (where X is an integer in minutes), or never. Let's discuss in brief about these choices:

  • always: This checks for updates for every Maven run.
  • daily: This checks for updates once a day. This does not necessarily mean exactly 24 hours from the last check; just once a day at the start of the day.
  • interval:X: This checks for updates after a specified time.

    Tip

    In a multi-module project, it is good to set the updatePolicy element value to always for intermodule dependencies.

There's more...

As mentioned earlier, there are separate repositories for release and snapshot versions. By default, snapshots are disabled for a release repository and vice versa. The repository element has separate releases and snapshots sub-elements where this can be specified:

       <repository>
          <id>my-repo</id>
          <name>My Release Repo</name>
          <releases>
            <enabled>true</enabled>
            <updatePolicy>never</updatePolicy>
            <checksumPolicy>fail</checksumPolicy>
          </releases>
          <snapshots>
            <enabled>false</enabled>
            <updatePolicy>always</updatePolicy>
            <checksumPolicy>fail</checksumPolicy>
          </snapshots>
          <url>http://my.repo.url</url>
          <layout>default</layout>
        </repository>

Typically, for a release repository, enabled will be false for snapshots. For a snapshot repository, enabled will be true for snapshots and false for releases. This is so that Maven looks at the right repository for the right artifacts and does not unnecessarily look at the wrong repositories each time it needs a dependency.

The checksumPolicy element tells Maven what to do in case the checksum of the downloaded dependency does not match the actual checksum. The value of fail will stop the build with a checksum error.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.247.11