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 setup, configuration, and interview questions.
Do visit our website to read more such blogs. Make sure you enroll in our courses. You can take mock tests, solve 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! 💻