Maven SNAPSHOT

In Maven, SNAPSHOT is a special version identifier used to denote the current development version of an artifact. When a SNAPSHOT version is built, it incorporates the latest changes made to the project. It allows developers to share and test the most recent code changes without waiting for a formal release. SNAPSHOTs are typically used during development, and not for production or public releases.

In this tutorial, we'll cover the basics of using SNAPSHOTs in your Maven projects.

1. Set the SNAPSHOT version

To create a SNAPSHOT version of your project, add -SNAPSHOT to your project's version number in the pom.xml:

<version>1.0.0-SNAPSHOT</version>

With this configuration, every build of your project will produce a SNAPSHOT artifact containing the latest code changes.

2. Install the SNAPSHOT artifact

To share the SNAPSHOT artifact with other developers or projects, install it into your local Maven repository. Use the following Maven command:

mvn clean install

This command builds and installs the SNAPSHOT artifact to your local repository. The installed artifact will have a unique timestamp and build number, such as 1.0.0-20230511.152300-1.

3. Use the SNAPSHOT artifact in another project

To use the SNAPSHOT artifact in another Maven project, add a dependency to the project's pom.xml with the SNAPSHOT version number:

<dependencies>
  <dependency>
    <groupId>com.example</groupId>
    <artifactId>my-project</artifactId>
    <version>1.0.0-SNAPSHOT</version>
  </dependency>
</dependencies>

When the project is built, Maven will use the latest SNAPSHOT artifact from the local repository. If you want to use a SNAPSHOT artifact from a remote repository, add the repository's configuration to the pom.xml:

<repositories>
  <repository>
    <id>remote-repo-id</id>
    <url>http://path.to/remote/repo</url>
    <snapshots>
      <enabled>true</enabled>
    </snapshots>
  </repository>
</repositories>

Replace remote-repo-id with a unique identifier for the remote repository, and http://path.to/remote/repo with the repository URL.

4. Deploy the SNAPSHOT artifact

To deploy the SNAPSHOT artifact to a remote repository, configure the distributionManagement element in your pom.xml:

<distributionManagement>
  <snapshotRepository>
    <id>remote-repo-id</id>
    <url>http://path.to/remote/repo</url>
  </snapshotRepository>
</distributionManagement>

Replace remote-repo-id and http://path.to/remote/repo with the repository identifier and URL, respectively.

Next, add the required credentials to your settings.xml file (located in your .m2 directory, e.g., ~/.m2/settings.xml):

<servers>
  <server>
    <id>remote-repo-id</id>
    <username>your-username</username>
    <password>your-password</password>
  </server>
</servers>

Replace your-username and your-password with your repository credentials.

Finally, deploy the SNAPSHOT artifact using the following Maven command:

mvn clean deploy

This command builds and deploys the SNAPSHOT artifact to the remote repository.

In conclusion, SNAPSHOTs allow you to share and test the latest development changes within your Maven projects.

  1. How to use Maven SNAPSHOT dependencies:

    • Include SNAPSHOT dependencies in your project's POM.xml to use the latest development version.
    <!-- Example SNAPSHOT dependency in POM -->
    <dependencies>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>my-library</artifactId>
            <version>1.0.0-SNAPSHOT</version>
        </dependency>
        <!-- Additional dependencies... -->
    </dependencies>
    
  2. Publishing and deploying Maven SNAPSHOT artifacts:

    • Use the deploy goal to publish SNAPSHOT artifacts to a SNAPSHOT repository.
    mvn clean deploy
    
  3. Maven SNAPSHOT plugin usage:

    • The Maven Deploy Plugin is commonly used to deploy SNAPSHOT artifacts.
    <!-- Example Maven Deploy Plugin configuration -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-deploy-plugin</artifactId>
                <version>3.0.0</version>
                <!-- Additional configurations... -->
            </plugin>
        </plugins>
    </build>
    
<!-- Example SNAPSHOT dependency in POM -->
<dependencies>
    <dependency>
        <groupId>com.example</groupId>
        <artifactId>my-library</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </dependency>
    <!-- Additional dependencies... -->
</dependencies>