brunoborges commented on code in PR #1599:
URL: 
https://github.com/apache/maven-dependency-plugin/pull/1599#discussion_r3033895289


##########
src/main/java/org/apache/maven/plugins/dependency/pom/DependencyEntry.java:
##########
@@ -0,0 +1,197 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.plugins.dependency.pom;
+
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * Represents parsed Maven dependency coordinates (GAV + 
scope/type/classifier).
+ * Supports parsing from a colon-separated string in the format:
+ * {@code groupId:artifactId[:version[:scope[:type[:classifier]]]]}.
+ *
+ * @since 3.11.0
+ */
+public class DependencyCoordinates {
+
+    private static final Set<String> VALID_SCOPES =
+            new HashSet<>(Arrays.asList("compile", "provided", "runtime", 
"test", "system", "import"));
+
+    private String groupId;
+    private String artifactId;
+    private String version;
+    private String scope;
+    private String type;
+    private String classifier;
+    private Boolean optional;
+
+    public DependencyCoordinates() {}
+
+    public DependencyCoordinates(String groupId, String artifactId) {
+        this.groupId = groupId;
+        this.artifactId = artifactId;
+    }
+
+    /**
+     * Parses a colon-separated GAV string.
+     *
+     * @param gav the coordinate string in format {@code 
groupId:artifactId[:version[:scope[:type[:classifier]]]]}
+     * @return the parsed coordinates
+     * @throws IllegalArgumentException if the string has fewer than 2 or more 
than 6 segments
+     */
+    public static DependencyCoordinates parse(String gav) {
+        if (gav == null || gav.trim().isEmpty()) {
+            throw new IllegalArgumentException("GAV string must not be null or 
empty");
+        }
+        // Use split with -1 limit to preserve trailing empty segments for 
validation
+        String[] tokens = gav.split(":", -1);
+        if (tokens.length < 2 || tokens.length > 6) {
+            throw new IllegalArgumentException("Invalid GAV format: '" + gav
+                    + "'. Expected 
groupId:artifactId[:version[:scope[:type[:classifier]]]]");
+        }
+        DependencyCoordinates coords = new DependencyCoordinates();
+        coords.groupId = tokens[0].trim();
+        coords.artifactId = tokens[1].trim();
+        if (coords.groupId.isEmpty()) {
+            throw new IllegalArgumentException("Invalid GAV format: '" + gav + 
"'. groupId must not be empty.");
+        }
+        if (coords.artifactId.isEmpty()) {
+            throw new IllegalArgumentException("Invalid GAV format: '" + gav + 
"'. artifactId must not be empty.");
+        }
+        if (tokens.length >= 3 && !tokens[2].trim().isEmpty()) {
+            coords.version = tokens[2].trim();
+        }
+        if (tokens.length >= 4 && !tokens[3].trim().isEmpty()) {
+            coords.scope = tokens[3].trim();
+        }
+        if (tokens.length >= 5 && !tokens[4].trim().isEmpty()) {
+            coords.type = tokens[4].trim();
+        }
+        if (tokens.length >= 6 && !tokens[5].trim().isEmpty()) {
+            coords.classifier = tokens[5].trim();
+        }
+        return coords;
+    }
+
+    /**
+     * Validates that required fields are present.
+     *
+     * @throws IllegalArgumentException if groupId or artifactId is missing
+     */
+    public void validate() {

Review Comment:
   We decided to keep version out of `validate()`. The reason is that version 
requirements are context-dependent:
   
   - `dependency:remove` legitimately needs no version — it matches by `g:a` 
only
   - `dependency:add` to `<dependencyManagement>` requires version (fails 
explicitly)
   - `dependency:add` to `<dependencies>` can infer version from parent 
dependency management (fails if not found)
   
   Validating version in the value object would either break remove or require 
separate validation paths. The current approach validates version at the mojo 
layer where the context is known, and fails with clear error messages in all 
cases where version is missing and cannot be inferred.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to