gnodet commented on code in PR #11904:
URL: https://github.com/apache/maven/pull/11904#discussion_r4090484713
##########
impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelNormalizer.java:
##########
@@ -144,4 +154,92 @@ private <T> List<T> injectList(List<T> list,
UnaryOperator<T> modifer) {
}
return newList;
}
+
+ /**
+ * Expands the {@code id} attribute on a dependency into its component
fields.
+ * The id format is {@code groupId:artifactId:version}.
+ */
+ Dependency expandDependencyId(Dependency d) {
+ String id = d.getId();
+ if (id == null || id.isEmpty()) {
+ // No id attribute, but still expand exclusion ids
+ List<Exclusion> expanded = injectList(d.getExclusions(),
this::expandExclusionId);
+ return expanded != null ? d.withExclusions(expanded) : d;
+ }
+ String[] parts = id.split(":");
+ if (parts.length != 3) {
+ // Invalid format — will be caught by the validator
+ return d;
+ }
+ Dependency.Builder builder = Dependency.newBuilder(d);
+ if (isBlank(d.getGroupId())) {
+ builder.groupId(parts[0]);
+ }
+ if (isBlank(d.getArtifactId())) {
+ builder.artifactId(parts[1]);
+ }
+ if (isBlank(d.getVersion())) {
+ builder.version(parts[2]);
+ }
+ List<Exclusion> expanded = injectList(d.getExclusions(),
this::expandExclusionId);
+ if (expanded != null) {
+ builder.exclusions(expanded);
+ }
+ return builder.build();
+ }
+
+ /**
+ * Expands the {@code id} attribute on an exclusion into its component
fields.
+ * The id format is {@code groupId:artifactId}.
+ */
+ Exclusion expandExclusionId(Exclusion e) {
+ String id = e.getId();
+ if (id == null || id.isEmpty()) {
+ return e;
+ }
+ String[] parts = id.split(":");
+ if (parts.length != 2) {
+ // Invalid format — will be caught by the validator
+ return e;
+ }
+ Exclusion.Builder builder = Exclusion.newBuilder(e);
+ if (isBlank(e.getGroupId())) {
+ builder.groupId(parts[0]);
+ }
+ if (isBlank(e.getArtifactId())) {
+ builder.artifactId(parts[1]);
+ }
+ return builder.build();
+ }
+
+ /**
+ * Expands the {@code id} (XML attribute) / {@code gav} (Java field) on a
mixin
+ * into its component fields. The format is {@code
groupId:artifactId:version}.
+ */
+ Mixin expandMixinGav(Mixin m) {
+ String gav = m.getGav();
+ if (gav == null || gav.isEmpty()) {
+ return m;
+ }
+ String[] parts = gav.split(":");
+ if (parts.length != 3) {
+ // Invalid format — will be caught by the validator
+ return m;
+ }
+ Mixin.Builder builder = Mixin.newBuilder(m);
+ if (isBlank(m.getGroupId())) {
+ builder.groupId(parts[0]);
+ }
+ if (isBlank(m.getArtifactId())) {
+ builder.artifactId(parts[1]);
+ }
+ if (isBlank(m.getVersion())) {
+ builder.version(parts[2]);
+ }
+ return builder.build();
+ }
+
Review Comment:
Fixed in earlier commit — the method is now named `isNullOrEmpty` throughout.
##########
impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelValidator.java:
##########
@@ -1138,7 +1187,22 @@ private void validate20RawDependencies(
Map<String, Dependency> index = new HashMap<>();
for (Dependency dependency : dependencies) {
- String key = dependency.getManagementKey();
+ // When 'id' attribute is set, use it for the key since
groupId/artifactId
+ // have not yet been expanded (normalization runs after validation)
+ String key;
+ if (dependency.getId() != null && !dependency.getId().isEmpty()) {
+ key = dependency.getId();
+ } else {
+ key = dependency.getManagementKey();
Review Comment:
Acknowledged as intentional design — the validator prohibits mixing `id` and
explicit child elements (groupId/artifactId/version) on the same dependency, so
two deps with the same raw `id` but different scopes/classifiers would be
caught before reaching this dedup path. Low practical impact; noted in the
latest review.
##########
its/core-it-suite/src/test/java/org/apache/maven/it/MavenITgh11904DependencyIdAttributeTest.java:
##########
@@ -0,0 +1,91 @@
+/*
+ * 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.it;
+
+import java.io.Reader;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.List;
+import java.util.stream.Stream;
+
+import org.apache.maven.api.model.Model;
+import org.apache.maven.model.v4.MavenStaxReader;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * Integration test for <a
href="https://github.com/apache/maven/pull/11904">GH-11904</a>.
+ * Verifies that dependency {@code id} attributes are expanded into individual
+ * GAV elements in the consumer POM and that no {@code id} attribute remains.
+ */
+class MavenITgh11904DependencyIdAttributeTest extends
AbstractMavenIntegrationTestCase {
+
+ MavenITgh11904DependencyIdAttributeTest() {
+ super("[4.0.0-rc-3-SNAPSHOT,)");
Review Comment:
Fixed in `f585904284`: switched to no-arg constructor.
--
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]