Horje
Maven - Snapshots

A Maven Snapshot is a specific version of a Maven package that contains the most recent production branch code. It is a development version that comes before the final release version. A snapshot version of a Maven package is identified by the suffix SNAPSHOT applied to the package version number.

Snapshot

Snapshot is a special version that represents a current development copy. Maven looks for a new Snapshot version in a remote repository for each build. Every time the data-service team updates its code, it will release a Snapshot to the repository, naming it data-service: 1.0.0-SNAPSHOT and replacing an older SNAPSHOT jar.

Example of declaring a snapshot in a Maven project:

<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>my-library</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>

Create plugins in Maven – Snapshots

Whenever you want to customize the build for a Maven project, you do it by adding or reconfiguring plugins. For this example, we’ll set the Java compiler to accept JDK 5.0 sources. This is as simple as adding the following to your POM:

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>4.1.2.</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>

Working of Snapshots

Configure app-ui pom.xml

The app-ui project uses the data service version 1.0-SNAPSHOT.

XML
<project xmlns="http://maven.apache.org/POM/4.1.2"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.1.2.xsd">
    <modelVersion>4.1.2</modelVersion>
    <groupId>org.horje.appui</groupId>
    <artifactId>app-ui</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging>
    <name>App UI Module</name>
    <url>http://horje.org</url>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.example.dataservice</groupId>
            <artifactId>data-service</artifactId>
            <version>1.0.0-SNAPSHOT</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>


Use data-service pom.xml

Every small modification in the data-service project results in a 1.0-SNAPSHOT release.

XML
<project xmlns="http://maven.apache.org/POM/4.1.2"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.1.2.xsd">
    <modelVersion>4.1.2</modelVersion>
    <groupId>org.horje.dataservice</groupId>
    <artifactId>data-service</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>Data Service Module</name>
    <url>http://horje.org</url>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
</project>


Output:

Maven Build Successful


Deploying Snapshots

Step 1: Assume, we have a project in development with a SNAPSHOT version.

<groupId>org.horje</groupId>
<artifactId>maven-snapshot-repository</artifactId>
<version>1.1.0-SNAPSHOT</version>


Step 2: Provide the publishing repository where we want to deploy the artifact. We can utilize the distribution management plugin.

<distributionManagement>
<!-- Define the repository for managing snapshot distributions -->
<snapshotRepository>
<!-- The unique identifier for the snapshot repository -->
<id>tipid</id>

<!-- A human-readable name for the snapshot repository -->
<name>tipid-snapshot</name>

<!-- The URL where the snapshot repository is hosted -->
<url>http://localhost:8081/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>


Step 3: The maven-metadata.xml file contains accurate information about the snapshot version and a link to the most recent timestamp value.

XML
<metadata modelVersion="1.1.1">
    <groupId>org.horje</groupId>
    <artifactId>custom-maven-repo</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <versioning>
        <snapshot>
            <timestamp>20240615.143210</timestamp>
            <buildNumber>5</buildNumber>
        </snapshot>
        <lastUpdated>20240615143210</lastUpdated>
        <snapshotVersions>
            <snapshotVersion>
                <extension>jar</extension>
                <value>1.0.0-20230615.143210-5</value>
                <updated>20240615143210</updated>
            </snapshotVersion>
            <snapshotVersion>
                <extension>pom</extension>
                <value>1.0.0-20230615.143210-5</value>
                <updated>20240615143210</updated>
            </snapshotVersion>
        </snapshotVersions>
    </versioning>
</metadata>


Snapshot Repository Configuration

Step 1: To download the item from the repository, we first need to set up a dependencies repository.

<repositories>
<!-- Define a repository for Maven snapshots -->
<repository>
<!-- The unique identifier for this repository -->
<id>SNAPSHOT</id>

<!-- A human-readable name for this repository -->
<name>SNAPSHOT</name>

<!-- The URL where this repository can be accessed -->
<url>http://localhost:8081/repository/maven-snapshots/</url>

<!-- Configuration for handling snapshot versions -->
<snapshots>
<!-- Enable the use of snapshots from this repository -->
<enabled>true</enabled>
</snapshots>

<!-- Configuration for handling release versions -->
<releases>
<!-- Disable the use of releases from this repository -->
<enabled>false</enabled>
</releases>
</repository>
</repositories>


Step 2: Enabling snapshots allows us to specify how frequently we want to check for updated versions of the SNAPSHOT artifacts.

<snapshots>
<!-- Enable the use of snapshots from this repository -->
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>

Benefits of using snapshots

  • Organizations are often transferring databases from on-premises to the cloud or from cloud to cloud for convenient processes.
  • Application development is a dynamic field, developers are continuously creating new VMs or containers, loading massive databases into them.
  • Regular snapshots of databases and storage volumes provide a last line of defense if other cybersecurity preventions are broken.

Drawbacks of Using Snapshots

  • Vulnerable to disruptions that harm production servers.
  • Consumes a significant portion of the primary storage space.
  • Lacks granularity, you must recover data in its entirety, as individual files cannot be recovered from snapshots.





Reffered: https://www.geeksforgeeks.org


Advance Java

Related
Implementing CORS in Spring Boot with Spring Security Implementing CORS in Spring Boot with Spring Security
Deserialize Java 8 LocalDateTime with JacksonMapper Deserialize Java 8 LocalDateTime with JacksonMapper
Securing REST APIs with Spring Security Securing REST APIs with Spring Security
Maven Commands and Options Maven Commands and Options
Implementing Blue-Green Deployments with Eureka in Java Microservices Implementing Blue-Green Deployments with Eureka in Java Microservices

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
15