Snapshots in Maven


Snapshots in Maven

A large software application typically has numerous modules, and it's not uncommon for multiple teams to work on separate modules of the same application. Consider a team working on the application's front end as an app UI project (app UI.jar:1.0) and using a data-service project (data-service.jar:1.0).

Now the data-service team may be working on bug fixes or additions at a breakneck speed, publishing the library to remote repositories virtually every other day.

If the data-service team uploads a new version every other day, the following issues will arise: the data-service team should notify the app UI team whenever they release new code.

To receive the latest version, the app UI team needed to update their pom.xml regularly.

The SNAPSHOT idea is used to deal with such a predicament.

SNAPSHOT is a specific version that denotes a development copy currently in use. Maven checks for a new SNAPSHOT version in a remote repository for every build, unlike ordinary versions.

The data-service team will now regularly release a SNAPSHOT of its modified code to the repository, with the name data-service: 1.0-SNAPSHOT, to replace a previous SNAPSHOT jar.

Version vs. Snapshot

When it comes to version, if Maven has already downloaded a version, such as data-service:1.0, it will never try to download a newer 1.0 from the repository. The data-service version is upgraded to 1.1 to get the updated code.

When the app UI team builds their project with SNAPSHOT, Maven will automatically fetch the most recent SNAPSHOT (data-service:1.0-SNAPSHOT).

The app UI pom.xml project uses the data-service 1.0-SNAPSHOT.

<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/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>

data-service pom.xml

Every small modification in the data-service project is released as 1.0-SNAPSHOT.

<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/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>

Although Maven collects the newest SNAPSHOT on a daily basis in the case of SNAPSHOT, you may compel Maven to fetch the latest snapshot build by passing the -U switch to any maven command.

clean package -U mvn

Open the command prompt, navigate to the C: > MVN > app-ui directory, and run the mvn command.

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

After obtaining the latest SNAPSHOT of data-service, Maven will begin developing the project.

[INFO] Scanning for projects... [INFO]-------------------------------------------- [INFO] Building consumerBanking [INFO] task-segment: [clean, package] [INFO]-------------------------------------------- [INFO] Downloading data-service:1.0-SNAPSHOT [INFO] 290K downloaded. [INFO] [clean:clean {execution: default-clean}] [INFO] Deleting directory C:\MVN\app-ui\target [INFO] [resources:resources {execution: default-resources}] [WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory C:\MVN\app-ui\src\main\resources [INFO] [compiler:compile {execution:default-compile}] [INFO] Compiling 1 source file to C:\MVN\app-ui\target\classes [INFO] [resources:testResources {execution: default-testResources}] [WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory C:\MVN\app-ui\src\test\resources [INFO] [compiler:testCompile {execution: default-testCompile}] [INFO] Compiling 1 source file to C:\MVN\app-ui\target\test-classes [INFO] [surefire:test {execution: default-test}] [INFO] Surefire report directory: C:\MVN\app-ui\target\ surefire-reports -------------------------------------------------- T E S T S -------------------------------------------------- Running com.companyname.bank.AppTest Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.027 sec Results : Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [jar:jar {execution: default-jar}] [INFO] Building jar: C:\MVN\app-ui\target\ app-ui-1.0-SNAPSHOT.jar [INFO]-------------------------------------------------------- [INFO] BUILD SUCCESSFUL [INFO]-------------------------------------------------------- [INFO] Total time: 2 seconds [INFO] Finished at: 2015-09-27T12:30:02+05:30 [INFO] Final Memory: 16M/89M [INFO]------------------------------------------------------------------------