Skip to content

Commit 9b20cb4

Browse files
authored
Improve error message for unresolved expressions (apache#11615)
* Improve error message for unresolved expressions Signed-off-by: 高春晖 <18220699480@163.com> * Fix Spotless formatting violations Adjust if statement formatting to comply with project code style: - Split OR conditions onto separate lines with proper indentation - Ensures consistency with Spotless formatting rules * Trigger CI re-run * Trigger CI re-run (flaky test) Signed-off-by: 高春晖 <18220699480@163.com> --------- Signed-off-by: 高春晖 <18220699480@163.com>
1 parent e6fb273 commit 9b20cb4

File tree

1 file changed

+59
-1
lines changed

1 file changed

+59
-1
lines changed

impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultDependencyResolver.java

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,8 @@ public DependencyResolverResult collect(@Nonnull DependencyResolverRequest reque
174174
session.getNode(result.getRoot(), request.getVerbose()),
175175
0);
176176
} catch (DependencyCollectionException e) {
177-
throw new DependencyResolverException("Unable to collect dependencies", e);
177+
String enhancedMessage = enhanceCollectionError(e, collectRequest);
178+
throw new DependencyResolverException(enhancedMessage, e);
178179
}
179180
} finally {
180181
RequestTraceHelper.exit(trace);
@@ -274,4 +275,61 @@ public DependencyResolverResult resolve(DependencyResolverRequest request)
274275
private static DependencyResolverException cannotReadModuleInfo(final Path path, final IOException cause) {
275276
return new DependencyResolverException("Cannot read module information of " + path, cause);
276277
}
278+
279+
private static boolean containsUnresolvedExpression(String value) {
280+
return value != null && value.contains("${") && value.contains("}");
281+
}
282+
283+
private static String enhanceCollectionError(DependencyCollectionException e, CollectRequest request) {
284+
if (e.getMessage() != null && e.getMessage().contains("Invalid Collect Request")) {
285+
StringBuilder enhanced = new StringBuilder();
286+
enhanced.append("Failed to collect dependencies");
287+
288+
org.eclipse.aether.graph.Dependency root = request.getRoot();
289+
if (root != null && root.getArtifact() != null) {
290+
org.eclipse.aether.artifact.Artifact artifact = root.getArtifact();
291+
String groupId = artifact.getGroupId();
292+
String artifactId = artifact.getArtifactId();
293+
String version = artifact.getVersion();
294+
295+
if (containsUnresolvedExpression(groupId)
296+
|| containsUnresolvedExpression(artifactId)
297+
|| containsUnresolvedExpression(version)) {
298+
enhanced.append(" due to unresolved expression(s) in dependency: ")
299+
.append(groupId)
300+
.append(":")
301+
.append(artifactId)
302+
.append(":")
303+
.append(version)
304+
.append(".\n")
305+
.append("Please check that all properties are defined in your POM or settings.xml.");
306+
return enhanced.toString();
307+
}
308+
}
309+
310+
for (org.eclipse.aether.graph.Dependency dep : request.getDependencies()) {
311+
if (dep != null && dep.getArtifact() != null) {
312+
org.eclipse.aether.artifact.Artifact artifact = dep.getArtifact();
313+
String groupId = artifact.getGroupId();
314+
String artifactId = artifact.getArtifactId();
315+
String version = artifact.getVersion();
316+
317+
if (containsUnresolvedExpression(groupId)
318+
|| containsUnresolvedExpression(artifactId)
319+
|| containsUnresolvedExpression(version)) {
320+
enhanced.append(" due to unresolved expression(s) in dependency: ")
321+
.append(groupId)
322+
.append(":")
323+
.append(artifactId)
324+
.append(":")
325+
.append(version)
326+
.append(".\n")
327+
.append("Please check that all properties are defined in your POM or settings.xml.");
328+
return enhanced.toString();
329+
}
330+
}
331+
}
332+
}
333+
return e.getMessage();
334+
}
277335
}

0 commit comments

Comments
 (0)