From b714c2ffa44f1c2f87f6f50eb00fe40f5a513552 Mon Sep 17 00:00:00 2001 From: Thomas Rebele Date: Thu, 13 Nov 2025 14:25:53 +0100 Subject: [PATCH] [MSHADE-374] Exclude test shading when shadeTestJar is enabled Apply the artifact set to sources, tests, and test-sources as well. --- https://issues.apache.org/jira/browse/MSHADE-374 --- .../MSHADE-374_testJarArtifactSet/api/pom.xml | 32 ++++++++ .../api/src/main/java/Api.java | 22 +++++ .../api/src/main/resources/api-resource.txt | 16 ++++ .../api/src/test/java/ApiTest.java | 25 ++++++ .../src/test/resources/api-test-resource.txt | 16 ++++ .../impl/pom.xml | 43 ++++++++++ .../impl/src/main/java/Impl.java | 22 +++++ .../impl/src/main/resources/impl-resource.txt | 16 ++++ .../impl/src/test/java/ImplTest.java | 25 ++++++ .../src/test/resources/impl-test-resource.txt | 16 ++++ .../MSHADE-374_testJarArtifactSet/pom.xml | 77 ++++++++++++++++++ .../uber/pom.xml | 79 ++++++++++++++++++ .../verify.groovy | 53 ++++++++++++ .../maven/plugins/shade/mojo/ShadeMojo.java | 81 ++++++++++++------- .../plugins/shade/mojo/ShadeMojoTest.java | 5 +- 15 files changed, 497 insertions(+), 31 deletions(-) create mode 100644 src/it/projects/MSHADE-374_testJarArtifactSet/api/pom.xml create mode 100644 src/it/projects/MSHADE-374_testJarArtifactSet/api/src/main/java/Api.java create mode 100644 src/it/projects/MSHADE-374_testJarArtifactSet/api/src/main/resources/api-resource.txt create mode 100644 src/it/projects/MSHADE-374_testJarArtifactSet/api/src/test/java/ApiTest.java create mode 100644 src/it/projects/MSHADE-374_testJarArtifactSet/api/src/test/resources/api-test-resource.txt create mode 100644 src/it/projects/MSHADE-374_testJarArtifactSet/impl/pom.xml create mode 100644 src/it/projects/MSHADE-374_testJarArtifactSet/impl/src/main/java/Impl.java create mode 100644 src/it/projects/MSHADE-374_testJarArtifactSet/impl/src/main/resources/impl-resource.txt create mode 100644 src/it/projects/MSHADE-374_testJarArtifactSet/impl/src/test/java/ImplTest.java create mode 100644 src/it/projects/MSHADE-374_testJarArtifactSet/impl/src/test/resources/impl-test-resource.txt create mode 100644 src/it/projects/MSHADE-374_testJarArtifactSet/pom.xml create mode 100644 src/it/projects/MSHADE-374_testJarArtifactSet/uber/pom.xml create mode 100644 src/it/projects/MSHADE-374_testJarArtifactSet/verify.groovy diff --git a/src/it/projects/MSHADE-374_testJarArtifactSet/api/pom.xml b/src/it/projects/MSHADE-374_testJarArtifactSet/api/pom.xml new file mode 100644 index 00000000..b0242680 --- /dev/null +++ b/src/it/projects/MSHADE-374_testJarArtifactSet/api/pom.xml @@ -0,0 +1,32 @@ + + + + + + 4.0.0 + + org.apache.maven.its.shade.stj + mshade-374-parent + 1.0 + + mshade-374-api + diff --git a/src/it/projects/MSHADE-374_testJarArtifactSet/api/src/main/java/Api.java b/src/it/projects/MSHADE-374_testJarArtifactSet/api/src/main/java/Api.java new file mode 100644 index 00000000..b1a0ba74 --- /dev/null +++ b/src/it/projects/MSHADE-374_testJarArtifactSet/api/src/main/java/Api.java @@ -0,0 +1,22 @@ +/* + * 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. + */ + +public class Api +{ +} diff --git a/src/it/projects/MSHADE-374_testJarArtifactSet/api/src/main/resources/api-resource.txt b/src/it/projects/MSHADE-374_testJarArtifactSet/api/src/main/resources/api-resource.txt new file mode 100644 index 00000000..13a83393 --- /dev/null +++ b/src/it/projects/MSHADE-374_testJarArtifactSet/api/src/main/resources/api-resource.txt @@ -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/projects/MSHADE-374_testJarArtifactSet/api/src/test/java/ApiTest.java b/src/it/projects/MSHADE-374_testJarArtifactSet/api/src/test/java/ApiTest.java new file mode 100644 index 00000000..2e2eaab2 --- /dev/null +++ b/src/it/projects/MSHADE-374_testJarArtifactSet/api/src/test/java/ApiTest.java @@ -0,0 +1,25 @@ +/* + * 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. + */ + +public class ApiTest +{ + public static void main(String[] args) { + new Api(); + } +} diff --git a/src/it/projects/MSHADE-374_testJarArtifactSet/api/src/test/resources/api-test-resource.txt b/src/it/projects/MSHADE-374_testJarArtifactSet/api/src/test/resources/api-test-resource.txt new file mode 100644 index 00000000..13a83393 --- /dev/null +++ b/src/it/projects/MSHADE-374_testJarArtifactSet/api/src/test/resources/api-test-resource.txt @@ -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/projects/MSHADE-374_testJarArtifactSet/impl/pom.xml b/src/it/projects/MSHADE-374_testJarArtifactSet/impl/pom.xml new file mode 100644 index 00000000..c126e6c2 --- /dev/null +++ b/src/it/projects/MSHADE-374_testJarArtifactSet/impl/pom.xml @@ -0,0 +1,43 @@ + + + + + + 4.0.0 + + org.apache.maven.its.shade.stj + mshade-374-parent + 1.0 + + mshade-374-impl + + + org.apache.maven.its.shade.stj + mshade-374-api + + + org.apache.maven.its.shade.stj + mshade-374-api + test-jar + + + diff --git a/src/it/projects/MSHADE-374_testJarArtifactSet/impl/src/main/java/Impl.java b/src/it/projects/MSHADE-374_testJarArtifactSet/impl/src/main/java/Impl.java new file mode 100644 index 00000000..737fb97c --- /dev/null +++ b/src/it/projects/MSHADE-374_testJarArtifactSet/impl/src/main/java/Impl.java @@ -0,0 +1,22 @@ +/* + * 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. + */ + +public class Impl extends Api +{ +} diff --git a/src/it/projects/MSHADE-374_testJarArtifactSet/impl/src/main/resources/impl-resource.txt b/src/it/projects/MSHADE-374_testJarArtifactSet/impl/src/main/resources/impl-resource.txt new file mode 100644 index 00000000..13a83393 --- /dev/null +++ b/src/it/projects/MSHADE-374_testJarArtifactSet/impl/src/main/resources/impl-resource.txt @@ -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/projects/MSHADE-374_testJarArtifactSet/impl/src/test/java/ImplTest.java b/src/it/projects/MSHADE-374_testJarArtifactSet/impl/src/test/java/ImplTest.java new file mode 100644 index 00000000..f335ad5c --- /dev/null +++ b/src/it/projects/MSHADE-374_testJarArtifactSet/impl/src/test/java/ImplTest.java @@ -0,0 +1,25 @@ +/* + * 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. + */ + +public class ImplTest extends ApiTest +{ + public static void main(String[] args) { + new Impl(); + } +} diff --git a/src/it/projects/MSHADE-374_testJarArtifactSet/impl/src/test/resources/impl-test-resource.txt b/src/it/projects/MSHADE-374_testJarArtifactSet/impl/src/test/resources/impl-test-resource.txt new file mode 100644 index 00000000..13a83393 --- /dev/null +++ b/src/it/projects/MSHADE-374_testJarArtifactSet/impl/src/test/resources/impl-test-resource.txt @@ -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/projects/MSHADE-374_testJarArtifactSet/pom.xml b/src/it/projects/MSHADE-374_testJarArtifactSet/pom.xml new file mode 100644 index 00000000..d4a6e085 --- /dev/null +++ b/src/it/projects/MSHADE-374_testJarArtifactSet/pom.xml @@ -0,0 +1,77 @@ + + + + + + 4.0.0 + org.apache.maven.its.shade.stj + mshade-374-parent + pom + 1.0 + + api + impl + uber + + + + + org.apache.maven.its.shade.stj + mshade-374-api + 1.0 + + + org.apache.maven.its.shade.stj + mshade-374-api + 1.0 + test-jar + test + + + org.apache.maven.its.shade.stj + mshade-374-impl + 1.0 + + + org.apache.maven.its.shade.stj + mshade-374-impl + 1.0 + test-jar + test + + + + + + + maven-jar-plugin + + + + test-jar + + + + + + + diff --git a/src/it/projects/MSHADE-374_testJarArtifactSet/uber/pom.xml b/src/it/projects/MSHADE-374_testJarArtifactSet/uber/pom.xml new file mode 100644 index 00000000..573127a4 --- /dev/null +++ b/src/it/projects/MSHADE-374_testJarArtifactSet/uber/pom.xml @@ -0,0 +1,79 @@ + + + + + + 4.0.0 + + org.apache.maven.its.shade.stj + mshade-374-parent + 1.0 + + mshade-374-uber + + + org.apache.maven.its.shade.stj + mshade-374-api + + + org.apache.maven.its.shade.stj + mshade-374-api + test-jar + + + org.apache.maven.its.shade.stj + mshade-374-impl + + + org.apache.maven.its.shade.stj + mshade-374-impl + test-jar + + + + + + maven-shade-plugin + @project.version@ + + + + shade + + + true + + + org.apache.maven.its.shade.stj:mshade-374-impl:* + org.apache.maven.its.shade.stj:mshade-374-api:* + + + org.apache.maven.its.shade.stj:mshade-374-impl:*:tests + + + + + + + + + diff --git a/src/it/projects/MSHADE-374_testJarArtifactSet/verify.groovy b/src/it/projects/MSHADE-374_testJarArtifactSet/verify.groovy new file mode 100644 index 00000000..bf96066a --- /dev/null +++ b/src/it/projects/MSHADE-374_testJarArtifactSet/verify.groovy @@ -0,0 +1,53 @@ +/* + * 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. + */ + +def jarFile = new java.util.jar.JarFile( new File( basedir, "uber/target/mshade-374-uber-1.0.jar" ) ) +try +{ + // modified to get an exception + assert null != jarFile.getJarEntry( "Api.class" ) + assert null != jarFile.getJarEntry( "api-resource.txt" ) + assert null != jarFile.getJarEntry( "Impl.class" ) + assert null != jarFile.getJarEntry( "impl-resource.txt" ) + assert null == jarFile.getJarEntry( "ApiTest.class" ) + assert null == jarFile.getJarEntry( "api-test-resource.txt" ) + assert null == jarFile.getJarEntry( "ImplTest.class" ) + assert null == jarFile.getJarEntry( "impl-test-resource.txt" ) +} +finally +{ + jarFile.close() +} + +def testJarFile = new java.util.jar.JarFile( new File( basedir, "uber/target/mshade-374-uber-1.0-tests.jar" ) ) +try +{ + assert null == testJarFile.getJarEntry( "Api.class" ) + assert null == testJarFile.getJarEntry( "api-resource.txt" ) + assert null == testJarFile.getJarEntry( "Impl.class" ) + assert null == testJarFile.getJarEntry( "impl-resource.txt" ) + assert null != testJarFile.getJarEntry( "ApiTest.class" ) + assert null != testJarFile.getJarEntry( "api-test-resource.txt" ) + assert null == testJarFile.getJarEntry( "ImplTest.class" ) + assert null == testJarFile.getJarEntry( "impl-test-resource.txt" ) +} +finally +{ + testJarFile.close() +} 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 4ff74fa3..ba5f0b5c 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 @@ -535,7 +535,7 @@ public void execute() throws MojoExecutionException { // Now add our extra resources try { - List filters = getFilters(processedArtifacts); + List filters = getFilters(processedArtifacts, artifactSelector); List relocators = getRelocators(); @@ -790,36 +790,48 @@ private List processArtifactSelectors( artifactIds.add(getId(artifact)); if (createSourcesJar) { - Artifact sources = resolveArtifactForClassifier(artifact, "sources"); - if (sources != null) { - if (sources.getFile().length() > 0) { - sourceArtifacts.add(sources.getFile()); - processedArtifacts.add(sources); - } else { - emptySourceArtifacts.add(artifact); + ArtifactId sourcesArtifactId = + new ArtifactId(artifact.getGroupId(), artifact.getArtifactId(), artifact.getType(), "sources"); + if (artifactSelector.isSelected(sourcesArtifactId)) { + Artifact sources = resolveArtifactForClassifier(artifact, "sources"); + if (sources != null) { + if (sources.getFile().length() > 0) { + sourceArtifacts.add(sources.getFile()); + processedArtifacts.add(sources); + } else { + emptySourceArtifacts.add(artifact); + } } } } if (shadeTestJar) { - Artifact tests = resolveArtifactForClassifier(artifact, "tests"); - if (tests != null) { - if (tests.getFile().length() > 0) { - testArtifacts.add(tests.getFile()); - processedArtifacts.add(tests); - } else { - emptyTestArtifacts.add(artifact); + ArtifactId testArtifactId = + new ArtifactId(artifact.getGroupId(), artifact.getArtifactId(), artifact.getType(), "tests"); + if (artifactSelector.isSelected(testArtifactId)) { + Artifact tests = resolveArtifactForClassifier(artifact, "tests"); + if (tests != null) { + if (tests.getFile().length() > 0) { + testArtifacts.add(tests.getFile()); + processedArtifacts.add(tests); + } else { + emptyTestArtifacts.add(artifact); + } } } } if (createTestSourcesJar) { - Artifact testSources = resolveArtifactForClassifier(artifact, "test-sources"); - if (testSources != null) { - testSourceArtifacts.add(testSources.getFile()); - processedArtifacts.add(testSources); - } else { - emptyTestSourceArtifacts.add(artifact); + ArtifactId testSourcesArtifactId = new ArtifactId( + artifact.getGroupId(), artifact.getArtifactId(), artifact.getType(), "test-sources"); + if (artifactSelector.isSelected(testSourcesArtifactId)) { + Artifact testSources = resolveArtifactForClassifier(artifact, "test-sources"); + if (testSources != null) { + testSourceArtifacts.add(testSources.getFile()); + processedArtifacts.add(testSources); + } else { + emptyTestSourceArtifacts.add(artifact); + } } } } @@ -961,7 +973,8 @@ private List getResourceTransformers() throws MojoExecution return Arrays.asList(transformers); } - private List getFilters(List artifactCollection) throws MojoExecutionException { + private List getFilters(List artifactCollection, ArtifactSelector artifactSelector) + throws MojoExecutionException { List filters = new ArrayList<>(); List simpleFilters = new ArrayList<>(); @@ -972,7 +985,9 @@ private List getFilters(List artifactCollection) throws MojoEx artifacts.put(project.getArtifact(), new ArtifactId(project.getArtifact())); for (Artifact artifact : artifactCollection) { - artifacts.put(artifact, new ArtifactId(artifact)); + if (artifactSelector.isSelected(artifact)) { + artifacts.put(artifact, new ArtifactId(artifact)); + } } for (ArchiveFilter filter : this.filters) { @@ -987,16 +1002,24 @@ private List getFilters(List artifactCollection) throws MojoEx jars.add(artifact.getFile()); if (createSourcesJar) { - Artifact sources = resolveArtifactForClassifier(artifact, "sources"); - if (sources != null) { - jars.add(sources.getFile()); + ArtifactId sourcesArtifactId = new ArtifactId( + artifact.getGroupId(), artifact.getArtifactId(), artifact.getType(), "sources"); + if (artifactSelector.isSelected(sourcesArtifactId)) { + Artifact sources = resolveArtifactForClassifier(artifact, "sources"); + if (sources != null) { + jars.add(sources.getFile()); + } } } if (shadeTestJar) { - Artifact tests = resolveArtifactForClassifier(artifact, "tests"); - if (tests != null) { - jars.add(tests.getFile()); + ArtifactId testsArtifactId = new ArtifactId( + artifact.getGroupId(), artifact.getArtifactId(), artifact.getType(), "tests"); + if (artifactSelector.isSelected(testsArtifactId)) { + Artifact tests = resolveArtifactForClassifier(artifact, "tests"); + if (tests != null) { + jars.add(tests.getFile()); + } } } } 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 4066442c..31b596b8 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 @@ -214,9 +214,10 @@ public void testShadeWithFilter() throws Exception { filtersField.set(mojo, new ArchiveFilter[] {archiveFilter}); // invoke getFilters() - Method getFilters = ShadeMojo.class.getDeclaredMethod("getFilters", List.class); + Method getFilters = ShadeMojo.class.getDeclaredMethod("getFilters", List.class, ArtifactSelector.class); getFilters.setAccessible(true); - List filters = (List) getFilters.invoke(mojo, Collections.emptyList()); + ArtifactSelector selector = new ArtifactSelector(Collections.emptyList(), Collections.emptyList(), null); + List filters = (List) getFilters.invoke(mojo, Collections.emptyList(), selector); // assertions - there must be one filter assertEquals(1, filters.size());