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

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

commit c152a35fda3582165abb344d8ac0186df8041b2e
Author: Sylwester Lachiewicz <[email protected]>
AuthorDate: Sat Nov 21 19:09:40 2026 +0100

    Warn when a mojo sets a non-POM file on a pom project's main artifact
    
    In a pom-packaged project, project.getArtifact() has the POM's coordinates; 
a file set on it by a plugin such as jar:jar can be published as the POM. Warn 
once, naming the mojo that set it. Part of #10481 (MNG-8138); the Maven 4 
counterpart is #14089.
---
 .../maven/lifecycle/internal/MojoExecutor.java     | 38 +++++++++
 .../MojoExecutorPomArtifactWarningTest.java        | 98 ++++++++++++++++++++++
 2 files changed, 136 insertions(+)

diff --git 
a/maven-core/src/main/java/org/apache/maven/lifecycle/internal/MojoExecutor.java
 
b/maven-core/src/main/java/org/apache/maven/lifecycle/internal/MojoExecutor.java
index e56b78f1a6..f9c6d0b657 100644
--- 
a/maven-core/src/main/java/org/apache/maven/lifecycle/internal/MojoExecutor.java
+++ 
b/maven-core/src/main/java/org/apache/maven/lifecycle/internal/MojoExecutor.java
@@ -22,6 +22,7 @@
 import javax.inject.Named;
 import javax.inject.Singleton;
 
+import java.io.File;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
@@ -333,7 +334,15 @@ 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);
+                String warning = pomArtifactFileWarning(project, 
artifactFileBefore, mojoExecution);
+                if (warning != null) {
+                    LOGGER.warn(warning);
+                }
             } catch (MojoFailureException
                     | PluginManagerException
                     | PluginConfigurationException
@@ -350,6 +359,35 @@ 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 may then publish
+     * that file as the POM. Name the mojo that changed the file, not every 
later mojo that runs while it is still set.
+     *
+     * @return the warning, or {@code null} when there is nothing to report
+     */
+    static String pomArtifactFileWarning(MavenProject project, File 
artifactFileBefore, MojoExecution mojoExecution) {
+        if (project == null || project.getFile() == null) {
+            return null;
+        }
+        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 null;
+        }
+        return "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()
+                + "). Install and deploy may publish this file in place of the 
POM;"
+                + " attach it with a classifier instead.";
+    }
+
     public void ensureDependenciesAreResolved(
             MojoDescriptor mojoDescriptor, MavenSession session, 
DependencyContext dependencyContext)
             throws LifecycleExecutionException {
diff --git 
a/maven-core/src/test/java/org/apache/maven/lifecycle/internal/MojoExecutorPomArtifactWarningTest.java
 
b/maven-core/src/test/java/org/apache/maven/lifecycle/internal/MojoExecutorPomArtifactWarningTest.java
new file mode 100644
index 0000000000..538a39721b
--- /dev/null
+++ 
b/maven-core/src/test/java/org/apache/maven/lifecycle/internal/MojoExecutorPomArtifactWarningTest.java
@@ -0,0 +1,98 @@
+/*
+ * 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 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.assertNull;
+
+/**
+ * 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"));
+
+        String warning = MojoExecutor.pomArtifactFileWarning(project, null, 
jarJar());
+
+        assertEquals(
+                "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
+                        + "). Install and deploy may publish this file in 
place of the POM;"
+                        + " attach it with a classifier instead.",
+                warning);
+    }
+
+    @Test
+    void silentWhenPomArtifactHasThePom() {
+        assertNull(MojoExecutor.pomArtifactFileWarning(project("pom", POM), 
null, jarJar()));
+    }
+
+    @Test
+    void silentWhenPomArtifactHasNoFile() {
+        assertNull(MojoExecutor.pomArtifactFileWarning(project("pom", null), 
null, jarJar()));
+    }
+
+    @Test
+    void silentForJarProject() {
+        assertNull(
+                MojoExecutor.pomArtifactFileWarning(project("jar", new 
File("target/test-1.0.jar")), null, jarJar()));
+    }
+
+    @Test
+    void silentForLaterMojosOnceTheFileWasAlreadySet() {
+        File jar = new File("target/test-1.0.jar");
+        assertNull(MojoExecutor.pomArtifactFileWarning(project("pom", jar), 
jar, jarJar()));
+    }
+
+    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