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 949435329c Keep a jar:jar file on a pom project from shadowing the POM 
artifact's path (#14090)
949435329c is described below

commit 949435329c2b78115578a6b910406e33bfd8c79e
Author: Sylwester Lachiewicz <[email protected]>
AuthorDate: Mon Nov 23 17:43:07 2026 +0100

    Keep a jar:jar file on a pom project from shadowing the POM artifact's path 
(#14090)
    
    In a pom-packaged project, project.getArtifact() has the POM's coordinates, 
and a plugin such as jar:jar may set another file on it. getPath() returned 
that file for the POM artifact, so install and deploy published it as the POM. 
The POM artifact now keeps its own path, the project's POM; an explicit 
setPath() still wins. MojoExecutor warns, naming the mojo that set the file. 
Part of #10481 (MNG-8138).
---
 .../internal/impl/DefaultArtifactManager.java      |  12 ++
 .../maven/lifecycle/internal/MojoExecutor.java     |  35 ++++++
 .../internal/impl/DefaultArtifactManagerTest.java  | 121 +++++++++++++++++++++
 .../MojoExecutorPomArtifactWarningTest.java        | 101 +++++++++++++++++
 4 files changed, 269 insertions(+)

diff --git 
a/impl/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultArtifactManager.java
 
b/impl/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultArtifactManager.java
index dca618d0e9..207ad93420 100644
--- 
a/impl/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultArtifactManager.java
+++ 
b/impl/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultArtifactManager.java
@@ -77,7 +77,11 @@ public Optional<Path> getPath(@Nonnull Artifact artifact) {
         }
         if (session.getMavenSession().getAllProjects() != null) {
             for (MavenProject project : 
session.getMavenSession().getAllProjects()) {
+                // In a pom-packaged project, project.getArtifact() has the 
POM's coordinates; a plugin such as
+                // jar:jar may still set another file on it. Do not let that 
file shadow the POM artifact, whose
+                // own path is the project's POM (MNG-8138).
                 if (id.equals(id(project.getArtifact()))
+                        && !id.equals(pomId(project))
                         && project.getArtifact().getFile() != null) {
                     return 
Optional.of(project.getArtifact().getFile().toPath());
                 }
@@ -123,6 +127,14 @@ private String id(org.apache.maven.artifact.Artifact 
artifact) {
                 + ":" + artifact.getVersion();
     }
 
+    /**
+     * The id of the project's POM artifact, as {@link 
#id(org.apache.maven.artifact.Artifact)} computes it for
+     * {@link org.apache.maven.project.artifact.ProjectArtifact}: extension 
{@code pom}, no classifier.
+     */
+    private String pomId(MavenProject project) {
+        return project.getGroupId() + ":" + project.getArtifactId() + ":pom:" 
+ project.getVersion();
+    }
+
     private String id(Artifact artifact) {
         return artifact.key();
     }
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 1eeab0a7e1..3b0ed1a6f1 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
@@ -23,6 +23,7 @@
 import javax.inject.Provider;
 import javax.inject.Singleton;
 
+import java.io.File;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
@@ -376,7 +377,13 @@ private void doExecute2(MavenSession session, 
MojoExecution mojoExecution) throw
         eventCatapult.fire(ExecutionEvent.Type.MojoStarted, session, 
mojoExecution);
         try {
             try {
+                MavenProject project = session.getCurrentProject();
+                File artifactFileBefore = project != null && 
project.getArtifact() != null
+                        ? project.getArtifact().getFile()
+                        : null;
                 pluginManager.executeMojo(session, mojoExecution);
+                pomArtifactFileWarning(project, artifactFileBefore, 
mojoExecution)
+                        .ifPresent(LOGGER::warn);
             } catch (MojoFailureException
                     | PluginManagerException
                     | PluginConfigurationException
@@ -394,6 +401,34 @@ private void doExecute2(MavenSession session, 
MojoExecution mojoExecution) throw
         }
     }
 
+    /**
+     * MNG-8138: a mojo (e.g. {@code jar:jar} on a pom-packaged project) may 
set a file on
+     * {@code project.getArtifact()} while that artifact has the POM's 
coordinates. Install and deploy then use the
+     * project's POM file instead, so the mojo's output is not published; say 
so, naming the mojo. Only the mojo that
+     * changed the file is named, not every later mojo that runs while the 
file is still set.
+     */
+    static Optional<String> pomArtifactFileWarning(
+            MavenProject project, File artifactFileBefore, MojoExecution 
mojoExecution) {
+        if (project == null || project.getFile() == null) {
+            return Optional.empty();
+        }
+        org.apache.maven.artifact.Artifact artifact = project.getArtifact();
+        if (artifact == null
+                || artifact.getFile() == null
+                || artifact.getArtifactHandler() == null
+                || !"pom".equals(artifact.getArtifactHandler().getExtension())
+                || (artifact.getClassifier() != null
+                        && !artifact.getClassifier().isEmpty())
+                || artifact.getFile().equals(project.getFile())
+                || artifact.getFile().equals(artifactFileBefore)) {
+            return Optional.empty();
+        }
+        return Optional.of("Plugin " + mojoExecution.getPlugin().getId() + ":" 
+ mojoExecution.getGoal()
+                + " set the file of " + artifact.getGroupId() + ":" + 
artifact.getArtifactId() + ":pom:"
+                + artifact.getVersion() + " to " + artifact.getFile() + ", but 
that artifact is the project's POM ("
+                + project.getFile() + "). The POM file is used; attach the 
file with a classifier instead.");
+    }
+
     public void ensureDependenciesAreResolved(
             MojoDescriptor mojoDescriptor, MavenSession session, 
DependencyContext dependencyContext)
             throws LifecycleExecutionException {
diff --git 
a/impl/maven-core/src/test/java/org/apache/maven/internal/impl/DefaultArtifactManagerTest.java
 
b/impl/maven-core/src/test/java/org/apache/maven/internal/impl/DefaultArtifactManagerTest.java
index f0fd9d2c3a..4b93db47ed 100644
--- 
a/impl/maven-core/src/test/java/org/apache/maven/internal/impl/DefaultArtifactManagerTest.java
+++ 
b/impl/maven-core/src/test/java/org/apache/maven/internal/impl/DefaultArtifactManagerTest.java
@@ -23,17 +23,23 @@
 import java.util.List;
 import java.util.Optional;
 
+import org.apache.maven.RepositoryUtils;
+import org.apache.maven.api.Artifact;
 import org.apache.maven.api.ProducedArtifact;
+import org.apache.maven.api.Version;
 import org.apache.maven.artifact.DefaultArtifact;
 import org.apache.maven.artifact.handler.ArtifactHandler;
 import org.apache.maven.execution.DefaultMavenExecutionRequest;
 import org.apache.maven.execution.MavenSession;
+import org.apache.maven.impl.InternalSession;
 import org.apache.maven.project.MavenProject;
+import org.apache.maven.project.artifact.ProjectArtifact;
 import org.eclipse.aether.DefaultRepositorySystemSession;
 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.ArgumentMatchers.anyString;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
@@ -117,6 +123,121 @@ void setPathTakesPrecedenceOverProjectArtifactFile() {
                         + " not the JAR file in the project artifact 
(MINSTALL-315)");
     }
 
+    /**
+     * MNG-8138: {@code jar:jar} on a pom-packaged project sets the JAR as the 
file of the project artifact, which has
+     * the POM's coordinates. Without any {@code setPath()} call, {@code 
getPath(pomArtifact)} must still return the
+     * project's POM file, so install and deploy never publish the JAR under 
the POM's coordinates.
+     */
+    @Test
+    void pomArtifactPathIsProjectFileEvenWhenProjectArtifactHasJar() {
+        Path pomFile = Path.of("pom.xml").toAbsolutePath();
+        MavenProject project = pomProject(pomFile);
+        project.getArtifact().setFile(new File("target/pom-project-1.0.jar"));
+
+        DefaultArtifactManager manager = manager(project);
+
+        assertEquals(Optional.of(pomFile), 
manager.getPath(pomArtifact(project)));
+    }
+
+    @Test
+    void explicitSetPathOfPomArtifactWinsOverProjectFile() {
+        MavenProject project = pomProject(Path.of("pom.xml").toAbsolutePath());
+        DefaultArtifactManager manager = manager(project);
+        ProducedArtifact pomArtifact = 
artifact("com.example:pom-project:pom:1.0");
+        Path flattened = Path.of("target/flattened-pom.xml").toAbsolutePath();
+
+        manager.setPath(pomArtifact, flattened);
+
+        assertEquals(Optional.of(flattened), manager.getPath(pomArtifact));
+    }
+
+    /**
+     * An artifact with a reactor project's POM coordinates but its own path, 
such as the consumer POM of a project
+     * not selected with {@code -pl}, keeps that path: only the pom-packaged 
project's main artifact is ignored.
+     */
+    @Test
+    void artifactWithPomCoordinatesKeepsItsOwnPath() {
+        MavenProject project = pomProject(Path.of("pom.xml").toAbsolutePath());
+        project.getArtifact().setFile(new File("target/pom-project-1.0.jar"));
+        Path repositoryPom = 
Path.of("target/repo/pom-project-1.0.pom").toAbsolutePath();
+
+        DefaultArtifactManager manager = manager(project);
+
+        assertEquals(
+                Optional.of(repositoryPom),
+                manager.getPath(new org.apache.maven.impl.DefaultArtifact(
+                        versionParsingSession(),
+                        new 
org.eclipse.aether.artifact.DefaultArtifact("com.example", "pom-project", "", 
"pom", "1.0")
+                                .setPath(repositoryPom))));
+    }
+
+    @Test
+    void mainArtifactOfJarProjectIsStillItsJar() {
+        MavenProject project = new MavenProject();
+        project.setGroupId("com.example");
+        project.setArtifactId("jar-project");
+        project.setVersion("1.0");
+        project.setPackaging("jar");
+        project.setFile(Path.of("pom.xml").toAbsolutePath().toFile());
+        DefaultArtifact main = new DefaultArtifact(
+                "com.example", "jar-project", "1.0", null, "jar", null, new 
TestArtifactHandler("jar"));
+        File jar = new File("target/jar-project-1.0.jar");
+        main.setFile(jar);
+        project.setArtifact(main);
+
+        DefaultArtifactManager manager = manager(project);
+
+        assertEquals(Optional.of(jar.toPath()), 
manager.getPath(artifact("com.example:jar-project:jar:1.0")));
+    }
+
+    private static MavenProject pomProject(Path pomFile) {
+        MavenProject project = new MavenProject();
+        project.setGroupId("com.example");
+        project.setArtifactId("pom-project");
+        project.setVersion("1.0");
+        project.setPackaging("pom");
+        if (pomFile != null) {
+            project.setFile(pomFile.toFile());
+        }
+        project.setArtifact(
+                new DefaultArtifact("com.example", "pom-project", "1.0", null, 
"pom", null, new PomArtifactHandler()));
+        return project;
+    }
+
+    private static Artifact pomArtifact(MavenProject project) {
+        return new org.apache.maven.impl.DefaultArtifact(
+                versionParsingSession(), RepositoryUtils.toArtifact(new 
ProjectArtifact(project)));
+    }
+
+    /**
+     * {@link org.apache.maven.impl.DefaultArtifact#key()} goes through {@code 
session.parseVersion()}; a bare mock
+     * would yield {@code null} and a key that never matches a project.
+     */
+    private static InternalSession versionParsingSession() {
+        InternalSession session = mock(InternalSession.class);
+        when(session.parseVersion(anyString())).thenAnswer(invocation -> {
+            Version version = mock(Version.class);
+            when(version.toString()).thenReturn(invocation.getArgument(0));
+            return version;
+        });
+        return session;
+    }
+
+    private static DefaultArtifactManager manager(MavenProject project) {
+        InternalMavenSession internalSession = 
mock(InternalMavenSession.class);
+        MavenSession mavenSession = new MavenSession(
+                null, new DefaultRepositorySystemSession(h -> false), new 
DefaultMavenExecutionRequest(), null);
+        mavenSession.setAllProjects(List.of(project));
+        when(internalSession.getMavenSession()).thenReturn(mavenSession);
+        return new DefaultArtifactManager(internalSession);
+    }
+
+    private static ProducedArtifact artifact(String key) {
+        ProducedArtifact artifact = mock(ProducedArtifact.class);
+        when(artifact.key()).thenReturn(key);
+        return artifact;
+    }
+
     /**
      * Minimal ArtifactHandler for POM-type artifacts, matching the handler 
used by
      * {@link org.apache.maven.project.artifact.ProjectArtifact}.
diff --git 
a/impl/maven-core/src/test/java/org/apache/maven/lifecycle/internal/MojoExecutorPomArtifactWarningTest.java
 
b/impl/maven-core/src/test/java/org/apache/maven/lifecycle/internal/MojoExecutorPomArtifactWarningTest.java
new file mode 100644
index 0000000000..52fe3df87a
--- /dev/null
+++ 
b/impl/maven-core/src/test/java/org/apache/maven/lifecycle/internal/MojoExecutorPomArtifactWarningTest.java
@@ -0,0 +1,101 @@
+/*
+ * 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 java.io.File;
+import java.util.Optional;
+
+import org.apache.maven.artifact.DefaultArtifact;
+import org.apache.maven.artifact.handler.DefaultArtifactHandler;
+import org.apache.maven.model.Plugin;
+import org.apache.maven.plugin.MojoExecution;
+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.assertTrue;
+
+/**
+ * MNG-8138: warn when a mojo sets a file on the project artifact that has the 
POM's coordinates.
+ */
+class MojoExecutorPomArtifactWarningTest {
+
+    private static final File POM = new File("pom.xml").getAbsoluteFile();
+
+    @Test
+    void warnsWhenJarIsSetOnPomArtifact() {
+        MavenProject project = project("pom", new File("target/test-1.0.jar"));
+
+        Optional<String> warning = 
MojoExecutor.pomArtifactFileWarning(project, null, jarJar());
+
+        assertEquals(
+                Optional.of("Plugin 
org.apache.maven.plugins:maven-jar-plugin:3.5.1:jar set the file of"
+                        + " org.example:test:pom:1.0 to " + new 
File("target/test-1.0.jar")
+                        + ", but that artifact is the project's POM (" + POM
+                        + "). The POM file is used; attach the file with a 
classifier instead."),
+                warning);
+    }
+
+    @Test
+    void silentWhenPomArtifactHasThePom() {
+        assertTrue(MojoExecutor.pomArtifactFileWarning(project("pom", POM), 
null, jarJar())
+                .isEmpty());
+    }
+
+    @Test
+    void silentWhenPomArtifactHasNoFile() {
+        assertTrue(MojoExecutor.pomArtifactFileWarning(project("pom", null), 
null, jarJar())
+                .isEmpty());
+    }
+
+    @Test
+    void silentForJarProject() {
+        assertTrue(MojoExecutor.pomArtifactFileWarning(project("jar", new 
File("target/test-1.0.jar")), null, jarJar())
+                .isEmpty());
+    }
+
+    @Test
+    void silentForLaterMojosOnceTheFileWasAlreadySet() {
+        File jar = new File("target/test-1.0.jar");
+        assertTrue(MojoExecutor.pomArtifactFileWarning(project("pom", jar), 
jar, jarJar())
+                .isEmpty());
+    }
+
+    private static MavenProject project(String extension, File artifactFile) {
+        MavenProject project = new MavenProject();
+        project.setGroupId("org.example");
+        project.setArtifactId("test");
+        project.setVersion("1.0");
+        project.setPackaging(extension);
+        project.setFile(POM);
+        DefaultArtifact artifact = new DefaultArtifact(
+                "org.example", "test", "1.0", null, extension, null, new 
DefaultArtifactHandler(extension));
+        artifact.setFile(artifactFile);
+        project.setArtifact(artifact);
+        return project;
+    }
+
+    private static MojoExecution jarJar() {
+        Plugin plugin = new Plugin();
+        plugin.setGroupId("org.apache.maven.plugins");
+        plugin.setArtifactId("maven-jar-plugin");
+        plugin.setVersion("3.5.1");
+        return new MojoExecution(plugin, "jar", "default-cli");
+    }
+}

Reply via email to