This is an automated email from the ASF dual-hosted git repository.

slawekjaranowski pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-enforcer.git


The following commit(s) were added to refs/heads/master by this push:
     new 2b75dd32 Improve RequireUpperBoundDeps error message (#1059)
2b75dd32 is described below

commit 2b75dd32499dc971ca990ba5b62d564f3709122b
Author: Sambuddha-Jana <[email protected]>
AuthorDate: Tue Oct 27 02:41:14 2026 +0530

    Improve RequireUpperBoundDeps error message (#1059)
    
    * Improved RequireUpperBoundDeps error message
    
    * Extract dependencyManagement suggestion helper and handle classifier
    
    * Update integration test expectation for requireUpperBoundDeps message
---
 .../rules/dependency/RequireUpperBoundDeps.java    | 68 ++++++++++++++++++++++
 .../dependency/RequireUpperBoundDepsTest.java      | 11 +++-
 .../verify.groovy                                  | 67 ++++++++++++---------
 3 files changed, 116 insertions(+), 30 deletions(-)

diff --git 
a/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/dependency/RequireUpperBoundDeps.java
 
b/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/dependency/RequireUpperBoundDeps.java
index f45b1298..93d43a46 100644
--- 
a/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/dependency/RequireUpperBoundDeps.java
+++ 
b/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/dependency/RequireUpperBoundDeps.java
@@ -150,9 +150,77 @@ public final class RequireUpperBoundDeps extends 
AbstractStandardEnforcerRule {
             errorMessage.append(buildTreeString(node));
         }
 
+        appendDependencyManagementSuggestion(errorMessage, conflict);
+
         return errorMessage.toString();
     }
 
+    private void appendDependencyManagementSuggestion(StringBuilder 
errorMessage, List<DependencyNode> conflict) {
+        if (conflict == null || conflict.isEmpty()) {
+            return;
+        }
+
+        org.eclipse.aether.artifact.Artifact artifact = 
conflict.get(0).getArtifact();
+        ArtifactVersion highestVersion = null;
+
+        for (DependencyNode node : conflict) {
+            String versionStr = 
DependencyManagerUtils.getPremanagedVersion(node);
+            if (versionStr == null) {
+                org.eclipse.aether.artifact.Artifact nodeArtifact = 
node.getArtifact();
+                versionStr = uniqueVersions ? nodeArtifact.getVersion() : 
nodeArtifact.getBaseVersion();
+            }
+
+            ArtifactVersion version = new DefaultArtifactVersion(versionStr);
+            if (highestVersion == null || version.compareTo(highestVersion) > 
0) {
+                highestVersion = version;
+            }
+        }
+
+        if (highestVersion == null) {
+            return;
+        }
+
+        errorMessage
+                .append(System.lineSeparator())
+                .append("To fix this, add the following to your 
<dependencyManagement> section:")
+                .append(System.lineSeparator())
+                .append("<dependencyManagement>")
+                .append(System.lineSeparator())
+                .append("  <dependencies>")
+                .append(System.lineSeparator())
+                .append("    <dependency>")
+                .append(System.lineSeparator())
+                .append("      <groupId>")
+                .append(artifact.getGroupId())
+                .append("</groupId>")
+                .append(System.lineSeparator())
+                .append("      <artifactId>")
+                .append(artifact.getArtifactId())
+                .append("</artifactId>")
+                .append(System.lineSeparator())
+                .append("      <version>")
+                .append(highestVersion)
+                .append("</version>")
+                .append(System.lineSeparator());
+
+        String classifier = artifact.getClassifier();
+        if (classifier != null && !classifier.isEmpty()) {
+            errorMessage
+                    .append("      <classifier>")
+                    .append(classifier)
+                    .append("</classifier>")
+                    .append(System.lineSeparator());
+        }
+
+        errorMessage
+                .append("    </dependency>")
+                .append(System.lineSeparator())
+                .append("  </dependencies>")
+                .append(System.lineSeparator())
+                .append("</dependencyManagement>")
+                .append(System.lineSeparator());
+    }
+
     private StringBuilder buildTreeString(DependencyNode node) {
         List<String> loc = new ArrayList<>();
         DependencyNode currentNode = node;
diff --git 
a/enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/dependency/RequireUpperBoundDepsTest.java
 
b/enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/dependency/RequireUpperBoundDepsTest.java
index 4fd47313..e66427bf 100644
--- 
a/enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/dependency/RequireUpperBoundDepsTest.java
+++ 
b/enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/dependency/RequireUpperBoundDepsTest.java
@@ -62,7 +62,11 @@ class RequireUpperBoundDepsTest {
         assertThatCode(rule::execute)
                 .isInstanceOf(EnforcerRuleException.class)
                 .hasMessageContaining("default-group:childA:1.0.0:classifier")
-                .hasMessageContaining("default-group:childA:2.0.0:classifier");
+                .hasMessageContaining("default-group:childA:2.0.0:classifier")
+                .hasMessageContaining("<groupId>default-group</groupId>")
+                .hasMessageContaining("<artifactId>childA</artifactId>")
+                .hasMessageContaining("<version>2.0.0</version>")
+                .hasMessageContaining("<classifier>classifier</classifier>");
     }
 
     @Test
@@ -82,6 +86,9 @@ class RequireUpperBoundDepsTest {
 
         assertThatCode(rule::execute)
                 .isInstanceOf(EnforcerRuleException.class)
-                .hasMessageContaining("default-group:childA:1.0.0:classifier 
(version managed from 2.0.0)");
+                .hasMessageContaining("default-group:childA:1.0.0:classifier 
(version managed from 2.0.0)")
+                .hasMessageContaining("<groupId>default-group</groupId>")
+                .hasMessageContaining("<artifactId>childA</artifactId>")
+                .hasMessageContaining("<version>2.0.0</version>");
     }
 }
diff --git 
a/maven-enforcer-plugin/src/it/projects/require-upper-bound-dependencies-managed_failure/verify.groovy
 
b/maven-enforcer-plugin/src/it/projects/require-upper-bound-dependencies-managed_failure/verify.groovy
index 1b8a0056..9f37ff52 100644
--- 
a/maven-enforcer-plugin/src/it/projects/require-upper-bound-dependencies-managed_failure/verify.groovy
+++ 
b/maven-enforcer-plugin/src/it/projects/require-upper-bound-dependencies-managed_failure/verify.groovy
@@ -1,29 +1,40 @@
-/*
- * 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.
- */
-def LS = System.getProperty( "line.separator" )
-File buildLog = new File( basedir, 'build.log' )
-assert buildLog.text.contains( 'Rule 0: 
org.apache.maven.enforcer.rules.dependency.RequireUpperBoundDeps failed with 
message:' )
-
-def message = 'Failed while enforcing RequireUpperBoundDeps. The error(s) are 
[' + LS+
-'Require upper bound dependencies error for 
org.apache.maven.plugins.enforcer.its:menforcer146-x:1.1. Paths to dependency 
are:' + LS+
-'+-org.apache.maven.enforcer.its:menforcer146-a:1.0-SNAPSHOT' + LS+
-'  +-org.apache.maven.plugins.enforcer.its:menforcer146-b:1.0' + LS+
-'    +-org.apache.maven.plugins.enforcer.its:menforcer146-x:1.1 (version 
managed from 2.1)' + LS+
-']' + LS
+/*
+ * 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.
+ */
+def LS = System.getProperty( "line.separator" )
+File buildLog = new File( basedir, 'build.log' )
+assert buildLog.text.contains( 'Rule 0: 
org.apache.maven.enforcer.rules.dependency.RequireUpperBoundDeps failed with 
message:' )
+
+def message = 'Failed while enforcing RequireUpperBoundDeps. The error(s) are 
[' + LS+
+'Require upper bound dependencies error for 
org.apache.maven.plugins.enforcer.its:menforcer146-x:1.1. Paths to dependency 
are:' + LS+
+'+-org.apache.maven.enforcer.its:menforcer146-a:1.0-SNAPSHOT' + LS+
+'  +-org.apache.maven.plugins.enforcer.its:menforcer146-b:1.0' + LS+
+'    +-org.apache.maven.plugins.enforcer.its:menforcer146-x:1.1 (version 
managed from 2.1)' + LS+
+LS+
+'To fix this, add the following to your <dependencyManagement> section:' + LS+
+'<dependencyManagement>' + LS+
+'  <dependencies>' + LS+
+'    <dependency>' + LS+
+'      <groupId>org.apache.maven.plugins.enforcer.its</groupId>' + LS+
+'      <artifactId>menforcer146-x</artifactId>' + LS+
+'      <version>2.1</version>' + LS+
+'    </dependency>' + LS+
+'  </dependencies>' + LS+
+'</dependencyManagement>' + LS+
+']' + LS
 assert buildLog.text.contains( message )
\ No newline at end of file

Reply via email to