Class Coordinates


  • public final class Coordinates
    extends java.lang.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 Summary

      Constructors 
      Constructor Description
      Coordinates​(java.lang.String groupId, java.lang.String artifactId, java.lang.String version, java.lang.String classifier, java.lang.String type)
      Note: groupId and version can be null to support Maven 4's inference mechanism.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.String artifactId()
      Returns the Maven artifactId.
      java.lang.String classifier()
      Returns the artifact classifier.
      static Coordinates fromPom​(java.nio.file.Path pomPath)
      Creates a POM Coordinates from a POM file by reading its GAV coordinates.
      java.lang.String groupId()
      Returns the Maven groupId.
      static Coordinates of​(java.lang.String groupId, java.lang.String artifactId, java.lang.String version)
      Creates a Coordinates with groupId, artifactId, and version (JAR type, no classifier).
      static Coordinates of​(java.lang.String groupId, java.lang.String artifactId, java.lang.String version, java.lang.String classifier, java.lang.String type)
      Creates a Coordinates with groupId, artifactId, version, classifier, and type.
      java.util.function.Predicate<eu.maveniverse.domtrip.Element> predicateGA()
      Creates a predicate that matches elements by GA (groupId:artifactId).
      java.util.function.Predicate<eu.maveniverse.domtrip.Element> predicateGATC()
      Creates a predicate that matches elements by GATC (groupId:artifactId:type[:classifier]).
      java.util.function.Predicate<eu.maveniverse.domtrip.Element> predicatePluginGA()
      Creates a predicate that matches plugin elements by GA (with default groupId handling).
      java.lang.String toFullString()
      Returns the full string representation: groupId:artifactId:type[:classifier]:version.
      java.lang.String toGA()
      Returns the groupId:artifactId string representation.
      java.lang.String toGATC()
      Returns the groupId:artifactId:type[:classifier] string representation.
      java.lang.String toGAV()
      Returns the groupId:artifactId:version string representation.
      java.lang.String type()
      Returns the dependency/artifact type (e.g., "jar", "pom").
      java.lang.String version()
      Returns the artifact version.
      Coordinates withType​(java.lang.String newType)
      Returns a new Coordinates with the same coordinates but different type.
      Coordinates withVersion​(java.lang.String newVersion)
      Returns a new Coordinates with the same coordinates but different version.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Coordinates

        public Coordinates​(java.lang.String groupId,
                           java.lang.String artifactId,
                           java.lang.String version,
                           java.lang.String classifier,
                           java.lang.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 Detail

      • of

        public static Coordinates of​(java.lang.String groupId,
                                     java.lang.String artifactId,
                                     java.lang.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​(java.lang.String groupId,
                                     java.lang.String artifactId,
                                     java.lang.String version,
                                     java.lang.String classifier,
                                     java.lang.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 java.lang.String toGA()
        Returns the groupId:artifactId string representation.
        Returns:
        GA string (e.g., "org.junit.jupiter:junit-jupiter")
      • toGAV

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

        public java.lang.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 java.lang.String toFullString()
        Returns the full string representation: groupId:artifactId:type[:classifier]:version.
        Returns:
        full coordinates string
      • withVersion

        public Coordinates withVersion​(java.lang.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​(java.lang.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 java.util.function.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 java.util.function.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 java.util.function.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​(java.nio.file.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 java.lang.String groupId()
        Returns the Maven groupId.
        Returns:
        the groupId, or null if not specified (Maven 4 inference)
      • artifactId

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

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

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

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