Skip to content

Commit 10e2fba

Browse files
authored
[MNG-8177] Add contextual info for model warnings (#1636)
As they really can come from anywhere. In case of this issue even from some eliminated POM that was read while collecting dirty tree, and was later eliminated. So confusing for users. --- https://issues.apache.org/jira/browse/MNG-8177
1 parent e6b5c81 commit 10e2fba

File tree

4 files changed

+202
-9
lines changed

4 files changed

+202
-9
lines changed

maven-api-impl/src/main/java/org/apache/maven/internal/impl/resolver/DefaultArtifactDescriptorReader.java

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.apache.maven.api.services.ModelSource;
4141
import org.apache.maven.internal.impl.InternalSession;
4242
import org.apache.maven.internal.impl.model.ModelProblemUtils;
43+
import org.codehaus.plexus.interpolation.util.StringUtils;
4344
import org.eclipse.aether.RepositoryEvent;
4445
import org.eclipse.aether.RepositoryEvent.EventType;
4546
import org.eclipse.aether.RepositoryException;
@@ -127,6 +128,7 @@ public ArtifactDescriptorResult readArtifactDescriptor(
127128
return result;
128129
}
129130

131+
@SuppressWarnings("MethodLength")
130132
private Model loadPom(
131133
RepositorySystemSession session, ArtifactDescriptorRequest request, ArtifactDescriptorResult result)
132134
throws ArtifactDescriptorException {
@@ -227,16 +229,29 @@ private Model loadPom(
227229
// that may lead to unexpected build failure, log them
228230
if (!modelResult.getProblems().isEmpty()) {
229231
List<ModelProblem> problems = modelResult.getProblems();
230-
logger.warn(
231-
"{} {} encountered while building the effective model for {}",
232-
problems.size(),
233-
(problems.size() == 1) ? "problem was" : "problems were",
234-
request.getArtifact());
235232
if (logger.isDebugEnabled()) {
236-
for (ModelProblem problem : problems) {
237-
logger.warn(
238-
"{} @ {}", problem.getMessage(), ModelProblemUtils.formatLocation(problem, null));
233+
String problem = (problems.size() == 1) ? "problem" : "problems";
234+
String problemPredicate = problem + ((problems.size() == 1) ? " was" : " were");
235+
String message = String.format(
236+
"%s %s encountered while building the effective model for %s during %s\n",
237+
problems.size(),
238+
problemPredicate,
239+
request.getArtifact(),
240+
RequestTraceHelper.interpretTrace(true, request.getTrace()));
241+
message += StringUtils.capitalizeFirstLetter(problem);
242+
for (ModelProblem modelProblem : problems) {
243+
message += String.format(
244+
"\n* %s @ %s",
245+
modelProblem.getMessage(), ModelProblemUtils.formatLocation(modelProblem, null));
239246
}
247+
logger.warn(message);
248+
} else {
249+
logger.warn(
250+
"{} {} encountered while building the effective model for {} during {} (use -X to see details)",
251+
problems.size(),
252+
(problems.size() == 1) ? "problem was" : "problems were",
253+
request.getArtifact(),
254+
RequestTraceHelper.interpretTrace(false, request.getTrace()));
240255
}
241256
}
242257
model = modelResult.getEffectiveModel();
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.maven.internal.impl.resolver;
20+
21+
import java.util.stream.Collectors;
22+
23+
import org.eclipse.aether.RequestTrace;
24+
import org.eclipse.aether.collection.CollectRequest;
25+
import org.eclipse.aether.collection.CollectStepData;
26+
import org.eclipse.aether.resolution.ArtifactDescriptorRequest;
27+
import org.eclipse.aether.resolution.ArtifactRequest;
28+
import org.eclipse.aether.resolution.DependencyRequest;
29+
30+
/**
31+
* Helper class to manage {@link RequestTrace} for better error logging.
32+
*/
33+
public final class RequestTraceHelper {
34+
35+
/**
36+
* Method that creates some informational string based on passed in {@link RequestTrace}. The contents of request
37+
* trace can literally be anything, but this class tries to cover "most common" cases that are happening in Maven.
38+
*/
39+
public static String interpretTrace(boolean detailed, RequestTrace requestTrace) {
40+
while (requestTrace != null) {
41+
Object data = requestTrace.getData();
42+
if (data instanceof DependencyRequest request) {
43+
return "dependency resolution for " + request;
44+
} else if (data instanceof CollectRequest request) {
45+
return "dependency collection for " + request;
46+
} else if (data instanceof CollectStepData stepData) {
47+
String msg = "dependency collection step for " + stepData.getContext();
48+
if (detailed) {
49+
msg += ". Path to offending node from root:\n";
50+
msg += stepData.getPath().stream()
51+
.map(n -> " -> " + n.toString())
52+
.collect(Collectors.joining("\n"));
53+
msg += "\n => " + stepData.getNode();
54+
}
55+
return msg;
56+
} else if (data instanceof ArtifactDescriptorRequest request) {
57+
return "artifact descriptor request for " + request.getArtifact();
58+
} else if (data instanceof ArtifactRequest request) {
59+
return "artifact request for " + request.getArtifact();
60+
// TODO: this class is not reachable here!
61+
// } else if (data instanceof org.apache.maven.model.Plugin plugin) {
62+
// return "plugin " + plugin.getId();
63+
}
64+
requestTrace = requestTrace.getParent();
65+
}
66+
67+
return "n/a";
68+
}
69+
}

maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultArtifactDescriptorReader.java

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import javax.inject.Singleton;
2424

2525
import java.util.LinkedHashSet;
26+
import java.util.List;
2627
import java.util.Map;
2728
import java.util.Objects;
2829
import java.util.Properties;
@@ -33,8 +34,11 @@
3334
import org.apache.maven.model.building.ModelBuilder;
3435
import org.apache.maven.model.building.ModelBuildingException;
3536
import org.apache.maven.model.building.ModelBuildingRequest;
37+
import org.apache.maven.model.building.ModelBuildingResult;
3638
import org.apache.maven.model.building.ModelProblem;
39+
import org.apache.maven.model.building.ModelProblemUtils;
3740
import org.apache.maven.model.resolution.UnresolvableModelException;
41+
import org.codehaus.plexus.util.StringUtils;
3842
import org.eclipse.aether.RepositoryEvent;
3943
import org.eclipse.aether.RepositoryEvent.EventType;
4044
import org.eclipse.aether.RepositoryException;
@@ -61,6 +65,8 @@
6165
import org.eclipse.aether.resolution.VersionResolutionException;
6266
import org.eclipse.aether.resolution.VersionResult;
6367
import org.eclipse.aether.transfer.ArtifactNotFoundException;
68+
import org.slf4j.Logger;
69+
import org.slf4j.LoggerFactory;
6470

6571
/**
6672
* Default artifact descriptor reader.
@@ -77,6 +83,7 @@ public class DefaultArtifactDescriptorReader implements ArtifactDescriptorReader
7783
private final ModelCacheFactory modelCacheFactory;
7884
private final Map<String, MavenArtifactRelocationSource> artifactRelocationSources;
7985
private final ArtifactDescriptorReaderDelegate delegate;
86+
private final Logger logger = LoggerFactory.getLogger(getClass());
8087

8188
@Inject
8289
@SuppressWarnings("checkstyle:ParameterNumber")
@@ -124,6 +131,7 @@ public ArtifactDescriptorResult readArtifactDescriptor(
124131
return result;
125132
}
126133

134+
@SuppressWarnings("MethodLength")
127135
private Model loadPom(
128136
RepositorySystemSession session, ArtifactDescriptorRequest request, ArtifactDescriptorResult result)
129137
throws ArtifactDescriptorException {
@@ -221,7 +229,37 @@ private Model loadPom(
221229
pomArtifact.getVersion()));
222230
}
223231

224-
model = modelBuilder.build(modelRequest).getEffectiveModel();
232+
ModelBuildingResult modelResult = modelBuilder.build(modelRequest);
233+
// ModelBuildingEx is thrown only on FATAL and ERROR severities, but we still can have WARNs
234+
// that may lead to unexpected build failure, log them
235+
if (!modelResult.getProblems().isEmpty()) {
236+
List<ModelProblem> problems = modelResult.getProblems();
237+
if (logger.isDebugEnabled()) {
238+
String problem = (problems.size() == 1) ? "problem" : "problems";
239+
String problemPredicate = problem + ((problems.size() == 1) ? " was" : " were");
240+
String message = String.format(
241+
"%s %s encountered while building the effective model for %s during %s\n",
242+
problems.size(),
243+
problemPredicate,
244+
request.getArtifact(),
245+
RequestTraceHelper.interpretTrace(true, request.getTrace()));
246+
message += StringUtils.capitalizeFirstLetter(problem);
247+
for (ModelProblem modelProblem : problems) {
248+
message += String.format(
249+
"\n* %s @ %s",
250+
modelProblem.getMessage(), ModelProblemUtils.formatLocation(modelProblem, null));
251+
}
252+
logger.warn(message);
253+
} else {
254+
logger.warn(
255+
"{} {} encountered while building the effective model for {} during {} (use -X to see details)",
256+
problems.size(),
257+
(problems.size() == 1) ? "problem was" : "problems were",
258+
request.getArtifact(),
259+
RequestTraceHelper.interpretTrace(false, request.getTrace()));
260+
}
261+
}
262+
model = modelResult.getEffectiveModel();
225263
} catch (ModelBuildingException e) {
226264
for (ModelProblem problem : e.getProblems()) {
227265
if (problem.getException() instanceof UnresolvableModelException) {
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.maven.repository.internal;
20+
21+
import java.util.stream.Collectors;
22+
23+
import org.apache.maven.model.Plugin;
24+
import org.eclipse.aether.RequestTrace;
25+
import org.eclipse.aether.collection.CollectRequest;
26+
import org.eclipse.aether.collection.CollectStepData;
27+
import org.eclipse.aether.resolution.ArtifactDescriptorRequest;
28+
import org.eclipse.aether.resolution.ArtifactRequest;
29+
import org.eclipse.aether.resolution.DependencyRequest;
30+
31+
/**
32+
* Helper class to manage {@link RequestTrace} for better error logging.
33+
*
34+
* @since 3.9.9
35+
*/
36+
public final class RequestTraceHelper {
37+
38+
/**
39+
* Method that creates some informational string based on passed in {@link RequestTrace}. The contents of request
40+
* trace can literally be anything, but this class tries to cover "most common" cases that are happening in Maven.
41+
*/
42+
public static String interpretTrace(boolean detailed, RequestTrace requestTrace) {
43+
while (requestTrace != null) {
44+
Object data = requestTrace.getData();
45+
if (data instanceof DependencyRequest request) {
46+
return "dependency resolution for " + request;
47+
} else if (data instanceof CollectRequest request) {
48+
return "dependency collection for " + request;
49+
} else if (data instanceof CollectStepData stepData) {
50+
String msg = "dependency collection step for " + stepData.getContext();
51+
if (detailed) {
52+
msg += ". Path to offending node from root:\n";
53+
msg += stepData.getPath().stream()
54+
.map(n -> " -> " + n.toString())
55+
.collect(Collectors.joining("\n"));
56+
msg += "\n => " + stepData.getNode();
57+
}
58+
return msg;
59+
} else if (data instanceof ArtifactDescriptorRequest request) {
60+
return "artifact descriptor request for " + request.getArtifact();
61+
} else if (data instanceof ArtifactRequest request) {
62+
return "artifact request for " + request.getArtifact();
63+
} else if (data instanceof Plugin plugin) {
64+
return "plugin request " + plugin.getId();
65+
}
66+
requestTrace = requestTrace.getParent();
67+
}
68+
69+
return "n/a";
70+
}
71+
}

0 commit comments

Comments
 (0)