Class Coordinates

java.lang.Object
eu.maveniverse.domtrip.maven.Coordinates

public final class Coordinates extends Object
Represents Maven coordinates (groupId, artifactId, version, classifier, and type). This record is pure abstraction, and is neither an "artifact" nor a "dependency", is really just a structure to carry coordinates together (and validate them). Important: because of this, we used name type for last element, while in fact it is sometimes used as "type" and sometimes used as "extension", depending on context where this record is used. In real life artifacts have extension (and not type), while dependencies have type (or as in Maven resolver, extension derived from type).

This is a simple immutable record for representing Maven artifact coordinates. It provides convenient factory methods and string representations commonly used in Maven.

Maven 4 Inference Support: With Maven 4's inference mechanism, groupId and version may be inferred from the reactor or parent POM and might not be present in the build POM. This record allows null values for groupId and version to support such scenarios. Only artifactId is strictly required.

Usage Examples:

// Create Coordinates
Coordinates jar = Coordinates.of("org.junit.jupiter", "junit-jupiter", "5.9.2");
Coordinates pom = Coordinates.of("org.example", "my-project", "1.0.0", null, "pom");
Coordinates classified = Coordinates.of("org.example", "my-lib", "1.0.0", "sources", "jar");

// Maven 4 inference - groupId/version may be null
Coordinates inferred = Coordinates.of(null, "my-module", null, null, "jar");

// Get string representations
String ga = jar.toGA();           // "org.junit.jupiter:junit-jupiter"
String gav = jar.toGAV();         // "org.junit.jupiter:junit-jupiter:5.9.2"
String gatc = jar.toGATC();       // "org.junit.jupiter:junit-jupiter:jar"
String full = jar.toFullString(); // "org.junit.jupiter:junit-jupiter:jar:5.9.2"
Since:
0.3.0
  • Constructor Details

    • Coordinates

      public Coordinates(String groupId, String artifactId, String version, String classifier, String type)

      Note: groupId and version can be null to support Maven 4's inference mechanism. Only artifactId is strictly required.

      Throws:
      eu.maveniverse.domtrip.DomTripException - if requirements are not fulfilled.
  • Method Details

    • of

      public static Coordinates of(String groupId, String artifactId, String version)
      Creates a Coordinates with groupId, artifactId, and version (JAR type, no classifier).
      Parameters:
      groupId - the Maven groupId
      artifactId - the Maven artifactId
      version - the artifact version
      Returns:
      a new Coordinates instance
    • of

      public static Coordinates of(String groupId, String artifactId, String version, String classifier, String type)
      Creates a Coordinates with groupId, artifactId, version, classifier, and type.
      Parameters:
      groupId - the Maven groupId
      artifactId - the Maven artifactId
      version - the artifact version
      classifier - the artifact classifier (can be null)
      type - the artifact type (can be null, defaults to "jar")
      Returns:
      a new Coordinates instance
    • toGA

      public String toGA()
      Returns the groupId:artifactId string representation.
      Returns:
      GA string (e.g., "org.junit.jupiter:junit-jupiter")
    • toGAV

      public String toGAV()
      Returns the groupId:artifactId:version string representation.
      Returns:
      GAV string (e.g., "org.junit.jupiter:junit-jupiter:5.9.2")
    • toGATC

      public String toGATC()
      Returns the groupId:artifactId:type[:classifier] string representation.
      Returns:
      GATC string (e.g., "org.junit.jupiter:junit-jupiter:jar" or "org.example:lib:jar:sources")
    • toFullString

      public String toFullString()
      Returns the full string representation: groupId:artifactId:type[:classifier]:version.
      Returns:
      full coordinates string
    • withVersion

      public Coordinates withVersion(String newVersion)
      Returns a new Coordinates with the same coordinates but different version.
      Parameters:
      newVersion - the new version
      Returns:
      a new Coordinates instance with updated version
    • withType

      public Coordinates withType(String newType)
      Returns a new Coordinates with the same coordinates but different type.
      Parameters:
      newType - the new type
      Returns:
      a new Coordinates instance with updated type
    • predicateGA

      public Predicate<eu.maveniverse.domtrip.Element> predicateGA()
      Creates a predicate that matches elements by GA (groupId:artifactId).

      This is useful for filtering streams of elements to find matching artifacts:

      Coordinates junit = Coordinates.of("junit", "junit", "4.13.2");
      dependencies.children("dependency")
          .filter(junit.predicateGA())
          .findFirst();
      
      Returns:
      a predicate for matching elements by GA
      Since:
      0.3.0
    • predicatePluginGA

      public Predicate<eu.maveniverse.domtrip.Element> predicatePluginGA()
      Creates a predicate that matches plugin elements by GA (with default groupId handling).

      Maven plugins default to groupId "org.apache.maven.plugins" if not specified. This predicate handles that convention:

      Coordinates compiler = Coordinates.of("org.apache.maven.plugins", "maven-compiler-plugin", "3.11.0");
      plugins.children("plugin")
          .filter(compiler.predicatePluginGA())
          .findFirst();
      
      Returns:
      a predicate for matching plugin elements by GA
      Since:
      0.3.0
    • predicateGATC

      public Predicate<eu.maveniverse.domtrip.Element> predicateGATC()
      Creates a predicate that matches elements by GATC (groupId:artifactId:type[:classifier]).

      This is useful for filtering dependencies or artifacts with specific types and classifiers:

      Coordinates sources = Coordinates.of("org.example", "my-lib", "1.0.0", "sources", "jar");
      dependencies.children("dependency")
          .filter(sources.predicateGATC())
          .findFirst();
      
      Returns:
      a predicate for matching elements by GATC
      Since:
      0.3.0
    • fromPom

      public static Coordinates fromPom(Path pomPath) throws eu.maveniverse.domtrip.DomTripException
      Creates a POM Coordinates from a POM file by reading its GAV coordinates.

      This method reads the groupId, artifactId, and version from the POM file. If groupId or version are not present in the project element, it looks for them in the parent element. With Maven 4's inference mechanism, groupId and version may be inferred from the reactor and not present in the POM - in such cases, the returned Coordinates will have null values for these fields.

      Example:

      Path pomFile = Paths.get("pom.xml");
      Coordinates project = Coordinates.fromPom(pomFile);
      System.out.println("Project: " + project.toGAV());
      
      Parameters:
      pomPath - the path to the POM file
      Returns:
      a new Coordinates instance representing the POM (groupId and version may be null)
      Throws:
      eu.maveniverse.domtrip.DomTripException - if artifactId is missing
      Since:
      0.3.0
    • groupId

      public String groupId()
      Returns the Maven groupId.
      Returns:
      the groupId, or null if not specified (Maven 4 inference)
    • artifactId

      public String artifactId()
      Returns the Maven artifactId.
      Returns:
      the artifactId (never null)
    • version

      public String version()
      Returns the artifact version.
      Returns:
      the version, or null if not specified
    • classifier

      public String classifier()
      Returns the artifact classifier.
      Returns:
      the classifier, or null if not specified
    • type

      public String type()
      Returns the dependency/artifact type (e.g., "jar", "pom").
      Returns:
      the type (defaults to "jar", never null)