From c2e98b4bcfcdf06a82f63e452df8b8735602dcbd Mon Sep 17 00:00:00 2001 From: Christopher Lambert Date: Wed, 31 Aug 2022 14:17:22 +0200 Subject: [PATCH] [MSHADE-419] createDependencyReducedPom should not replace original pom when shadedArtifactAttached The problem was introduced by MSHADE-321 in 3.3.0 because before, the `createDependencyReducedPom` method was only ever called when `shadedArtifactAttached` is false. The `createDependencyReducedPom` method not only writes the dependency reduced pom file but also modifies the current project to use it as replacement for its original pom. This has an effect on all maven build logic being executed afterwards. Thus, in multi module builds, when a module attaches a shaded jar, its non-shaded artifact no longer contains transitive dependencies when used from other modules, even though the pom in its jar still declares them. # Conflicts: # src/main/java/org/apache/maven/plugins/shade/mojo/ShadeMojo.java --- .../api/pom.xml | 33 ++++++++++ .../api/src/main/java/Api.java | 22 +++++++ .../impl-user/pom.xml | 55 +++++++++++++++++ .../impl-user/src/main/java/SimpleUser.java | 24 ++++++++ .../impl/pom.xml | 60 ++++++++++++++++++ .../impl/src/main/java/Impl.java | 22 +++++++ .../pom.xml | 49 +++++++++++++++ .../verify.groovy | 61 +++++++++++++++++++ 8 files changed, 326 insertions(+) create mode 100644 src/it/projects/MSHADE-419_createDependencyReducedPom_multimodule/api/pom.xml create mode 100644 src/it/projects/MSHADE-419_createDependencyReducedPom_multimodule/api/src/main/java/Api.java create mode 100644 src/it/projects/MSHADE-419_createDependencyReducedPom_multimodule/impl-user/pom.xml create mode 100644 src/it/projects/MSHADE-419_createDependencyReducedPom_multimodule/impl-user/src/main/java/SimpleUser.java create mode 100644 src/it/projects/MSHADE-419_createDependencyReducedPom_multimodule/impl/pom.xml create mode 100644 src/it/projects/MSHADE-419_createDependencyReducedPom_multimodule/impl/src/main/java/Impl.java create mode 100644 src/it/projects/MSHADE-419_createDependencyReducedPom_multimodule/pom.xml create mode 100644 src/it/projects/MSHADE-419_createDependencyReducedPom_multimodule/verify.groovy diff --git a/src/it/projects/MSHADE-419_createDependencyReducedPom_multimodule/api/pom.xml b/src/it/projects/MSHADE-419_createDependencyReducedPom_multimodule/api/pom.xml new file mode 100644 index 00000000..3fd4d0d7 --- /dev/null +++ b/src/it/projects/MSHADE-419_createDependencyReducedPom_multimodule/api/pom.xml @@ -0,0 +1,33 @@ + + + + + + 4.0.0 + + org.apache.maven.its.shade.cdrp-mm + mshade-419-parent + 1.0 + + mshade-419-api + 1.0 + diff --git a/src/it/projects/MSHADE-419_createDependencyReducedPom_multimodule/api/src/main/java/Api.java b/src/it/projects/MSHADE-419_createDependencyReducedPom_multimodule/api/src/main/java/Api.java new file mode 100644 index 00000000..b1a0ba74 --- /dev/null +++ b/src/it/projects/MSHADE-419_createDependencyReducedPom_multimodule/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-419_createDependencyReducedPom_multimodule/impl-user/pom.xml b/src/it/projects/MSHADE-419_createDependencyReducedPom_multimodule/impl-user/pom.xml new file mode 100644 index 00000000..199e396a --- /dev/null +++ b/src/it/projects/MSHADE-419_createDependencyReducedPom_multimodule/impl-user/pom.xml @@ -0,0 +1,55 @@ + + + + + + 4.0.0 + + org.apache.maven.its.shade.cdrp-mm + mshade-419-parent + 1.0 + + mshade-419-simple-user + 1.0 + + + + org.apache.maven.its.shade.cdrp-mm + mshade-419-impl + 1.0 + jar + + + + + diff --git a/src/it/projects/MSHADE-419_createDependencyReducedPom_multimodule/impl-user/src/main/java/SimpleUser.java b/src/it/projects/MSHADE-419_createDependencyReducedPom_multimodule/impl-user/src/main/java/SimpleUser.java new file mode 100644 index 00000000..8e67f173 --- /dev/null +++ b/src/it/projects/MSHADE-419_createDependencyReducedPom_multimodule/impl-user/src/main/java/SimpleUser.java @@ -0,0 +1,24 @@ +/* + * 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 SimpleUser +{ + Impl impl = new Impl(); + Api api = new Impl(); +} diff --git a/src/it/projects/MSHADE-419_createDependencyReducedPom_multimodule/impl/pom.xml b/src/it/projects/MSHADE-419_createDependencyReducedPom_multimodule/impl/pom.xml new file mode 100644 index 00000000..2dcedc8b --- /dev/null +++ b/src/it/projects/MSHADE-419_createDependencyReducedPom_multimodule/impl/pom.xml @@ -0,0 +1,60 @@ + + + + + + 4.0.0 + + org.apache.maven.its.shade.cdrp-mm + mshade-419-parent + 1.0 + + mshade-419-impl + 1.0 + + + org.apache.maven.its.shade.cdrp-mm + mshade-419-api + 1.0 + + + + + + maven-shade-plugin + @project.version@ + + + + shade + + + true + + + + + + + + + diff --git a/src/it/projects/MSHADE-419_createDependencyReducedPom_multimodule/impl/src/main/java/Impl.java b/src/it/projects/MSHADE-419_createDependencyReducedPom_multimodule/impl/src/main/java/Impl.java new file mode 100644 index 00000000..737fb97c --- /dev/null +++ b/src/it/projects/MSHADE-419_createDependencyReducedPom_multimodule/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-419_createDependencyReducedPom_multimodule/pom.xml b/src/it/projects/MSHADE-419_createDependencyReducedPom_multimodule/pom.xml new file mode 100644 index 00000000..e577f89c --- /dev/null +++ b/src/it/projects/MSHADE-419_createDependencyReducedPom_multimodule/pom.xml @@ -0,0 +1,49 @@ + + + + + + 4.0.0 + org.apache.maven.its.shade.cdrp-mm + mshade-419-parent + pom + 1.0 + + api + impl + impl-user + + + + + maven-jar-plugin + + + + jar + + + + + + + diff --git a/src/it/projects/MSHADE-419_createDependencyReducedPom_multimodule/verify.groovy b/src/it/projects/MSHADE-419_createDependencyReducedPom_multimodule/verify.groovy new file mode 100644 index 00000000..952f5a3d --- /dev/null +++ b/src/it/projects/MSHADE-419_createDependencyReducedPom_multimodule/verify.groovy @@ -0,0 +1,61 @@ +/* + * 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 java.util.jar.* +import org.codehaus.plexus.util.* + +// note that the following checks were passing even before any fix for MSHADE-419, +// since the generated poms and jars are generated correctly. +// only compilation of the impl-user module is failing because it uses the normal jar but +// without its transitive dependencies + +String jarPrefix = "impl/target/mshade-419-impl-1.0" +String jarPomPath = "META-INF/maven/org.apache.maven.its.shade.cdrp-mm/mshade-419-impl/pom.xml" +String apiDependencyString = "mshade-419-api" + +String mainPomFileContent = FileUtils.fileRead( new File( basedir, "impl/pom.xml" ), "UTF-8" ) +if ( !mainPomFileContent.contains( apiDependencyString ) ) +{ + throw new IllegalStateException( "The pom.xml should still contain the api dependency:\n" + mainPomFileContent ) +} + +String reducedPomFileContent = FileUtils.fileRead( new File( basedir, "impl/dependency-reduced-pom.xml" ), "UTF-8" ) +if ( reducedPomFileContent.contains( apiDependencyString ) ) +{ + throw new IllegalStateException( "The dependency-reduced-pom.xml should not contain the api dependency:\n" + reducedPomFileContent ) +} + +JarFile jarFile = null +try +{ + jarFile = new JarFile ( new File( basedir, jarPrefix + ".jar" ) ) + JarEntry jarEntry = jarFile.getEntry(jarPomPath ) + String pomFile = IOUtil.toString( jarFile.getInputStream( jarEntry ), "UTF-8" ) + if ( !pomFile.contains( apiDependencyString ) ) + { + throw new IllegalStateException( "The pom.xml in the normal jar should still contain the api dependency:\n" + pomFile ) + } +} +finally +{ + if ( jarFile != null ) + { + jarFile.close() + } +}