News and Notes from the Makers of Nexus | Sonatype Blog

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

Written by Brian Fox | May 29, 2008

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".