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

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


The following commit(s) were added to refs/heads/master by this push:
     new e9642d6  Add skipAttachedArtifacts to keep selected attached artifacts 
out of a deployment
e9642d6 is described below

commit e9642d61f9ec4a9058d8c4075c9ff39dbe68143f
Author: Sylwester Lachiewicz <[email protected]>
AuthorDate: Sat Nov 21 20:50:21 2026 +0100

    Add skipAttachedArtifacts to keep selected attached artifacts out of a 
deployment
    
    An entry that matches no attached artifact fails the build: a misspelled
    entry would otherwise publish the artifact it was meant to hold back.
    
    The selection is made in the deploy execution of each project and kept in
    its plugin context, because with deployAtEnd the mojo of the last project
    deploys all of them and has only its own configuration.
    
    Co-authored-by: gregorycallea <[email protected]>
---
 src/it/skip-attached-artifacts/module1/pom.xml     |  49 ++++++++
 .../java/org/apache/maven/test/HelloWorld.java     |  21 ++++
 src/it/skip-attached-artifacts/module2/pom.xml     |  32 ++++++
 .../java/org/apache/maven/test/HelloWorld.java     |  21 ++++
 src/it/skip-attached-artifacts/pom.xml             |  99 +++++++++++++++++
 src/it/skip-attached-artifacts/verify.groovy       |  41 +++++++
 .../maven/plugins/deploy/AttachedArtifact.java     | 113 +++++++++++++++++++
 .../apache/maven/plugins/deploy/DeployMojo.java    |  86 +++++++++++++-
 src/site/fml/faq.fml                               |  34 ++++++
 .../maven/plugins/deploy/DeployMojoTest.java       | 123 +++++++++++++++++++++
 .../plugin-config.xml                              |  36 ++++++
 11 files changed, 652 insertions(+), 3 deletions(-)

diff --git a/src/it/skip-attached-artifacts/module1/pom.xml 
b/src/it/skip-attached-artifacts/module1/pom.xml
new file mode 100644
index 0000000..03c1ef1
--- /dev/null
+++ b/src/it/skip-attached-artifacts/module1/pom.xml
@@ -0,0 +1,49 @@
+<?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 xmlns="http://maven.apache.org/POM/4.0.0"; 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd";>
+  <modelVersion>4.0.0</modelVersion>
+
+  <parent>
+    <groupId>org.apache.maven.its.deploy.saa</groupId>
+    <artifactId>skip-attached-artifacts</artifactId>
+    <version>1.0</version>
+  </parent>
+  <artifactId>module1</artifactId>
+
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-deploy-plugin</artifactId>
+        <configuration>
+          <skipAttachedArtifacts>
+            <attachedArtifact>
+              <classifier>sources</classifier>
+              <extension>jar</extension>
+            </attachedArtifact>
+          </skipAttachedArtifacts>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+
+</project>
diff --git 
a/src/it/skip-attached-artifacts/module1/src/main/java/org/apache/maven/test/HelloWorld.java
 
b/src/it/skip-attached-artifacts/module1/src/main/java/org/apache/maven/test/HelloWorld.java
new file mode 100644
index 0000000..341c633
--- /dev/null
+++ 
b/src/it/skip-attached-artifacts/module1/src/main/java/org/apache/maven/test/HelloWorld.java
@@ -0,0 +1,21 @@
+/*
+ * 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.test;
+
+public class HelloWorld {}
diff --git a/src/it/skip-attached-artifacts/module2/pom.xml 
b/src/it/skip-attached-artifacts/module2/pom.xml
new file mode 100644
index 0000000..22a1612
--- /dev/null
+++ b/src/it/skip-attached-artifacts/module2/pom.xml
@@ -0,0 +1,32 @@
+<?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 xmlns="http://maven.apache.org/POM/4.0.0"; 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd";>
+  <modelVersion>4.0.0</modelVersion>
+
+  <parent>
+    <groupId>org.apache.maven.its.deploy.saa</groupId>
+    <artifactId>skip-attached-artifacts</artifactId>
+    <version>1.0</version>
+  </parent>
+  <artifactId>module2</artifactId>
+
+</project>
diff --git 
a/src/it/skip-attached-artifacts/module2/src/main/java/org/apache/maven/test/HelloWorld.java
 
b/src/it/skip-attached-artifacts/module2/src/main/java/org/apache/maven/test/HelloWorld.java
new file mode 100644
index 0000000..341c633
--- /dev/null
+++ 
b/src/it/skip-attached-artifacts/module2/src/main/java/org/apache/maven/test/HelloWorld.java
@@ -0,0 +1,21 @@
+/*
+ * 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.test;
+
+public class HelloWorld {}
diff --git a/src/it/skip-attached-artifacts/pom.xml 
b/src/it/skip-attached-artifacts/pom.xml
new file mode 100644
index 0000000..aa9628d
--- /dev/null
+++ b/src/it/skip-attached-artifacts/pom.xml
@@ -0,0 +1,99 @@
+<?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 xmlns="http://maven.apache.org/POM/4.0.0"; 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd";>
+  <modelVersion>4.0.0</modelVersion>
+
+  <groupId>org.apache.maven.its.deploy.saa</groupId>
+  <artifactId>skip-attached-artifacts</artifactId>
+  <version>1.0</version>
+  <packaging>pom</packaging>
+
+  <description>Tests that skipAttachedArtifacts of one module holds back only 
the attached artifacts of that module,
+    also when all modules are deployed at the end of the build.</description>
+
+  <modules>
+    <module>module1</module>
+    <module>module2</module>
+  </modules>
+
+  <distributionManagement>
+    <repository>
+      <id>it</id>
+      <url>file:///${basedir}/target/repo</url>
+    </repository>
+  </distributionManagement>
+
+  <properties>
+    <maven.test.skip>true</maven.test.skip>
+  </properties>
+
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-compiler-plugin</artifactId>
+        <version>@mavenCompilerPluginVersion@</version>
+      </plugin>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-deploy-plugin</artifactId>
+        <version>@project.version@</version>
+        <configuration>
+          <deployAtEnd>true</deployAtEnd>
+        </configuration>
+      </plugin>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-install-plugin</artifactId>
+        <version>@mavenInstallPluginVersion@</version>
+      </plugin>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-jar-plugin</artifactId>
+        <version>@mavenJarPluginVersion@</version>
+      </plugin>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-resources-plugin</artifactId>
+        <version>@mavenResourcesPluginVersion@</version>
+      </plugin>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-source-plugin</artifactId>
+        <version>@mavenSourcePluginVersion@</version>
+        <executions>
+          <execution>
+            <id>attach-sources</id>
+            <goals>
+              <goal>jar-no-fork</goal>
+            </goals>
+          </execution>
+        </executions>
+      </plugin>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-surefire-plugin</artifactId>
+        <version>@mavenSurefirePluginVersion@</version>
+      </plugin>
+    </plugins>
+  </build>
+</project>
diff --git a/src/it/skip-attached-artifacts/verify.groovy 
b/src/it/skip-attached-artifacts/verify.groovy
new file mode 100644
index 0000000..324d511
--- /dev/null
+++ b/src/it/skip-attached-artifacts/verify.groovy
@@ -0,0 +1,41 @@
+/*
+ * 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.
+ */
+
+// the repository URL is relative to the basedir of each module on Maven 3 and 
to the root on Maven 4
+def deployed = []
+basedir.eachFileRecurse( groovy.io.FileType.FILES ) { file ->
+    if ( file.path.replace( '\\', '/' ).contains( '/target/repo/' ) )
+    {
+        deployed << file.name
+    }
+}
+
+assert deployed.contains( 'module1-1.0.pom' )
+assert deployed.contains( 'module1-1.0.jar' )
+assert deployed.contains( 'module2-1.0.pom' )
+assert deployed.contains( 'module2-1.0.jar' )
+
+// module1 holds its sources JAR back; the JAR was built and is still attached
+assert new File( basedir, 'module1/target/module1-1.0-sources.jar' ).isFile()
+assert !deployed.contains( 'module1-1.0-sources.jar' )
+
+// module2, the last module of the build, has no skipAttachedArtifacts and 
deploys its sources JAR
+assert deployed.contains( 'module2-1.0-sources.jar' )
+
+assert new File( basedir, 'build.log' ).text.contains( 'Skipping deployment of 
attached artifact' )
diff --git 
a/src/main/java/org/apache/maven/plugins/deploy/AttachedArtifact.java 
b/src/main/java/org/apache/maven/plugins/deploy/AttachedArtifact.java
new file mode 100644
index 0000000..b00fed0
--- /dev/null
+++ b/src/main/java/org/apache/maven/plugins/deploy/AttachedArtifact.java
@@ -0,0 +1,113 @@
+/*
+ * 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.plugins.deploy;
+
+import org.apache.maven.artifact.Artifact;
+
+/**
+ * Selects attached artifacts of a project by their coordinates. Every element 
that is set must be equal to the
+ * corresponding coordinate of the attached artifact; an element that is not 
set matches any value. At least one
+ * element must be set.
+ *
+ * @since 3.3.0
+ */
+public class AttachedArtifact {
+
+    private String groupId;
+
+    private String artifactId;
+
+    private String classifier;
+
+    private String extension;
+
+    public String getGroupId() {
+        return groupId;
+    }
+
+    public void setGroupId(String groupId) {
+        this.groupId = groupId;
+    }
+
+    public String getArtifactId() {
+        return artifactId;
+    }
+
+    public void setArtifactId(String artifactId) {
+        this.artifactId = artifactId;
+    }
+
+    public String getClassifier() {
+        return classifier;
+    }
+
+    public void setClassifier(String classifier) {
+        this.classifier = classifier;
+    }
+
+    public String getExtension() {
+        return extension;
+    }
+
+    public void setExtension(String extension) {
+        this.extension = extension;
+    }
+
+    /**
+     * @return {@code true} if no element is set, in which case the selector 
would match every attached artifact
+     */
+    boolean isEmpty() {
+        return isBlank(groupId) && isBlank(artifactId) && isBlank(classifier) 
&& isBlank(extension);
+    }
+
+    /**
+     * The consumer POM that Maven 4 attaches to every project is part of the 
POM, not an attachment of the build:
+     * it is never selected.
+     */
+    static boolean isConsumerPom(Artifact artifact) {
+        return "consumer".equals(artifact.getClassifier())
+                && "pom".equals(artifact.getArtifactHandler().getExtension());
+    }
+
+    boolean matches(Artifact artifact) {
+        return !isConsumerPom(artifact)
+                && matches(groupId, artifact.getGroupId())
+                && matches(artifactId, artifact.getArtifactId())
+                && matches(classifier, artifact.getClassifier())
+                && matches(extension, 
artifact.getArtifactHandler().getExtension());
+    }
+
+    private static boolean matches(String expected, String actual) {
+        return isBlank(expected) || expected.trim().equals(actual);
+    }
+
+    private static boolean isBlank(String value) {
+        return value == null || value.trim().isEmpty();
+    }
+
+    @Override
+    public String toString() {
+        return "groupId=" + any(groupId) + ", artifactId=" + any(artifactId) + 
", classifier=" + any(classifier)
+                + ", extension=" + any(extension);
+    }
+
+    private static String any(String value) {
+        return isBlank(value) ? "*" : value.trim();
+    }
+}
diff --git a/src/main/java/org/apache/maven/plugins/deploy/DeployMojo.java 
b/src/main/java/org/apache/maven/plugins/deploy/DeployMojo.java
index 3dadc0d..822e3b6 100644
--- a/src/main/java/org/apache/maven/plugins/deploy/DeployMojo.java
+++ b/src/main/java/org/apache/maven/plugins/deploy/DeployMojo.java
@@ -21,10 +21,13 @@ package org.apache.maven.plugins.deploy;
 import javax.inject.Inject;
 
 import java.io.File;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.LinkedHashMap;
+import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 import java.util.stream.Collectors;
@@ -154,6 +157,30 @@ public class DeployMojo extends AbstractDeployMojo {
     @Parameter(defaultValue = "false", property = "allowIncompleteProjects")
     private boolean allowIncompleteProjects;
 
+    /**
+     * Attached artifacts that must not be deployed, for example a shaded or 
executable JAR that other modules of
+     * the build use but that is of no use in the remote repository. The main 
artifact, the POM and the
+     * consumer POM of Maven 4 are never skipped this way.
+     * <p>
+     * Each entry selects attached artifacts by {@code groupId}, {@code 
artifactId}, {@code classifier} and
+     * {@code extension}. Every element that is set must match; an element 
that is left out matches any value,
+     * and at least one element is required:
+     * <pre>
+     * &lt;skipAttachedArtifacts&gt;
+     *   &lt;attachedArtifact&gt;
+     *     &lt;classifier&gt;shaded&lt;/classifier&gt;
+     *     &lt;extension&gt;jar&lt;/extension&gt;
+     *   &lt;/attachedArtifact&gt;
+     * &lt;/skipAttachedArtifacts&gt;
+     * </pre>
+     * The build fails when an entry matches no attached artifact of the 
project, so that a misspelled entry
+     * cannot publish an artifact that was meant to stay local.
+     *
+     * @since 3.3.0
+     */
+    @Parameter
+    private List<AttachedArtifact> skipAttachedArtifacts;
+
     @Inject
     protected DeployMojo(RuntimeInformation runtimeInformation, 
RepositorySystem repositorySystem) {
         super(runtimeInformation, repositorySystem);
@@ -193,6 +220,9 @@ public class DeployMojo extends AbstractDeployMojo {
     private static final String DEPLOY_ALT_DEPLOYMENT_REPOSITORY =
             DeployMojo.class.getName() + ".altDeploymentRepository";
 
+    private static final String DEPLOY_SKIPPED_ATTACHED_ARTIFACTS =
+            DeployMojo.class.getName() + ".skippedAttachedArtifacts";
+
     private static final String PROJECTS_WITH_DEPLOY_KEY = 
DeployMojo.class.getName() + ".projectsWithDeploy";
 
     private void putState(State state) {
@@ -230,6 +260,9 @@ public class DeployMojo extends AbstractDeployMojo {
         } else {
             failIfOffline();
             warnIfAffectedPackagingAndMaven(project.getPackaging());
+            // resolved here, with the configuration of this project: with 
deployAtEnd the mojo instance of the
+            // last project processes all of them, and would apply its own 
skipAttachedArtifacts to each
+            Set<String> skippedAttachedArtifacts = 
selectSkippedAttachedArtifacts(project);
 
             if (!deployAtEnd) {
 
@@ -241,13 +274,14 @@ public class DeployMojo extends AbstractDeployMojo {
 
                 DeployRequest request = new DeployRequest();
                 request.setRepository(deploymentRepository);
-                processProject(project, request);
+                processProject(project, request, skippedAttachedArtifacts);
                 deploy(request);
                 state = State.DEPLOYED;
             } else {
                 
putPluginContextValue(DEPLOY_ALT_SNAPSHOT_DEPLOYMENT_REPOSITORY, 
altSnapshotDeploymentRepository);
                 
putPluginContextValue(DEPLOY_ALT_RELEASE_DEPLOYMENT_REPOSITORY, 
altReleaseDeploymentRepository);
                 putPluginContextValue(DEPLOY_ALT_DEPLOYMENT_REPOSITORY, 
altDeploymentRepository);
+                getPluginContext().put(DEPLOY_SKIPPED_ATTACHED_ARTIFACTS, 
skippedAttachedArtifacts);
                 state = State.TO_BE_DEPLOYED;
             }
         }
@@ -294,7 +328,13 @@ public class DeployMojo extends AbstractDeployMojo {
                     newRequest.setRepository(repo);
                     return newRequest;
                 });
-                processProject(reactorProject, request);
+                Object skippedAttachedArtifacts = 
pluginContext.get(DEPLOY_SKIPPED_ATTACHED_ARTIFACTS);
+                processProject(
+                        reactorProject,
+                        request,
+                        skippedAttachedArtifacts instanceof Collection
+                                ? (Collection<?>) skippedAttachedArtifacts
+                                : Collections.emptySet());
             }
         }
         // finally execute all deployments request, lets resolver to optimize 
deployment
@@ -357,7 +397,9 @@ public class DeployMojo extends AbstractDeployMojo {
         return false;
     }
 
-    private void processProject(final MavenProject project, DeployRequest 
request) throws MojoExecutionException {
+    private void processProject(
+            final MavenProject project, DeployRequest request, Collection<?> 
skippedAttachedArtifacts)
+            throws MojoExecutionException {
         // always exists, as project exists
         Artifact pomArtifact = RepositoryUtils.toArtifact(new 
ProjectArtifact(project));
         // always exists, but at "init" is w/o file (packaging plugin assigns 
file to this when packaged)
@@ -408,11 +450,49 @@ public class DeployMojo extends AbstractDeployMojo {
         }
 
         for (org.apache.maven.artifact.Artifact attached : 
project.getAttachedArtifacts()) {
+            if (skippedAttachedArtifacts.contains(attached.getId())) {
+                getLog().info("Skipping deployment of attached artifact " + 
attached.getId());
+                continue;
+            }
             getLog().debug("Attaching for deploy: " + attached.getId());
             request.addArtifact(RepositoryUtils.toArtifact(attached));
         }
     }
 
+    /**
+     * Returns the ids of the attached artifacts selected by {@link 
#skipAttachedArtifacts}. Fails on an entry that
+     * selects nothing: a silent no-op would upload an artifact the 
configuration asked to keep out of the
+     * repository.
+     */
+    private Set<String> selectSkippedAttachedArtifacts(MavenProject project) 
throws MojoExecutionException {
+        Set<String> skipped = new LinkedHashSet<>();
+        if (skipAttachedArtifacts == null) {
+            return skipped;
+        }
+        for (AttachedArtifact selector : skipAttachedArtifacts) {
+            if (selector == null || selector.isEmpty()) {
+                throw new MojoExecutionException("An entry of 
skipAttachedArtifacts is empty: set at least one of"
+                        + " 'groupId', 'artifactId', 'classifier' or 
'extension'.");
+            }
+            boolean matched = false;
+            for (org.apache.maven.artifact.Artifact attached : 
project.getAttachedArtifacts()) {
+                if (selector.matches(attached)) {
+                    matched = true;
+                    skipped.add(attached.getId());
+                }
+            }
+            if (!matched) {
+                throw new MojoExecutionException("The skipAttachedArtifacts 
entry [" + selector + "] matches no"
+                        + " attached artifact of " + project.getGroupId() + 
":" + project.getArtifactId() + ":"
+                        + project.getVersion() + "; attached artifacts: "
+                        + project.getAttachedArtifacts().stream()
+                                .filter(a -> 
!AttachedArtifact.isConsumerPom(a))
+                                .collect(Collectors.toList()));
+            }
+        }
+        return skipped;
+    }
+
     private boolean isFile(File file) {
         return file != null && file.isFile();
     }
diff --git a/src/site/fml/faq.fml b/src/site/fml/faq.fml
index 532cb5a..847ef4e 100644
--- a/src/site/fml/faq.fml
+++ b/src/site/fml/faq.fml
@@ -77,6 +77,40 @@ under the License.
 </plugin>]]></source>
        </answer>
      </faq>
+     <faq id="skipAttachedArtifacts">
+       <question>I don't want to deploy one of the attached artifacts of a 
module. Can I skip it?</question>
+       <answer>
+         <p>
+           Yes. An attached artifact, for example a shaded JAR that other 
modules of the build use but that is of no
+           use in the remote repository, stays out of the deployment when you 
select it with
+           <code>skipAttachedArtifacts</code>:
+         </p>
+         <source>
+<![CDATA[<plugin>
+  <groupId>org.apache.maven.plugins</groupId>
+  <artifactId>maven-deploy-plugin</artifactId>
+  <version>X.Y</version>
+  <configuration>
+    <skipAttachedArtifacts>
+      <attachedArtifact>
+        <classifier>shaded</classifier>
+        <extension>jar</extension>
+      </attachedArtifact>
+    </skipAttachedArtifacts>
+  </configuration>
+</plugin>]]></source>
+         <p>
+           An entry selects by <code>groupId</code>, <code>artifactId</code>, 
<code>classifier</code> and
+           <code>extension</code>. An element you leave out matches any value, 
and at least one element is required.
+           The main artifact and the POM, including the consumer POM of Maven 
4, are always deployed, and the
+           artifact is still installed in the local repository.
+         </p>
+         <p>
+           The build fails when an entry matches no attached artifact of the 
project, so configure the parameter in
+           the module that attaches the artifact rather than in a parent POM.
+         </p>
+       </answer>
+     </faq>
      <faq id="deploy_deploy">
        <question>What does the message "The packaging for this project did not 
assign a file to the build artifact" mean when I run 
<code>deploy:deploy</code>?</question>
        <answer>
diff --git a/src/test/java/org/apache/maven/plugins/deploy/DeployMojoTest.java 
b/src/test/java/org/apache/maven/plugins/deploy/DeployMojoTest.java
index 33b0155..98a4917 100644
--- a/src/test/java/org/apache/maven/plugins/deploy/DeployMojoTest.java
+++ b/src/test/java/org/apache/maven/plugins/deploy/DeployMojoTest.java
@@ -57,6 +57,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
 import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 import static org.junit.jupiter.api.Assertions.fail;
 import static org.mockito.Mockito.when;
@@ -391,6 +392,128 @@ class DeployMojoTest {
         assertEquals(0, getSizeOfExpectedFiles(fileList, expectedFiles));
     }
 
+    @Test
+    @Basedir("/unit/deploy-skip-attached-artifacts")
+    @InjectMojo(goal = "deploy", pom = "plugin-config.xml")
+    void testSkippedAttachedArtifactsAreNotDeployed(DeployMojo mojo) throws 
Exception {
+        mojo.setPluginContext(new ConcurrentHashMap<>());
+        mavenProject.setVersion("1.0");
+        setProjectArtifact(mavenProject, "jar");
+        attachArtifact("jar", "shaded");
+        attachArtifact("zip", null);
+
+        mojo.execute();
+
+        assertDeployed(
+                remoteRepo,
+                GA_PATH + "1.0/maven-deploy-test-1.0.pom",
+                GA_PATH + "1.0/maven-deploy-test-1.0.jar",
+                GA_PATH + "1.0/maven-deploy-test-1.0.zip");
+        assertFalse(new File(remoteRepo, GA_PATH + 
"1.0/maven-deploy-test-1.0-shaded.jar").exists());
+        // the artifact stays attached to the project: only this deployment 
leaves it out
+        assertEquals(2, mavenProject.getAttachedArtifacts().size());
+    }
+
+    @Test
+    @InjectMojo(goal = "deploy")
+    void testSkippedAttachedArtifactElementLeftOutMatchesAnyValue(DeployMojo 
mojo) throws Exception {
+        mojo.setPluginContext(new ConcurrentHashMap<>());
+        mavenProject.setVersion("1.0");
+        setProjectArtifact(mavenProject, "jar");
+        attachArtifact("jar", "shaded");
+        attachArtifact("zip", null);
+        AttachedArtifact all = new AttachedArtifact();
+        all.setArtifactId("maven-deploy-test");
+        // a second entry selecting an artifact that an earlier entry already 
selected is not an error
+        AttachedArtifact zip = new AttachedArtifact();
+        zip.setExtension("zip");
+        setVariableValueToObject(mojo, "skipAttachedArtifacts", 
Arrays.asList(all, zip));
+
+        mojo.execute();
+
+        // the main artifact has the same artifactId and the extension jar, 
and is deployed all the same
+        assertDeployed(
+                remoteRepo, GA_PATH + "1.0/maven-deploy-test-1.0.pom", GA_PATH 
+ "1.0/maven-deploy-test-1.0.jar");
+        assertFalse(new File(remoteRepo, GA_PATH + 
"1.0/maven-deploy-test-1.0-shaded.jar").exists());
+        assertFalse(new File(remoteRepo, GA_PATH + 
"1.0/maven-deploy-test-1.0.zip").exists());
+    }
+
+    @Test
+    @InjectMojo(goal = "deploy")
+    void testSkippedAttachedArtifactMatchingNothingFailsTheBuild(DeployMojo 
mojo) throws Exception {
+        mojo.setPluginContext(new ConcurrentHashMap<>());
+        setProjectArtifact(mavenProject, "jar");
+        attachArtifact("jar", "shaded");
+        AttachedArtifact misspelled = new AttachedArtifact();
+        misspelled.setClassifier("shadded");
+        setVariableValueToObject(mojo, "skipAttachedArtifacts", 
Collections.singletonList(misspelled));
+
+        MojoExecutionException e = assertThrows(MojoExecutionException.class, 
mojo::execute);
+
+        assertTrue(e.getMessage().contains("classifier=shadded"), 
e.getMessage());
+        assertTrue(e.getMessage().contains("matches no attached artifact"), 
e.getMessage());
+        assertFalse(remoteRepo.exists());
+    }
+
+    @Test
+    @InjectMojo(goal = "deploy")
+    void testEmptySkippedAttachedArtifactFailsTheBuild(DeployMojo mojo) throws 
Exception {
+        mojo.setPluginContext(new ConcurrentHashMap<>());
+        setProjectArtifact(mavenProject, "jar");
+        attachArtifact("jar", "shaded");
+        setVariableValueToObject(mojo, "skipAttachedArtifacts", 
Collections.singletonList(new AttachedArtifact()));
+
+        MojoExecutionException e = assertThrows(MojoExecutionException.class, 
mojo::execute);
+
+        assertTrue(e.getMessage().contains("is empty"), e.getMessage());
+    }
+
+    @Test
+    @InjectMojo(goal = "deploy")
+    void testSkippedAttachedArtifactsNeverSelectTheConsumerPom(DeployMojo 
mojo) throws Exception {
+        mojo.setPluginContext(new ConcurrentHashMap<>());
+        mavenProject.setVersion("1.0");
+        setProjectArtifact(mavenProject, "jar");
+        attachArtifact("jar", "shaded");
+        // Maven 4 attaches the consumer POM to every project
+        attachArtifact("pom", "consumer");
+        AttachedArtifact all = new AttachedArtifact();
+        all.setArtifactId("maven-deploy-test");
+        setVariableValueToObject(mojo, "skipAttachedArtifacts", 
Collections.singletonList(all));
+
+        mojo.execute();
+
+        assertDeployed(
+                remoteRepo,
+                GA_PATH + "1.0/maven-deploy-test-1.0.pom",
+                GA_PATH + "1.0/maven-deploy-test-1.0.jar",
+                GA_PATH + "1.0/maven-deploy-test-1.0-consumer.pom");
+        assertFalse(new File(remoteRepo, GA_PATH + 
"1.0/maven-deploy-test-1.0-shaded.jar").exists());
+    }
+
+    @Test
+    @InjectMojo(goal = "deploy")
+    void 
testSkippedAttachedArtifactSelectingOnlyTheConsumerPomFailsTheBuild(DeployMojo 
mojo) throws Exception {
+        mojo.setPluginContext(new ConcurrentHashMap<>());
+        setProjectArtifact(mavenProject, "jar");
+        attachArtifact("pom", "consumer");
+        AttachedArtifact consumer = new AttachedArtifact();
+        consumer.setClassifier("consumer");
+        setVariableValueToObject(mojo, "skipAttachedArtifacts", 
Collections.singletonList(consumer));
+
+        MojoExecutionException e = assertThrows(MojoExecutionException.class, 
mojo::execute);
+
+        assertTrue(e.getMessage().contains("matches no attached artifact"), 
e.getMessage());
+    }
+
+    private void attachArtifact(String type, String classifier) throws 
IOException {
+        mavenProjectHelper.attachArtifact(
+                mavenProject,
+                type,
+                classifier,
+                Files.createTempFile(tempDir, "test-attached", type).toFile());
+    }
+
     @Test
     @InjectMojo(goal = "deploy")
     void testNonPomDeployWithAttachedArtifactsOnly(DeployMojo mojo) throws 
Exception {
diff --git 
a/src/test/resources/unit/deploy-skip-attached-artifacts/plugin-config.xml 
b/src/test/resources/unit/deploy-skip-attached-artifacts/plugin-config.xml
new file mode 100644
index 0000000..38b1d58
--- /dev/null
+++ b/src/test/resources/unit/deploy-skip-attached-artifacts/plugin-config.xml
@@ -0,0 +1,36 @@
+<!--
+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>
+  <build>
+    <plugins>
+      <plugin>
+        <artifactId>maven-deploy-plugin</artifactId>
+        <configuration>
+          <skipAttachedArtifacts>
+            <attachedArtifact>
+              <classifier>shaded</classifier>
+              <extension>jar</extension>
+            </attachedArtifact>
+          </skipAttachedArtifacts>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+</project>

Reply via email to