Daily Hack #day23 - Maven Range Patterns

Daily Hack #day23 - Maven Range Patterns

Maven range patterns are a convenient way to specify version ranges for dependencies in Maven projects. They allow developers to define flexible version constraints that ensure compatibility with a range of versions without specifying exact version numbers. Maven range patterns follow a concise syntax that includes various operators to define version boundaries.

Square brackets [ & ] mean closed (inclusive).

Parenthesis ( & ) mean open (exclusive).

For example, the range pattern "[1.0,)" indicates that the dependency is compatible with version 1.0 and any higher versions, while excluding version 1.0 itself. Similarly, the pattern "(1.0,2.0]" specifies compatibility with versions higher than 1.0 but less than or equal to 2.0, including both boundaries.

Using Maven range patterns helps streamline dependency management and ensures that projects remain compatible with evolving dependencies over time. It allows for easier updates and maintenance by automatically selecting compatible versions within the specified range.

Overall, Maven range patterns provide a flexible and robust mechanism for managing dependencies in Maven projects, promoting better dependency resolution and version management practices.

Here's an example illustrating the various options. In the Maven repository, cloudtuned.com:cloudtuned-library has the following metadata:

<?xml version="1.0" encoding="UTF-8"?><metadata>
  <groupId>com.cloudtuned</groupId>
  <artifactId>cloudtuned-library</artifactId>
  <version>2.0.0</version>
  <versioning>
    <release>1.1.1</release>
    <versions>
      <version>1.0</version>
      <version>1.0.1</version>
      <version>1.1</version>
      <version>1.1.1</version>
      <version>2.0.0</version>
    </versions>
    <lastUpdated>20240426140000</lastUpdated>
  </versioning>
</metadata>

Declare an open-ended version range (will resolve to 2.0.0):

<dependency>
  <groupId>com.cloudtuned</groupId>
  <artifactId>cloudtuned-library</artifactId>
  <version>[1.0,)</version>
</dependency>

The full specification of what you can do with this very powerful feature can be found on this link

Did you find this article valuable?

Support Cloud Tuned by becoming a sponsor. Any amount is appreciated!