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

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


The following commit(s) were added to refs/heads/master by this push:
     new 1f64db63c7 Keep dependency artifacts for mojos without a resolution 
scope (#14145)
1f64db63c7 is described below

commit 1f64db63c78bbddbf14eabdd5bf36f7659a49917
Author: Sylwester Lachiewicz <[email protected]>
AuthorDate: Fri Nov 27 19:00:26 2026 +0200

    Keep dependency artifacts for mojos without a resolution scope (#14145)
    
    The #14081 guard skipped the only place that initialised 
MavenProject.getDependencyArtifacts() for such mojos; the initialisation now 
runs outside the guard.
---
 .../internal/LifecycleDependencyResolver.java      | 22 ++++---
 .../maven/lifecycle/internal/MojoExecutor.java     |  3 +
 .../MojoExecutorDependencyArtifactsTest.java       | 73 ++++++++++++++++++++++
 .../internal/MojoExecutorEmptyScopeTest.java       |  4 +-
 .../mojo-executor-dependency-artifacts/pom.xml     | 36 +++++++++++
 5 files changed, 129 insertions(+), 9 deletions(-)

diff --git 
a/impl/maven-core/src/main/java/org/apache/maven/lifecycle/internal/LifecycleDependencyResolver.java
 
b/impl/maven-core/src/main/java/org/apache/maven/lifecycle/internal/LifecycleDependencyResolver.java
index a6d719e4f1..e65c63fc10 100644
--- 
a/impl/maven-core/src/main/java/org/apache/maven/lifecycle/internal/LifecycleDependencyResolver.java
+++ 
b/impl/maven-core/src/main/java/org/apache/maven/lifecycle/internal/LifecycleDependencyResolver.java
@@ -103,6 +103,20 @@ public static List<MavenProject> getProjects(MavenProject 
project, MavenSession
         }
     }
 
+    /**
+     * Makes sure {@link MavenProject#getDependencyArtifacts()} holds the 
(unresolved) artifacts of the project's
+     * direct dependencies, so that mojos which declare no dependency 
resolution can still read them.
+     */
+    public void ensureDependencyArtifacts(MavenProject project) throws 
LifecycleExecutionException {
+        if (project.getDependencyArtifacts() == null) {
+            try {
+                
project.setDependencyArtifacts(artifactFactory.createArtifacts(project));
+            } catch (InvalidDependencyVersionException e) {
+                throw new LifecycleExecutionException(e);
+            }
+        }
+    }
+
     public void resolveProjectDependencies(
             MavenProject project,
             Collection<String> scopesToCollect,
@@ -121,13 +135,7 @@ public void resolveProjectDependencies(
                 }
             }
 
-            if (project.getDependencyArtifacts() == null) {
-                try {
-                    
project.setDependencyArtifacts(artifactFactory.createArtifacts(project));
-                } catch (InvalidDependencyVersionException e) {
-                    throw new LifecycleExecutionException(e);
-                }
-            }
+            ensureDependencyArtifacts(project);
 
             Set<Artifact> resolvedArtifacts = resolveProjectArtifacts(
                     project, scopesToCollect, scopesToResolve, session, 
aggregating, projectArtifacts);
diff --git 
a/impl/maven-core/src/main/java/org/apache/maven/lifecycle/internal/MojoExecutor.java
 
b/impl/maven-core/src/main/java/org/apache/maven/lifecycle/internal/MojoExecutor.java
index 3b0ed1a6f1..59fc76ef75 100644
--- 
a/impl/maven-core/src/main/java/org/apache/maven/lifecycle/internal/MojoExecutor.java
+++ 
b/impl/maven-core/src/main/java/org/apache/maven/lifecycle/internal/MojoExecutor.java
@@ -453,6 +453,9 @@ public void ensureDependenciesAreResolved(
                     project, scopesToCollect, scopesToResolve, session, 
aggregating, Collections.emptySet());
 
             dependencyContext.synchronizeWithProjectState();
+        } else {
+            // Mojos without a resolution scope still expect the direct 
dependency artifacts (unresolved)
+            lifeCycleDependencyResolver.ensureDependencyArtifacts(project);
         }
 
         if (aggregating) {
diff --git 
a/impl/maven-core/src/test/java/org/apache/maven/lifecycle/internal/MojoExecutorDependencyArtifactsTest.java
 
b/impl/maven-core/src/test/java/org/apache/maven/lifecycle/internal/MojoExecutorDependencyArtifactsTest.java
new file mode 100644
index 0000000000..407c5193fd
--- /dev/null
+++ 
b/impl/maven-core/src/test/java/org/apache/maven/lifecycle/internal/MojoExecutorDependencyArtifactsTest.java
@@ -0,0 +1,73 @@
+/*
+ * 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.lifecycle.internal;
+
+import javax.inject.Inject;
+
+import java.io.File;
+import java.util.Collections;
+import java.util.Set;
+
+import org.apache.maven.AbstractCoreMavenComponentTestCase;
+import org.apache.maven.artifact.Artifact;
+import org.apache.maven.execution.MavenSession;
+import org.apache.maven.plugin.descriptor.MojoDescriptor;
+import org.apache.maven.plugin.descriptor.PluginDescriptor;
+import org.apache.maven.project.MavenProject;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+
+/**
+ * A mojo without any dependency resolution or collection scope must still see 
the direct dependency artifacts
+ * of its project, as it did before the scope-less step stopped being resolved 
(#14081).
+ */
+class MojoExecutorDependencyArtifactsTest extends 
AbstractCoreMavenComponentTestCase {
+
+    @Inject
+    private MojoExecutor mojoExecutor;
+
+    @Override
+    protected String getProjectsDirectory() {
+        return null;
+    }
+
+    @Test
+    void mojoWithoutScopeSeesDirectDependencyArtifacts() throws Exception {
+        MavenSession session =
+                createMavenSession(new 
File("src/test/projects/mojo-executor-dependency-artifacts/pom.xml"));
+        MavenProject project = session.getCurrentProject();
+
+        MojoDescriptor mojo = new MojoDescriptor();
+        mojo.setPluginDescriptor(new PluginDescriptor());
+
+        mojoExecutor.ensureDependenciesAreResolved(
+                mojo, session, new DependencyContext(project, 
Collections.emptyList(), Collections.emptyList()));
+
+        Set<Artifact> artifacts = project.getDependencyArtifacts();
+        assertNotNull(artifacts, "getDependencyArtifacts() must not be null 
for a mojo without a scope");
+        assertEquals(1, artifacts.size());
+        Artifact direct = artifacts.iterator().next();
+        assertEquals("direct", direct.getArtifactId());
+        assertFalse(direct.isResolved(), "nothing may be resolved for a mojo 
without a scope");
+        assertEquals(0, project.getArtifacts().size());
+    }
+}
diff --git 
a/impl/maven-core/src/test/java/org/apache/maven/lifecycle/internal/MojoExecutorEmptyScopeTest.java
 
b/impl/maven-core/src/test/java/org/apache/maven/lifecycle/internal/MojoExecutorEmptyScopeTest.java
index 8a5003c0ae..2637f84880 100644
--- 
a/impl/maven-core/src/test/java/org/apache/maven/lifecycle/internal/MojoExecutorEmptyScopeTest.java
+++ 
b/impl/maven-core/src/test/java/org/apache/maven/lifecycle/internal/MojoExecutorEmptyScopeTest.java
@@ -40,8 +40,8 @@
 import static org.mockito.Mockito.clearInvocations;
 import static org.mockito.Mockito.doAnswer;
 import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
 import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.verifyNoInteractions;
 import static org.mockito.Mockito.when;
 
 /**
@@ -110,7 +110,7 @@ void emptyScopeStepKeepsResolvedArtifacts() throws 
Exception {
                 new DependencyContext(project, Collections.emptyList(), 
Collections.emptyList());
         executor.ensureDependenciesAreResolved(mojo(null), session, 
noScopeStep);
 
-        verifyNoInteractions(resolver);
+        verify(resolver, never()).resolveProjectDependencies(any(), any(), 
any(), any(), anyBoolean(), any());
         assertEquals(0, project.getArtifacts().size(), "the no-scope mojo must 
still see no artifacts");
 
         // A later mojo of the original step must still see the artifacts 
resolved for that step.
diff --git 
a/impl/maven-core/src/test/projects/mojo-executor-dependency-artifacts/pom.xml 
b/impl/maven-core/src/test/projects/mojo-executor-dependency-artifacts/pom.xml
new file mode 100644
index 0000000000..e2625eb451
--- /dev/null
+++ 
b/impl/maven-core/src/test/projects/mojo-executor-dependency-artifacts/pom.xml
@@ -0,0 +1,36 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+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.
+-->
+
+<project>
+  <modelVersion>4.0.0</modelVersion>
+
+  <groupId>org.apache.maven.its</groupId>
+  <artifactId>mojo-executor-dependency-artifacts</artifactId>
+  <version>1.0</version>
+
+  <dependencies>
+    <dependency>
+      <groupId>org.apache.maven.its</groupId>
+      <artifactId>direct</artifactId>
+      <version>1.0</version>
+    </dependency>
+  </dependencies>
+</project>

Reply via email to