Table of contents
1.
Introduction
2.
Need For Snapshots
3.
Snapshots in Maven
4.
Snapshot vs. Version
4.1.
app-ui POM.xml
4.2.
data-service POM.xml
5.
Frequently Asked Questions
5.1.
What do you understand by the Maven Build cycle?
5.2.
What is a Maven repository?
5.3.
Is Maven still a trend in 2022?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

SNAPSHOT IN MAVEN

Author Lokesh Sharma
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Hey Ninja! Welcome to another interesting article by coding ninjas. We hope that you are doing fine. In this article, we are going to discuss Snapshots in Maven. After reading this article, you will understand the need for Snapshots and how to use them. We will also see how a snapshot is different from a version. We will understand snapshots better using some examples.

Snapshot In Maven

Need For Snapshots

Ninja, we are pretty sure that you will develop large applications when you crack your dream company or start something of your own. But developing large applications is a challenging task. A large project is broken down into smaller modules to make things easier. Multiple teams may work together on different modules of the same application. 

Now, consider that you are building an application. A team of developers is already working on the front-end module of the application. The file they are working on is app-ui.jar:1.0 (application user interface). Suppose they need a module named data-service with the file data-service.jar:1.0. Now, you are developing and testing the data-service module.

You develop this module and frequently release the library to a remote repository. But this daily update of the module and releasing its new versions may create some problems for others. You and the team working on the front end may face the following issues:

  1. You may have to inform the front-end team (app.ui) every time you release a new module version.
     
  2. The front-end team will have to update their POM.xml regularly and update the version of data-service.jar.
     

We handle such kind of situations using Snapshots.

Snapshots in Maven

As the name suggests, Snapshot presents a copy of the currently developed project. Maven always checks out for a snapshot of the project whenever we build it. These snapshots keep getting updated regularly. If Maven finds a new Snapshot of the project, it updates the older .jar file of the project. 

Snapshots must exist only during the development phase of the application. The importance of Snapshots is that it tells us the current state of our project and that it is still under development. 

In your case, you release a Snapshot every time you update the data-service module. For example, a new data-service: 1.0 Snapshot will be released, replacing the older Snapshot jar file. 

Snapshot vs. Version

Let us first take the case of Version. If Maven downloads a particular version of a module, then it will never try to install the same version again. It will download only if a newer version of that module is available. For example, if data-service 1.0 exists and you release a new version of 1.0, then Maven will not update it in the local repository. It will only update if you release a newer version, data-service 1.1. 

However, Snapshot automatically gets the latest Snapshot available every time the front-end team builds their project.

Let us look at these example codes showing app-ui and data-service POM files. 

app-ui POM.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" 
   xmlns:xsi="http://www.codingninjas.com/2022/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
   http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   
   <groupId>app-ui</groupId>
   
   <artifactId>app-ui</artifactId>
   
   <version>1.0</version>
   <packaging>jar</packaging>
   <name>health</name>
   <url>http://maven.apache.org</url>
   
   <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
   </properties>
   <dependencies>
      <dependency>
      <groupId>data-service</groupId>
         <artifactId>data-service</artifactId>
         <version>1.0-SNAPSHOT</version>
         <scope>test</scope>
      </dependency>
   </dependencies>
</project>

 

You specify the version of Snapshot inside the <version> element as a dependency.

This code shows us how app-ui module uses the data-service Snapshot with version 1.0.

data-service POM.xml

<project xmlns = "http://maven.apache.org/POM/4.0.0" 
   xmlns:xsi = "http://www.codingninjas.com/2022/XMLSchema-instance"
   xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0 
   http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   
   <groupId>data-service</groupId>
   <artifactId>data-service</artifactId>
   <version>1.0-SNAPSHOT</version>
   <packaging>jar</packaging>
   <name>health</name>
   <url>http://maven.apache.org</url>
   
   <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
   </properties>
</project>

 

Maven fetches the latest Snapshot daily. We can also ask Maven to download the latest Snapshot whenever we want. We just have to add the -U switch to any Maven command in the console. 

mvn clean package -U

 

Now to use the created Snapshot, we will build our project. We open the front-end module (app-ui) inside the Maven directory. We then execute the following command in the console.

C:\MVN\app-ui>mvn clean package -U. 

As soon as you run this command, Maven will fetch the latest Snapshot version and start building the project.

Must Read Apache Server

Frequently Asked Questions

What do you understand by the Maven Build cycle?

A Maven build cycle decides the order of the execution of tasks. We divide each life cycle into different phases. We follow a sequence while running these phases.

There are three Build lifecycles in Maven clean, default, and site. 

What is a Maven repository?

A Maven repository stores all the Maven projects, library jars, documentation, and plugins in a single place. Maven can easily access this stored data. There are three types of repositories in Maven, central, remote, and local.

Is Maven still a trend in 2022?

Yes! Maven is still one of the most popular Java development tools in 2022. It is the ultimate tool for the building and management of projects. Maven makes the everyday work of Java developers an easy ride.

Conclusion

Did you enjoy the ride, Ninja? We hope you understood everything that we discussed in this article.

We discussed the need for Snapshots in Maven with a relevant example. We then understood the meaning of Snapshot. We compared it against the Version and saw how Snapshot helps. At last, we looked at some examples and setups to use Snapshots. 

If you wish to learn more about Maven and its various components, you can refer to blogs on maven setupconfiguration, and interview questions.

Do visit our website to read more such blogs. Make sure you enroll in our courses. You can take mock testssolve problems, and interview puzzles. Also, you can check out some exciting interview stuff- interview experiences and an interview bundle for placement preparations. Do upvote our blog to help fellow ninjas grow.

Keep Grinding! 🦾

Happy Coding! 💻

 

Live masterclass