Maven - Snapshots



A large software application generally consists of multiple modules and it is common scenario where multiple teams are working on different modules of same application. For example, consider a team is working on the front end of the application as app-ui project (app-ui.jar:1.0) and they are using data-service project (data-service.jar:1.0).

Now it may happen that team working on data-service is undergoing bug fixing or enhancements at rapid pace and they are releasing the library to remote repository almost every other day.

Now if dataservice team uploads a new version every other day, then following problems will arise −

  • dataservice team should tell app-ui team every time when they have released an updated code.

  • appui team required to update their pom.xml regularly to get the updated version.

To handle such kind of situation, SNAPSHOT concept comes into play.

What is SNAPSHOT?

SNAPSHOT is a special version that indicates a current development copy. Unlike regular versions, Maven checks for a new SNAPSHOT version in a remote repository for every build.

Now data-service team will release SNAPSHOT of its updated code every time to repository, say data-service: 1.0-SNAPSHOT, replacing an older SNAPSHOT jar.

Snapshot vs Version

In case of Version, if Maven once downloaded the mentioned version, say dataservice:1.0, it will never try to download a newer 1.0 available in repository. To download the updated code, data-service version is be upgraded to 1.1.

In case of SNAPSHOT, Maven will automatically fetch the latest SNAPSHOT (dataservice:1.0-SNAPSHOT) every time app-ui team build their project.

appui pom.xml

app-ui project is using 1.0-SNAPSHOT of data-service.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>appui</groupId>
   <artifactId>appui</artifactId>
   <packaging>jar</packaging>
   <version>1.0</version>
   <name>appui</name>
   <dependencies>
      <dependency>
         <groupId>junit</groupId>
         <artifactId>junit</artifactId>
         <version>3.8.1</version>
         <scope>test</scope>
      </dependency>
      <dependency>
         <groupId>dataservice</groupId>
         <artifactId>dataservice</artifactId>
         <version>1.0-SNAPSHOT</version>
      </dependency>
   </dependencies>
</project>

dataservice pom.xml

dataservice project is releasing 1.0SNAPSHOT for every minor change.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>dataservice</groupId>
   <artifactId>dataservice</artifactId>
   <packaging>jar</packaging>
   <version>1.0-snapshot</version>
   <name>dataservice</name>
   <url>http://maven.apache.org</url>
   <dependencies>
      <dependency>
         <groupId>junit</groupId>
         <artifactId>junit</artifactId>
         <version>3.8.1</version>
         <scope>test</scope>
      </dependency>
   </dependencies>
</project>

Although, in case of SNAPSHOT, Maven automatically fetches the latest SNAPSHOT on daily basis, you can force maven to download latest snapshot build using -U switch to any maven command.

mvn clean package -U

Example - Usage of snapshots

Let's create two maven project appui and dataservice where appui is using version as 1.0 and dataservice is having version as 1.0-SNAPSHOT.

Create appui project

D:\Projects\MVN>mvn archetype:generate -DgroupId=appui -DartifactId=appui -Dversion=1.0 -Dname=app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Create dataservice project

D:\Projects\MVN>mvn archetype:generate -DgroupId=dataservice -DartifactId=dataservice -Dversion=1.0-snapshot -Dname=dataservice -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Update pom.xml to have data-service dependency

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>appui</groupId>
  <artifactId>appui</artifactId>
  <packaging>jar</packaging>
  <version>1.0</version>
  <name>app-ui</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>dataservice</groupId>
      <artifactId>dataservice</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
  </dependencies>
</project>

Let's open the command console, go to the D:\Projects\MVN\dataservice directory and execute the following mvn command to publish dataservice to local repository.

D:\Projects\MVN\dataservice>mvn install
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------------< dataservice:dataservice >-----------------------
[INFO] Building dataservice 1.0-snapshot
[INFO]   from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- resources:3.3.1:resources (default-resources) @ dataservice ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\Projects\MVN\dataservice\src\main\resources
[INFO]
[INFO] --- compiler:3.13.0:compile (default-compile) @ dataservice ---
[INFO] Nothing to compile - all classes are up to date.
[INFO]
[INFO] --- resources:3.3.1:testResources (default-testResources) @ dataservice ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\Projects\MVN\dataservice\src\test\resources
[INFO]
[INFO] --- compiler:3.13.0:testCompile (default-testCompile) @ dataservice ---
[INFO] Nothing to compile - all classes are up to date.
[INFO]
[INFO] --- surefire:3.2.5:test (default-test) @ dataservice ---
[INFO] Using auto detected provider org.apache.maven.surefire.junit.JUnit3Provider
[INFO]
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running dataservice.AppTest
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.038 s -- in dataservice.AppTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO]
[INFO] --- jar:3.4.1:jar (default-jar) @ dataservice ---
[INFO] Building jar: D:\Projects\MVN\dataservice\target\dataservice-1.0-snapshot.jar
[INFO]
[INFO] --- install:3.1.2:install (default-install) @ dataservice ---
[INFO] Installing D:\Projects\MVN\dataservice\pom.xml to C:\Users\mahes\.m2\repository\dataservice\dataservice\1.0-snapshot\dataservice-1.0-snapshot.pom
[INFO] Installing D:\Projects\MVN\dataservice\target\dataservice-1.0-snapshot.jar to C:\Users\mahes\.m2\repository\dataservice\dataservice\1.0-snapshot\dataservice-1.0-snapshot.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.216 s
[INFO] Finished at: 2025-09-15T16:34:03+05:30
[INFO] ------------------------------------------------------------------------

Let's open the command console, go to the D:\Projects\MVN\app-ui directory and execute the following mvn command.

D:\Projects\MVN\appui>mvn clean package -U

Maven will start building the project after downloading the latest SNAPSHOT of dataservice.

[INFO] Scanning for projects...
[INFO]
[INFO] ----------------------------< appui:appui >-----------------------------
[INFO] Building appui 1.0
[INFO]   from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- clean:3.2.0:clean (default-clean) @ appui ---
[INFO] Deleting D:\Projects\MVN\appui\target
[INFO]
[INFO] --- resources:3.3.1:resources (default-resources) @ appui ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\Projects\MVN\appui\src\main\resources
[INFO]
[INFO] --- compiler:3.13.0:compile (default-compile) @ appui ---
[INFO] Recompiling the module because of changed source code.
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file with javac [debug target 1.8] to target\classes
[WARNING] bootstrap class path is not set in conjunction with -source 8
  not setting the bootstrap class path may lead to class files that cannot run on JDK 8
    --release 8 is recommended instead of -source 8 -target 1.8 because it sets the bootstrap class path automatically
[WARNING] source value 8 is obsolete and will be removed in a future release
[WARNING] target value 8 is obsolete and will be removed in a future release
[WARNING] To suppress warnings about obsolete options, use -Xlint:-options.
[INFO]
[INFO] --- resources:3.3.1:testResources (default-testResources) @ appui ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\Projects\MVN\appui\src\test\resources
[INFO]
[INFO] --- compiler:3.13.0:testCompile (default-testCompile) @ appui ---
[INFO] Recompiling the module because of changed dependency.
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file with javac [debug target 1.8] to target\test-classes
[WARNING] bootstrap class path is not set in conjunction with -source 8
  not setting the bootstrap class path may lead to class files that cannot run on JDK 8
    --release 8 is recommended instead of -source 8 -target 1.8 because it sets the bootstrap class path automatically
[WARNING] source value 8 is obsolete and will be removed in a future release
[WARNING] target value 8 is obsolete and will be removed in a future release
[WARNING] To suppress warnings about obsolete options, use -Xlint:-options.
[INFO]
[INFO] --- surefire:3.2.5:test (default-test) @ appui ---
[INFO] Using auto detected provider org.apache.maven.surefire.junit.JUnit3Provider
[INFO]
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running appui.AppTest
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.035 s -- in appui.AppTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO]
[INFO] --- jar:3.4.1:jar (default-jar) @ appui ---
[INFO] Building jar: D:\Projects\MVN\appui\target\appui-1.0.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.558 s
[INFO] Finished at: 2025-09-15T16:34:58+05:30
[INFO] ------------------------------------------------------------------------
Advertisements