Developers, Developers, Developers! Maksim Sorokin IT Blog

3Apr/11Off

Comparing Maven String Versions

This posts describes comparint Maven String versions. It is assumed, that you have a Maven-managed project.

Add a maven-artifact dependency

<dependency>
  <groupId>org.apache.maven</groupId>
  <artifactId>maven-artifact</artifactId>
  <version>3.0.3</version>
</dependency>

Then you could write something like this:

public int compare(String version1, String version2) {
  ArtifactVersion mavenVersion1 = new DefaultArtifactVersion(version1);
  ArtifactVersion mavenVersion2 = new DefaultArtifactVersion(version2);

  int result = mavenVersion1.compareTo(mavenVersion2);

  return result;
}