Maven Code: How to Detect if You Have a SNAPSHOT Version

May 29, 2008 By Brian Fox

1 minute read time

I answer this question often enough that I decided to burn it to a blog so I don't have to dig it out of the source anymore...and maybe save someone else the time along the way.

Here is the relevant code that Maven uses to determine if an artifact is a snapshot or not:

<span style="font-family: arial;">String LATEST_VERSION = "LATEST";
String SNAPSHOT_VERSION = "SNAPSHOT";
Pattern VERSION_FILE_PATTERN = Pattern.compile(
 "^(.*)-([0-9]{8}.[0-9]{6})-([0-9]+)$" );
public boolean isSnapshot()
{
     if ( version != null || baseVersion != null )
     {
       Matcher m = VERSION_FILE_PATTERN.matcher( getBaseVersion() );
       if ( m.matches() )
       {
          setBaseVersion( m.group( 1 ) + "-" + SNAPSHOT_VERSION );
          return true;
       }
       else
       {
         return getBaseVersion().endsWith( SNAPSHOT_VERSION ) ||
         getBaseVersion().equals( LATEST_VERSION );
       }
     }
     else
     {
       return false;
     }
  }
</span>

Essentially this regex ( regular expression ) is checking if the version ends with SNAPSHOT or if the pattern matches a timestamped version such as "2.0.10-20080415.233129-15".

Tags: Nexus Repo Reel, How-To, Everything Open Source, snapshot, best practice, Maven

Written by Brian Fox

Brian Fox is a software developer, innovator and entrepreneur. He is an active contributor within the open source development community, most prominently as a member of the Apache Software Foundation and former Chair of the Apache Maven project. As the CTO and co-founder of Sonatype, he is focused on building a platform for developers and DevOps professionals to build high-quality, secure applications with open source components.