Skip to content

Commit

Permalink
[MNG-8446] Project cannot start due to too many warnings (#1993)
Browse files Browse the repository at this point in the history
JIRA issue: [MNG-8446](https://issues.apache.org/jira/browse/MNG-8446)

This will help (but is not sufficient) to make [camel-quarkus](https://github.com/apache/camel-quarkus) buildable with maven 4.x.

---

https://issues.apache.org/jira/browse/MNG-8446
  • Loading branch information
gnodet authored Dec 17, 2024
1 parent a68b52f commit 8a69678
Show file tree
Hide file tree
Showing 6 changed files with 219 additions and 160 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -445,5 +445,13 @@ public final class Constants {
@Config(type = "java.time.Instant")
public static final String MAVEN_START_INSTANT = "maven.startInstant";

/**
* Max number of problems for each severity level retained by the model builder.
*
* @since 4.0.0
*/
@Config(type = "java.lang.Integer", defaultValue = "100")
public static final String MAVEN_BUILDER_MAX_PROBLEMS = "maven.builder.maxProblems";

private Constants() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,10 @@ public ModelBuilderSession newSession() {
return new ModelBuilderSessionImpl();
}

static int getMaxProblems(Session session) {
return Integer.parseInt(session.getUserProperties().getOrDefault(Constants.MAVEN_BUILDER_MAX_PROBLEMS, "100"));
}

protected class ModelBuilderSessionImpl implements ModelBuilderSession {
ModelBuilderSessionState mainSession;

Expand All @@ -223,7 +227,8 @@ public ModelBuilderResult build(ModelBuilderRequest request) throws ModelBuilder
mainSession = new ModelBuilderSessionState(request);
session = mainSession;
} else {
session = mainSession.derive(request, new DefaultModelBuilderResult());
session =
mainSession.derive(request, new DefaultModelBuilderResult(getMaxProblems(mainSession.session)));
}
// Build the request
if (request.getRequestType() == ModelBuilderRequest.RequestType.BUILD_PROJECT) {
Expand Down Expand Up @@ -259,7 +264,7 @@ protected class ModelBuilderSessionState implements ModelProblemCollector {
this(
request.getSession(),
request,
new DefaultModelBuilderResult(),
new DefaultModelBuilderResult(DefaultModelBuilder.getMaxProblems(request.getSession())),
request.getSession()
.getData()
.computeIfAbsent(SessionData.key(ModelCache.class), modelCacheFactory::newInstance),
Expand Down Expand Up @@ -300,8 +305,12 @@ private ModelBuilderSessionState(
this.result.setSource(this.request.getSource());
}

int getMaxProblems() {
return DefaultModelBuilder.getMaxProblems(session);
}

ModelBuilderSessionState derive(ModelSource source) {
return derive(source, new DefaultModelBuilderResult(result));
return derive(source, new DefaultModelBuilderResult(result, getMaxProblems()));
}

ModelBuilderSessionState derive(ModelSource source, DefaultModelBuilderResult result) {
Expand All @@ -312,7 +321,7 @@ ModelBuilderSessionState derive(ModelSource source, DefaultModelBuilderResult re
* Creates a new session, sharing cached datas and propagating errors.
*/
ModelBuilderSessionState derive(ModelBuilderRequest request) {
return derive(request, new DefaultModelBuilderResult(result));
return derive(request, new DefaultModelBuilderResult(result, getMaxProblems()));
}

ModelBuilderSessionState derive(ModelBuilderRequest request, DefaultModelBuilderResult result) {
Expand Down Expand Up @@ -711,7 +720,8 @@ Stream<DefaultModelBuilderResult> results(DefaultModelBuilderResult r) {

private void loadFromRoot(Path root, Path top) {
try (PhasingExecutor executor = createExecutor()) {
DefaultModelBuilderResult r = Objects.equals(top, root) ? result : new DefaultModelBuilderResult();
DefaultModelBuilderResult r =
Objects.equals(top, root) ? result : new DefaultModelBuilderResult(getMaxProblems());
loadFilePom(executor, top, root, Set.of(), r);
}
if (result.getFileModel() == null && !Objects.equals(top, root)) {
Expand Down Expand Up @@ -783,8 +793,9 @@ private void loadFilePom(
continue;
}

DefaultModelBuilderResult cr =
Objects.equals(top, subprojectFile) ? result : new DefaultModelBuilderResult(r);
DefaultModelBuilderResult cr = Objects.equals(top, subprojectFile)
? result
: new DefaultModelBuilderResult(r, getMaxProblems());
if (request.isRecursive()) {
r.getChildren().add(cr);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,18 @@
package org.apache.maven.internal.impl.model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.maven.api.model.Model;
import org.apache.maven.api.model.Profile;
import org.apache.maven.api.services.BuilderProblem;
import org.apache.maven.api.services.ModelBuilderResult;
import org.apache.maven.api.services.ModelProblem;
import org.apache.maven.api.services.ModelSource;
Expand All @@ -42,17 +46,21 @@ class DefaultModelBuilderResult implements ModelBuilderResult {
private Model effectiveModel;
private List<Profile> activePomProfiles;
private List<Profile> activeExternalProfiles;
private final List<ModelProblem> problems = new CopyOnWriteArrayList<>();
private final Queue<ModelProblem> problems = new ConcurrentLinkedQueue<>();
private final DefaultModelBuilderResult problemHolder;

private final List<DefaultModelBuilderResult> children = new ArrayList<>();

DefaultModelBuilderResult() {
this(null);
private int maxProblems;
private Map<BuilderProblem.Severity, AtomicInteger> problemCount = new ConcurrentHashMap<>();

DefaultModelBuilderResult(int maxProblems) {
this(null, maxProblems);
}

DefaultModelBuilderResult(DefaultModelBuilderResult problemHolder) {
DefaultModelBuilderResult(DefaultModelBuilderResult problemHolder, int maxProblems) {
this.problemHolder = problemHolder;
this.maxProblems = maxProblems;
}

public ModelSource getSource() {
Expand Down Expand Up @@ -125,7 +133,21 @@ public void setActiveExternalProfiles(List<Profile> activeProfiles) {
*/
@Override
public List<ModelProblem> getProblems() {
return Collections.unmodifiableList(problems);
List<ModelProblem> additionalProblems = new ArrayList<>();
problemCount.forEach((s, i) -> {
if (i.get() > maxProblems) {
additionalProblems.add(new DefaultModelProblem(
String.format("Too many problems %d of severity %s", i.get(), s.name()),
s,
ModelProblem.Version.BASE,
null,
-1,
-1,
null,
null));
}
});
return Stream.concat(problems.stream(), additionalProblems.stream()).toList();
}

/**
Expand All @@ -134,7 +156,12 @@ public List<ModelProblem> getProblems() {
* @param problem The problem to be added. It must be an instance of ModelProblem.
*/
public void addProblem(ModelProblem problem) {
problems.add(problem);
int problemCount = this.problemCount
.computeIfAbsent(problem.getSeverity(), s -> new AtomicInteger())
.incrementAndGet();
if (problemCount < maxProblems) {
problems.add(problem);
}
if (problemHolder != null) {
problemHolder.addProblem(problem);
}
Expand Down
Loading

0 comments on commit 8a69678

Please sign in to comment.