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

gnodet pushed a commit to branch maven-4.0.x
in repository https://gitbox.apache.org/repos/asf/maven.git


The following commit(s) were added to refs/heads/maven-4.0.x by this push:
     new 70ce2a685a fix: emit reactor-aware hint for 
ArtifactFilteredOutException (#13792)
70ce2a685a is described below

commit 70ce2a685a43b594ce8dcbc91c8ef039e1bad1ca
Author: Sylwester Lachiewicz <[email protected]>
AuthorDate: Fri Nov 6 08:40:05 2026 +0100

    fix: emit reactor-aware hint for ArtifactFilteredOutException (#13792)
---
 .../maven/cling/invoker/mvn/MavenInvoker.java      |  70 ++++++++
 .../cling/invoker/mvn/FilteredOutHintTest.java     | 179 +++++++++++++++++++++
 .../main/java/org/apache/maven/DefaultMaven.java   |   1 +
 .../maven/exception/DefaultExceptionHandler.java   |  21 ++-
 .../execution/DefaultMavenExecutionResult.java     |  13 ++
 .../maven/execution/MavenExecutionResult.java      |  15 ++
 .../exception/DefaultExceptionHandlerTest.java     |   2 +
 7 files changed, 295 insertions(+), 6 deletions(-)

diff --git 
a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvn/MavenInvoker.java
 
b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvn/MavenInvoker.java
index d1655ab41d..001d1a506b 100644
--- 
a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvn/MavenInvoker.java
+++ 
b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvn/MavenInvoker.java
@@ -22,10 +22,13 @@
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.IdentityHashMap;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Optional;
+import java.util.Set;
 import java.util.function.Consumer;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
@@ -72,6 +75,8 @@
 import org.apache.maven.project.MavenProject;
 import org.apache.maven.toolchain.model.ToolchainModel;
 import org.eclipse.aether.DefaultRepositoryCache;
+import org.eclipse.aether.artifact.Artifact;
+import org.eclipse.aether.transfer.ArtifactFilteredOutException;
 import org.eclipse.aether.transfer.TransferListener;
 
 import static java.util.Comparator.comparing;
@@ -472,6 +477,7 @@ protected int doExecute(MavenContext context, 
MavenExecutionRequest request) thr
             Map<String, String> references = new LinkedHashMap<>();
             List<MavenProject> failedProjects = new ArrayList<>();
 
+            Set<String> hintedArtifacts = new HashSet<>();
             for (Throwable exception : result.getExceptions()) {
                 ExceptionSummary summary = handler.handleException(exception);
                 logSummary(context, summary, references, "");
@@ -479,6 +485,8 @@ protected int doExecute(MavenContext context, 
MavenExecutionRequest request) thr
                 if (exception instanceof LifecycleExecutionException 
lifecycleExecutionException) {
                     
failedProjects.add(lifecycleExecutionException.getProject());
                 }
+
+                logFilteredOutHint(context, exception, result, 
hintedArtifacts);
             }
 
             context.logger.error("");
@@ -528,6 +536,68 @@ protected int doExecute(MavenContext context, 
MavenExecutionRequest request) thr
         }
     }
 
+    /**
+     * When an {@link ArtifactFilteredOutException} is found in the exception 
chain, log a reactor-aware hint
+     * that precisely identifies whether the artifact is an unselected project 
or an already-selected project.
+     * External (non-reactor) artifacts are handled by {@link 
org.apache.maven.exception.DefaultExceptionHandler}.
+     */
+    protected void logFilteredOutHint(
+            MavenContext context, Throwable exception, MavenExecutionResult 
result, Set<String> alreadyHinted) {
+        ArtifactFilteredOutException filteredOut = findCause(exception, 
ArtifactFilteredOutException.class);
+        if (filteredOut == null) {
+            return;
+        }
+
+        Artifact artifact = filteredOut.getArtifact();
+        if (artifact == null) {
+            return;
+        }
+
+        String groupId = artifact.getGroupId();
+        String artifactId = artifact.getArtifactId();
+        String version = artifact.getVersion();
+        String coords = groupId + ":" + artifactId + ":" + version;
+
+        if (!alreadyHinted.add(coords)) {
+            return;
+        }
+
+        List<MavenProject> allProjects = result.getAllProjects();
+        List<MavenProject> selectedProjects = 
result.getTopologicallySortedProjects();
+
+        boolean inAll = allProjects.stream()
+                .anyMatch(p -> p.getGroupId().equals(groupId)
+                        && p.getArtifactId().equals(artifactId)
+                        && p.getVersion().equals(version));
+        boolean inSelected = selectedProjects.stream()
+                .anyMatch(p -> p.getGroupId().equals(groupId)
+                        && p.getArtifactId().equals(artifactId)
+                        && p.getVersion().equals(version));
+
+        if (inAll && !inSelected) {
+            context.logger.error("");
+            context.logger.error(coords
+                    + " is a project of this build but was not selected."
+                    + " Add -am to also build its dependencies, or run the 
full reactor.");
+        } else if (inAll) {
+            context.logger.error("");
+            context.logger.error(coords
+                    + " is part of this build but has not been built yet."
+                    + " Build it first or reorder the reactor.");
+        }
+        // external artifact: DefaultExceptionHandler already emits the 
repo-filter hint
+    }
+
+    private static <T extends Throwable> T findCause(Throwable exception, 
Class<T> type) {
+        IdentityHashMap<Throwable, Boolean> seen = new IdentityHashMap<>();
+        for (Throwable t = exception; t != null && seen.put(t, Boolean.TRUE) 
== null; t = t.getCause()) {
+            if (type.isInstance(t)) {
+                return type.cast(t);
+            }
+        }
+        return null;
+    }
+
     protected void logBuildResumeHint(MavenContext context, String 
resumeBuildHint) {
         context.logger.error("");
         context.logger.error("After correcting the problems, you can resume 
the build with the command");
diff --git 
a/impl/maven-cli/src/test/java/org/apache/maven/cling/invoker/mvn/FilteredOutHintTest.java
 
b/impl/maven-cli/src/test/java/org/apache/maven/cling/invoker/mvn/FilteredOutHintTest.java
new file mode 100644
index 0000000000..32a8beead3
--- /dev/null
+++ 
b/impl/maven-cli/src/test/java/org/apache/maven/cling/invoker/mvn/FilteredOutHintTest.java
@@ -0,0 +1,179 @@
+/*
+ * 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.cling.invoker.mvn;
+
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import org.apache.maven.api.cli.Logger;
+import org.apache.maven.execution.DefaultMavenExecutionResult;
+import org.apache.maven.execution.MavenExecutionResult;
+import org.apache.maven.project.MavenProject;
+import org.eclipse.aether.artifact.DefaultArtifact;
+import org.eclipse.aether.repository.RemoteRepository;
+import org.eclipse.aether.resolution.ArtifactResolutionException;
+import org.eclipse.aether.resolution.ArtifactResult;
+import org.eclipse.aether.transfer.ArtifactFilteredOutException;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
+
+/**
+ * Unit tests for {@link MavenInvoker#logFilteredOutHint(MavenContext, 
Throwable, MavenExecutionResult, Set)}.
+ */
+class FilteredOutHintTest {
+
+    /** Simple Logger that accumulates error-level messages for assertion. */
+    private final List<String> errorLog = new ArrayList<>();
+
+    private final Logger logger = new Logger() {
+        @Override
+        public void log(Logger.Level level, String message, Throwable error) {
+            if (level == Logger.Level.ERROR) {
+                errorLog.add(message);
+            }
+        }
+    };
+
+    /** Subclass that exposes the protected method without requiring a real 
Lookup. */
+    static class TestableInvoker extends MavenInvoker {
+        TestableInvoker() {
+            super(mock(org.apache.maven.api.services.Lookup.class), null);
+        }
+    }
+
+    private MavenContext context;
+    private TestableInvoker invoker;
+
+    @BeforeEach
+    void setUp() {
+        context = mock(MavenContext.class);
+        context.logger = logger;
+        invoker = new TestableInvoker();
+    }
+
+    private ArtifactResolutionException makeException(String groupId, String 
artifactId, String version) {
+        RemoteRepository repo =
+                new RemoteRepository.Builder("central", "default", 
"https://repo.maven.apache.org/maven2";).build();
+        ArtifactFilteredOutException filterEx = new 
ArtifactFilteredOutException(
+                new DefaultArtifact(groupId + ":" + artifactId + ":jar:" + 
version), repo, "Prefix NOT allowed");
+        ArtifactResult result = new ArtifactResult(new 
org.eclipse.aether.resolution.ArtifactRequest(
+                new DefaultArtifact(groupId + ":" + artifactId + ":jar:" + 
version), List.of(repo), null));
+        result.addException(filterEx);
+        return new ArtifactResolutionException(List.of(result), "Could not 
resolve artifact");
+    }
+
+    private MavenProject makeProject(String groupId, String artifactId, String 
version) {
+        MavenProject project = new MavenProject();
+        project.setGroupId(groupId);
+        project.setArtifactId(artifactId);
+        project.setVersion(version);
+        return project;
+    }
+
+    @Test
+    void noHintWhenNoFilteredException() {
+        MavenExecutionResult result = new DefaultMavenExecutionResult();
+        invoker.logFilteredOutHint(context, new RuntimeException("unrelated"), 
result, new HashSet<>());
+        assertTrue(errorLog.isEmpty(), "No hint should be logged for unrelated 
exceptions");
+    }
+
+    @Test
+    void hintForUnselectedReactorProject() {
+        // lib is in all-projects but NOT in selected projects
+        MavenProject lib = makeProject("com.example", "lib", "1.0-SNAPSHOT");
+        MavenProject app = makeProject("com.example", "app", "1.0-SNAPSHOT");
+
+        MavenExecutionResult result = new DefaultMavenExecutionResult();
+        result.setAllProjects(List.of(lib, app));
+        result.setTopologicallySortedProjects(List.of(app)); // only app 
selected via -pl
+
+        ArtifactResolutionException exception = makeException("com.example", 
"lib", "1.0-SNAPSHOT");
+
+        invoker.logFilteredOutHint(context, exception, result, new 
HashSet<>());
+
+        String combined = String.join(" ", errorLog);
+        assertTrue(combined.contains("com.example:lib:1.0-SNAPSHOT"), "Should 
name the artifact");
+        assertTrue(combined.contains("was not selected"), "Should indicate the 
project was not selected");
+        assertTrue(combined.contains("-am"), "Should suggest -am");
+    }
+
+    @Test
+    void hintForSelectedButUnbuiltReactorProject() {
+        // lib is in both all-projects AND selected projects (but filtered out 
— ordering issue)
+        MavenProject lib = makeProject("com.example", "lib", "1.0-SNAPSHOT");
+        MavenProject app = makeProject("com.example", "app", "1.0-SNAPSHOT");
+
+        MavenExecutionResult result = new DefaultMavenExecutionResult();
+        result.setAllProjects(List.of(lib, app));
+        result.setTopologicallySortedProjects(List.of(lib, app)); // both 
selected
+
+        ArtifactResolutionException exception = makeException("com.example", 
"lib", "1.0-SNAPSHOT");
+
+        invoker.logFilteredOutHint(context, exception, result, new 
HashSet<>());
+
+        String combined = String.join(" ", errorLog);
+        assertTrue(combined.contains("com.example:lib:1.0-SNAPSHOT"), "Should 
name the artifact");
+        assertTrue(combined.contains("has not been built yet"), "Should 
indicate not yet built");
+    }
+
+    @Test
+    void noHintForExternalArtifact() {
+        // External artifacts are handled by DefaultExceptionHandler — 
MavenInvoker should stay silent
+        MavenProject app = makeProject("com.example", "app", "1.0-SNAPSHOT");
+
+        MavenExecutionResult result = new DefaultMavenExecutionResult();
+        result.setAllProjects(List.of(app));
+        result.setTopologicallySortedProjects(List.of(app));
+
+        ArtifactResolutionException exception = makeException("org.external", 
"library", "2.0");
+
+        invoker.logFilteredOutHint(context, exception, result, new 
HashSet<>());
+
+        assertTrue(
+                errorLog.isEmpty(),
+                "MavenInvoker should not duplicate the DefaultExceptionHandler 
hint for external artifacts");
+    }
+
+    @Test
+    void deduplicationPreventsDoubleHint() {
+        // Same artifact filtered out twice — should only log once
+        MavenProject lib = makeProject("com.example", "lib", "1.0-SNAPSHOT");
+        MavenProject app = makeProject("com.example", "app", "1.0-SNAPSHOT");
+
+        MavenExecutionResult result = new DefaultMavenExecutionResult();
+        result.setAllProjects(List.of(lib, app));
+        result.setTopologicallySortedProjects(List.of(app)); // lib not 
selected
+
+        ArtifactResolutionException exception = makeException("com.example", 
"lib", "1.0-SNAPSHOT");
+
+        Set<String> alreadyHinted = new HashSet<>();
+        invoker.logFilteredOutHint(context, exception, result, alreadyHinted);
+        int countAfterFirst = errorLog.size();
+        invoker.logFilteredOutHint(context, exception, result, alreadyHinted);
+
+        assertTrue(countAfterFirst > 0, "Should log on first call");
+        assertEquals(errorLog.size(), countAfterFirst, "Should not log again 
for the same artifact");
+    }
+}
diff --git a/impl/maven-core/src/main/java/org/apache/maven/DefaultMaven.java 
b/impl/maven-core/src/main/java/org/apache/maven/DefaultMaven.java
index 07c37b7a58..271bfa4fbd 100644
--- a/impl/maven-core/src/main/java/org/apache/maven/DefaultMaven.java
+++ b/impl/maven-core/src/main/java/org/apache/maven/DefaultMaven.java
@@ -293,6 +293,7 @@ private MavenExecutionResult doExecute(
             }
 
             result.setTopologicallySortedProjects(session.getProjects());
+            result.setAllProjects(session.getAllProjects());
 
             result.setProject(session.getTopLevelProject());
 
diff --git 
a/impl/maven-core/src/main/java/org/apache/maven/exception/DefaultExceptionHandler.java
 
b/impl/maven-core/src/main/java/org/apache/maven/exception/DefaultExceptionHandler.java
index fdf2441bec..8bd38b6176 100644
--- 
a/impl/maven-core/src/main/java/org/apache/maven/exception/DefaultExceptionHandler.java
+++ 
b/impl/maven-core/src/main/java/org/apache/maven/exception/DefaultExceptionHandler.java
@@ -40,6 +40,7 @@
 import org.apache.maven.plugin.PluginExecutionException;
 import org.apache.maven.project.ProjectBuildingException;
 import org.apache.maven.project.ProjectBuildingResult;
+import org.eclipse.aether.artifact.Artifact;
 import org.eclipse.aether.transfer.ArtifactFilteredOutException;
 
 /*
@@ -235,7 +236,7 @@ private boolean isNoteworthyException(Throwable exception) {
     private String getMessage(String message, Throwable exception) {
         String fullMessage = (message != null) ? message : "";
 
-        boolean hasArtifactFilteredOut = false;
+        ArtifactFilteredOutException artifactFilteredOut = null;
 
         // To break out of possible endless loop when getCause returns "this", 
or dejaVu for n-level recursion (n>1)
         Set<Throwable> dejaVu = Collections.newSetFromMap(new 
IdentityHashMap<>());
@@ -268,8 +269,8 @@ private String getMessage(String message, Throwable 
exception) {
                 fullMessage = join(fullMessage, exceptionMessage);
             }
 
-            if (t instanceof ArtifactFilteredOutException) {
-                hasArtifactFilteredOut = true;
+            if (artifactFilteredOut == null && t instanceof 
ArtifactFilteredOutException filteredOut) {
+                artifactFilteredOut = filteredOut;
             }
 
             if (!dejaVu.add(t)) {
@@ -278,19 +279,27 @@ private String getMessage(String message, Throwable 
exception) {
             }
         }
 
-        if (hasArtifactFilteredOut) {
+        if (artifactFilteredOut != null) {
             fullMessage += """
 
-                This error indicates that a remote repository filter has 
rejected this artifact. This commonly happens with repository managers using 
virtual/group repositories that do not properly aggregate prefix files.
+                This error indicates that a remote repository filter has 
rejected %s.
+                This commonly happens with repository managers using 
virtual/group repositories that do not properly aggregate prefix files.
                 To disable remote repository filtering, add one or both of 
these to your command line or to .mvn/maven.config:
                   -Daether.remoteRepositoryFilter.prefixes=false
                   -Daether.remoteRepositoryFilter.groupId=false
-                See 
https://maven.apache.org/resolver/remote-repository-filtering.html""";;
+                See 
https://maven.apache.org/resolver/remote-repository-filtering.html""".formatted(coordinates(artifactFilteredOut));
         }
 
         return fullMessage.trim();
     }
 
+    private static String coordinates(ArtifactFilteredOutException exception) {
+        Artifact artifact = exception.getArtifact();
+        return artifact == null
+                ? "the artifact"
+                : artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" 
+ artifact.getVersion();
+    }
+
     private static <T extends Throwable> T findCause(Throwable exception, 
Class<T> type) {
         Set<Throwable> dejaVu = Collections.newSetFromMap(new 
IdentityHashMap<>());
         for (Throwable t = exception; t != null && dejaVu.add(t); t = 
t.getCause()) {
diff --git 
a/impl/maven-core/src/main/java/org/apache/maven/execution/DefaultMavenExecutionResult.java
 
b/impl/maven-core/src/main/java/org/apache/maven/execution/DefaultMavenExecutionResult.java
index 82c331813f..88a899d93f 100644
--- 
a/impl/maven-core/src/main/java/org/apache/maven/execution/DefaultMavenExecutionResult.java
+++ 
b/impl/maven-core/src/main/java/org/apache/maven/execution/DefaultMavenExecutionResult.java
@@ -32,6 +32,8 @@ public class DefaultMavenExecutionResult implements 
MavenExecutionResult {
 
     private List<MavenProject> topologicallySortedProjects = 
Collections.emptyList();
 
+    private List<MavenProject> allProjects = Collections.emptyList();
+
     private DependencyResolutionResult dependencyResolutionResult;
 
     private final List<Throwable> exceptions = new CopyOnWriteArrayList<>();
@@ -66,6 +68,17 @@ public List<MavenProject> getTopologicallySortedProjects() {
                 : Collections.unmodifiableList(topologicallySortedProjects);
     }
 
+    @Override
+    public MavenExecutionResult setAllProjects(List<MavenProject> allProjects) 
{
+        this.allProjects = allProjects;
+        return this;
+    }
+
+    @Override
+    public List<MavenProject> getAllProjects() {
+        return null == allProjects ? Collections.emptyList() : 
Collections.unmodifiableList(allProjects);
+    }
+
     @Override
     public DependencyResolutionResult getDependencyResolutionResult() {
         return dependencyResolutionResult;
diff --git 
a/impl/maven-core/src/main/java/org/apache/maven/execution/MavenExecutionResult.java
 
b/impl/maven-core/src/main/java/org/apache/maven/execution/MavenExecutionResult.java
index 260de18fc7..7298a4088c 100644
--- 
a/impl/maven-core/src/main/java/org/apache/maven/execution/MavenExecutionResult.java
+++ 
b/impl/maven-core/src/main/java/org/apache/maven/execution/MavenExecutionResult.java
@@ -18,6 +18,7 @@
  */
 package org.apache.maven.execution;
 
+import java.util.Collections;
 import java.util.List;
 
 import org.apache.maven.project.DependencyResolutionResult;
@@ -37,6 +38,20 @@ public interface MavenExecutionResult {
      */
     List<MavenProject> getTopologicallySortedProjects();
 
+    default MavenExecutionResult setAllProjects(List<MavenProject> projects) {
+        return this;
+    }
+
+    /**
+     * Returns all projects in the reactor graph, including those not selected 
by {@code -pl}.
+     * This is a superset of {@link #getTopologicallySortedProjects()}.
+     *
+     * @return all reactor projects, or an empty list if the graph has not 
been built.
+     */
+    default List<MavenProject> getAllProjects() {
+        return Collections.emptyList();
+    }
+
     MavenExecutionResult 
setDependencyResolutionResult(DependencyResolutionResult result);
 
     DependencyResolutionResult getDependencyResolutionResult();
diff --git 
a/impl/maven-core/src/test/java/org/apache/maven/exception/DefaultExceptionHandlerTest.java
 
b/impl/maven-core/src/test/java/org/apache/maven/exception/DefaultExceptionHandlerTest.java
index e4cc6ab06a..2ea55f64b5 100644
--- 
a/impl/maven-core/src/test/java/org/apache/maven/exception/DefaultExceptionHandlerTest.java
+++ 
b/impl/maven-core/src/test/java/org/apache/maven/exception/DefaultExceptionHandlerTest.java
@@ -166,6 +166,8 @@ void testArtifactFilteredOutException() {
         assertTrue(
                 summary.getMessage().contains("remote repository filter"),
                 "Message should explain the filtering cause");
+        assertTrue(
+                summary.getMessage().contains("com.example:my-lib:1.0"), 
"Message should name the rejected artifact");
         assertEquals(
                 
"https://maven.apache.org/resolver/remote-repository-filtering.html";,
                 summary.getReference(),

Reply via email to