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-shade-plugin.git
The following commit(s) were added to refs/heads/master by this push:
new c2705d6 [MSHADE-456] Remove test-jar dependencies from the dependency
reduced POM
c2705d6 is described below
commit c2705d60f0a0c756941464051cc0cf4de4f39772
Author: Arturo Bernal <[email protected]>
AuthorDate: Thu Oct 29 08:50:35 2026 +0100
[MSHADE-456] Remove test-jar dependencies from the dependency reduced POM
A dependency declared with <type>test-jar</type> has no classifier in
the POM, but Maven resolves it to classifier "tests" once it becomes
an Artifact (DefaultArtifact falls back to the artifact handler's
classifier when none is given). ShadeMojo built the comparison id for
a Dependency straight from its (empty) classifier, so it never matched
the id of the shaded artifact and the dependency survived in
dependency-reduced-pom.xml.
Resolve the dependency's effective classifier through the injected
ArtifactHandlerManager before building the id, the same way Maven
itself derives it for the resolved artifact.
Claude-Session: https://claude.ai/code/session_0153qyh22xBMN1vPfWGz3jSH
---
.../a-0.1-tests.jar/a-tests.properties | 16 +++++
.../repository/dep-reduced-pom-test-jar/a-0.1.pom | 36 +++++++++++
.../dep-reduced-pom-test-jar/invoker.properties | 18 ++++++
src/it/projects/dep-reduced-pom-test-jar/pom.xml | 67 ++++++++++++++++++++
.../dep-reduced-pom-test-jar/verify.groovy | 28 +++++++++
.../apache/maven/plugins/shade/mojo/ShadeMojo.java | 32 +++++++++-
.../maven/plugins/shade/mojo/ShadeMojoTest.java | 72 ++++++++++++++++++++++
7 files changed, 268 insertions(+), 1 deletion(-)
diff --git
a/src/it/mrm/repository/dep-reduced-pom-test-jar/a-0.1-tests.jar/a-tests.properties
b/src/it/mrm/repository/dep-reduced-pom-test-jar/a-0.1-tests.jar/a-tests.properties
new file mode 100644
index 0000000..13a8339
--- /dev/null
+++
b/src/it/mrm/repository/dep-reduced-pom-test-jar/a-0.1-tests.jar/a-tests.properties
@@ -0,0 +1,16 @@
+# 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.
diff --git a/src/it/mrm/repository/dep-reduced-pom-test-jar/a-0.1.pom
b/src/it/mrm/repository/dep-reduced-pom-test-jar/a-0.1.pom
new file mode 100644
index 0000000..308f821
--- /dev/null
+++ b/src/it/mrm/repository/dep-reduced-pom-test-jar/a-0.1.pom
@@ -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.shade.drptj</groupId>
+ <artifactId>a</artifactId>
+ <version>0.1</version>
+ <packaging>jar</packaging>
+
+ <distributionManagement>
+ <repository>
+ <id>maven-core-it</id>
+ <url>file:///${basedir}/repo</url>
+ </repository>
+ </distributionManagement>
+</project>
diff --git a/src/it/projects/dep-reduced-pom-test-jar/invoker.properties
b/src/it/projects/dep-reduced-pom-test-jar/invoker.properties
new file mode 100644
index 0000000..5330830
--- /dev/null
+++ b/src/it/projects/dep-reduced-pom-test-jar/invoker.properties
@@ -0,0 +1,18 @@
+# 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.
+
+invoker.timeoutInSeconds=60
diff --git a/src/it/projects/dep-reduced-pom-test-jar/pom.xml
b/src/it/projects/dep-reduced-pom-test-jar/pom.xml
new file mode 100644
index 0000000..f61642a
--- /dev/null
+++ b/src/it/projects/dep-reduced-pom-test-jar/pom.xml
@@ -0,0 +1,67 @@
+<?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.shade.drptj</groupId>
+ <artifactId>test</artifactId>
+ <version>1.0</version>
+
+ <name>MSHADE-456</name>
+ <description>
+ Test that a dependency declared with type test-jar is removed from the
dependency reduced
+ POM once it has been shaded into the uber JAR, even though the POM
declares no classifier
+ for it.
+ </description>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.maven.its.shade.drptj</groupId>
+ <artifactId>a</artifactId>
+ <version>0.1</version>
+ <type>test-jar</type>
+ </dependency>
+ </dependencies>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-shade-plugin</artifactId>
+ <version>@project.version@</version>
+ <executions>
+ <execution>
+ <id>attach-shade</id>
+ <goals>
+ <goal>shade</goal>
+ </goals>
+ <configuration>
+ <shadedArtifactAttached>false</shadedArtifactAttached>
+ <createDependencyReducedPom>true</createDependencyReducedPom>
+
<dependencyReducedPomLocation>target/dependency-reduced-pom.xml</dependencyReducedPomLocation>
+ </configuration>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
+</project>
diff --git a/src/it/projects/dep-reduced-pom-test-jar/verify.groovy
b/src/it/projects/dep-reduced-pom-test-jar/verify.groovy
new file mode 100644
index 0000000..dfdcfde
--- /dev/null
+++ b/src/it/projects/dep-reduced-pom-test-jar/verify.groovy
@@ -0,0 +1,28 @@
+/*
+ * 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.
+ */
+import groovy.xml.XmlParser
+
+File pomFile = new File( basedir, "target/dependency-reduced-pom.xml" )
+assert pomFile.isFile()
+
+def ns = new groovy.xml.Namespace("http://maven.apache.org/POM/4.0.0")
+def pom = new XmlParser().parse( pomFile )
+
+assert pom[ns.modelVersion].size() == 1
+assert pom[ns.dependencies][ns.dependency].size() == 0
diff --git a/src/main/java/org/apache/maven/plugins/shade/mojo/ShadeMojo.java
b/src/main/java/org/apache/maven/plugins/shade/mojo/ShadeMojo.java
index c54bb22..707e354 100644
--- a/src/main/java/org/apache/maven/plugins/shade/mojo/ShadeMojo.java
+++ b/src/main/java/org/apache/maven/plugins/shade/mojo/ShadeMojo.java
@@ -44,6 +44,8 @@ import java.util.stream.Collectors;
import org.apache.maven.RepositoryUtils;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.DefaultArtifact;
+import org.apache.maven.artifact.handler.ArtifactHandler;
+import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
import org.apache.maven.artifact.versioning.VersionRange;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Dependency;
@@ -491,6 +493,14 @@ public class ShadeMojo extends AbstractMojo {
@Inject
private RepositorySystem repositorySystem;
+ /**
+ * Used to look up the default classifier Maven associates with a
dependency type (for example
+ * {@code test-jar} implies classifier {@code tests}), so that the id
computed for a POM-declared
+ * dependency matches the id computed for the corresponding resolved
artifact.
+ */
+ @Inject
+ private ArtifactHandlerManager artifactHandlerManager;
+
/**
* ProjectBuilder, needed to create projects from the artifacts.
*/
@@ -1529,13 +1539,33 @@ public class ShadeMojo extends AbstractMojo {
private String getId(Dependency dependency) {
return getId(
- dependency.getGroupId(), dependency.getArtifactId(),
dependency.getType(), dependency.getClassifier());
+ dependency.getGroupId(),
+ dependency.getArtifactId(),
+ dependency.getType(),
+ getEffectiveClassifier(dependency));
}
private String getId(String groupId, String artifactId, String type,
String classifier) {
return groupId + ":" + artifactId + ":" + type + ":" + ((classifier !=
null) ? classifier : "");
}
+ /**
+ * Returns the classifier a {@link Dependency} resolves to once Maven
turns it into an {@link Artifact}.
+ * A POM typically leaves the classifier element out and relies on the
artifact handler registered for
+ * the dependency's type to supply it, for example type {@code test-jar}
implies classifier
+ * {@code tests}. Without this, an id built straight from the {@code
Dependency} would never match the
+ * id built from the resolved {@code Artifact} in {@link
#getId(Artifact)}, and a shaded test-jar
+ * dependency would never be recognized as removable from the dependency
reduced POM.
+ */
+ private String getEffectiveClassifier(Dependency dependency) {
+ String classifier = dependency.getClassifier();
+ if (classifier != null && !classifier.isEmpty()) {
+ return classifier;
+ }
+ ArtifactHandler handler =
artifactHandlerManager.getArtifactHandler(dependency.getType());
+ return handler.getClassifier();
+ }
+
public boolean updateExcludesInDeps(
MavenProject project, List<Dependency> dependencies,
List<Dependency> transitiveDeps)
throws DependencyCollectionException {
diff --git
a/src/test/java/org/apache/maven/plugins/shade/mojo/ShadeMojoTest.java
b/src/test/java/org/apache/maven/plugins/shade/mojo/ShadeMojoTest.java
index e9295ec..0b2126a 100644
--- a/src/test/java/org/apache/maven/plugins/shade/mojo/ShadeMojoTest.java
+++ b/src/test/java/org/apache/maven/plugins/shade/mojo/ShadeMojoTest.java
@@ -40,10 +40,12 @@ import org.apache.maven.api.plugin.testing.MojoTest;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.DefaultArtifact;
import org.apache.maven.artifact.handler.ArtifactHandler;
+import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
import org.apache.maven.artifact.versioning.VersionRange;
import org.apache.maven.execution.MavenExecutionRequest;
import org.apache.maven.execution.MavenExecutionResult;
import org.apache.maven.execution.MavenSession;
+import org.apache.maven.model.Dependency;
import org.apache.maven.plugins.shade.ModuleInfoConfiguration;
import org.apache.maven.plugins.shade.ModuleInfoMode;
import org.apache.maven.plugins.shade.ShadeRequest;
@@ -99,6 +101,9 @@ class ShadeMojoTest {
@Inject
private MavenProject project;
+ @Inject
+ private ArtifactHandlerManager artifactHandlerManager;
+
@Test
void testManifestTransformerSelection() throws Exception {
final ShadeMojo mojo = new ShadeMojo();
@@ -200,6 +205,73 @@ class ShadeMojoTest {
}
}
+ /**
+ * MSHADE-456: a POM-declared {@code <dependency>} of type {@code
test-jar} has no classifier set
+ * explicitly, but resolves to classifier {@code tests} once Maven turns
it into an {@link Artifact}
+ * (see {@link DefaultArtifact}, which falls back to {@link
ArtifactHandler#getClassifier()} when
+ * constructed with a {@code null} classifier). The id computed from the
{@code Dependency} must match
+ * the id computed from the resolved {@code Artifact}, otherwise a shaded
test-jar dependency is never
+ * removed from the dependency reduced POM.
+ */
+ @Test
+ void testGetIdForTestJarDependencyMatchesResolvedArtifact() throws
Exception {
+ final ShadeMojo mojo = new ShadeMojo();
+ setVariableValueToObject(mojo, "artifactHandlerManager",
artifactHandlerManager);
+
+ final Dependency dependency = new Dependency();
+ dependency.setGroupId("org.apache.maven.its");
+ dependency.setArtifactId("foo");
+ dependency.setVersion("1.0");
+ dependency.setType("test-jar");
+ // no classifier set here, exactly as a user would write it in the POM
+
+ final Method getIdForDependency =
ShadeMojo.class.getDeclaredMethod("getId", Dependency.class);
+ getIdForDependency.setAccessible(true);
+ final String dependencyId = (String) getIdForDependency.invoke(mojo,
dependency);
+
+ assertEquals("org.apache.maven.its:foo:test-jar:tests", dependencyId);
+
+ final ArtifactHandler testJarHandler =
container.lookup(ArtifactHandler.class, "test-jar");
+ final Artifact artifact = new DefaultArtifact(
+ "org.apache.maven.its",
+ "foo",
+ VersionRange.createFromVersion("1.0"),
+ "compile",
+ "test-jar",
+ null,
+ testJarHandler);
+
+ final Method getIdForArtifact =
ShadeMojo.class.getDeclaredMethod("getId", Artifact.class);
+ getIdForArtifact.setAccessible(true);
+ final String artifactId = (String) getIdForArtifact.invoke(mojo,
artifact);
+
+ assertEquals(dependencyId, artifactId);
+ }
+
+ /**
+ * A dependency with an ordinary type keeps relying on its explicit
(possibly absent) classifier,
+ * unaffected by the MSHADE-456 fix for type {@code test-jar}.
+ */
+ @Test
+ void testGetIdForRegularDependencyKeepsExplicitClassifier() throws
Exception {
+ final ShadeMojo mojo = new ShadeMojo();
+ setVariableValueToObject(mojo, "artifactHandlerManager",
artifactHandlerManager);
+
+ final Dependency dependency = new Dependency();
+ dependency.setGroupId("org.apache.maven.its");
+ dependency.setArtifactId("bar");
+ dependency.setVersion("1.0");
+ dependency.setType("jar");
+
+ final Method getIdForDependency =
ShadeMojo.class.getDeclaredMethod("getId", Dependency.class);
+ getIdForDependency.setAccessible(true);
+
+ assertEquals("org.apache.maven.its:bar:jar:",
getIdForDependency.invoke(mojo, dependency));
+
+ dependency.setClassifier("linux");
+ assertEquals("org.apache.maven.its:bar:jar:linux",
getIdForDependency.invoke(mojo, dependency));
+ }
+
@Test
void testShaderWithDefaultShadedPattern() throws Exception {
shaderWithPattern(null, new File("target/foo-default.jar"));