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 bf482b6199 Fix MojoExtension.beforeEach to use merged model instead of 
raw parsed model (#12242)
bf482b6199 is described below

commit bf482b61999f581bd1c0b2bb6874f46701305a5b
Author: UjjwalChitransh <[email protected]>
AuthorDate: Wed Jun 17 11:58:58 2026 +0530

    Fix MojoExtension.beforeEach to use merged model instead of raw parsed 
model (#12242)
    
    Apply alignToBaseDirectory to the merged model (tmodel + defaultModel)
    instead of the raw parsed tmodel. This fixes two bugs:
    
    1. NPE when no POM file exists (tmodel is null in that case)
    2. Stored model missing defaults injected during merging (groupId,
       artifactId, build paths, etc.)
    
    Update createProject provider to use the aligned model consistently.
    Add unit test for model handling in MojoExtension.beforeEach.
    
    Fixes #12201
---
 .../maven/api/plugin/testing/MojoExtension.java    | 16 +++--
 .../api/plugin/testing/MojoExtensionModelTest.java | 84 ++++++++++++++++++++++
 2 files changed, 94 insertions(+), 6 deletions(-)

diff --git 
a/impl/maven-testing/src/main/java/org/apache/maven/api/plugin/testing/MojoExtension.java
 
b/impl/maven-testing/src/main/java/org/apache/maven/api/plugin/testing/MojoExtension.java
index 13138d3251..df777a1ba9 100644
--- 
a/impl/maven-testing/src/main/java/org/apache/maven/api/plugin/testing/MojoExtension.java
+++ 
b/impl/maven-testing/src/main/java/org/apache/maven/api/plugin/testing/MojoExtension.java
@@ -437,9 +437,9 @@ public void beforeEach(ExtensionContext context) throws 
Exception {
         } else {
             model = new MavenMerger().merge(tmodel, defaultModel, false, null);
         }
-        tmodel = new DefaultModelPathTranslator(new DefaultPathTranslator())
-                .alignToBaseDirectory(tmodel, Paths.get(getBasedir()), null);
-        context.getStore(MOJO_EXTENSION).put(Model.class, tmodel);
+        final Model alignedModel = new DefaultModelPathTranslator(new 
DefaultPathTranslator())
+                .alignToBaseDirectory(model, Paths.get(getBasedir()), null);
+        context.getStore(MOJO_EXTENSION).put(Model.class, alignedModel);
 
         // mojo execution
         // Map<Object, Object> map = 
getInjector().getContext().getContextData();
@@ -485,12 +485,16 @@ private InternalSession createSession() {
             @Priority(-10)
             private Project createProject(InternalSession s) {
                 ProjectStub stub = new ProjectStub();
-                if (!"pom".equals(model.getPackaging())) {
+                if (!"pom".equals(alignedModel.getPackaging())) {
                     ProducedArtifactStub artifact = new ProducedArtifactStub(
-                            model.getGroupId(), model.getArtifactId(), "", 
model.getVersion(), model.getPackaging());
+                            alignedModel.getGroupId(),
+                            alignedModel.getArtifactId(),
+                            "",
+                            alignedModel.getVersion(),
+                            alignedModel.getPackaging());
                     stub.setMainArtifact(artifact);
                 }
-                stub.setModel(model);
+                stub.setModel(alignedModel);
                 stub.setBasedir(Paths.get(MojoExtension.getBasedir()));
                 stub.setPomPath(modelPath[0]);
                 
s.getService(ArtifactManager.class).setPath(stub.getPomArtifact(), 
modelPath[0]);
diff --git 
a/impl/maven-testing/src/test/java/org/apache/maven/api/plugin/testing/MojoExtensionModelTest.java
 
b/impl/maven-testing/src/test/java/org/apache/maven/api/plugin/testing/MojoExtensionModelTest.java
new file mode 100644
index 0000000000..a6b2df0855
--- /dev/null
+++ 
b/impl/maven-testing/src/test/java/org/apache/maven/api/plugin/testing/MojoExtensionModelTest.java
@@ -0,0 +1,84 @@
+/*
+ * 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.api.plugin.testing;
+
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+import org.apache.maven.api.Project;
+import org.apache.maven.api.di.Inject;
+import org.apache.maven.api.model.Model;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+@MojoTest
+class MojoExtensionModelTest {
+
+    private static final String COORDINATES = 
"test:test-plugin:0.0.1-SNAPSHOT:goal";
+
+    private static final String MINIMAL_POM = "<project>"
+            + "<groupId>customGroupId</groupId>"
+            + "<artifactId>customArtifactId</artifactId>"
+            + "<version>2.0</version>"
+            + "</project>";
+
+    @Inject
+    Project project;
+
+    @Test
+    void beforeEachUsesDefaultModelWhenNoPom() {
+        assertNotNull(project);
+        Model model = project.getModel();
+        assertNotNull(model);
+        assertEquals("myGroupId", model.getGroupId());
+        assertEquals("myArtifactId", model.getArtifactId());
+        assertEquals("1.0-SNAPSHOT", model.getVersion());
+        assertEquals("jar", model.getPackaging());
+        assertNotNull(model.getBuild());
+        assertNotNull(model.getBuild().getDirectory());
+    }
+
+    @Test
+    @InjectMojo(goal = COORDINATES, pom = MINIMAL_POM)
+    void mergedModelIncludesDefaultBuildPaths() {
+        Model model = project.getModel();
+        assertEquals("customGroupId", model.getGroupId());
+        assertEquals("customArtifactId", model.getArtifactId());
+        assertEquals("2.0", model.getVersion());
+        assertNotNull(model.getBuild());
+        assertTrue(
+                
Paths.get(model.getBuild().getDirectory()).endsWith(Paths.get("target")),
+                "Build directory from defaultModel should be present after 
merge");
+        assertTrue(
+                
Paths.get(model.getBuild().getOutputDirectory()).endsWith(Paths.get("target", 
"classes")),
+                "Output directory from defaultModel should be present after 
merge");
+    }
+
+    @Test
+    @InjectMojo(goal = COORDINATES, pom = MINIMAL_POM)
+    void storedModelHasAlignedPaths() {
+        Model model = project.getModel();
+        assertNotNull(model.getBuild());
+        Path buildDir = Paths.get(model.getBuild().getDirectory());
+        assertTrue(buildDir.isAbsolute(), "Build directory should be absolute 
after path alignment");
+    }
+}

Reply via email to