From b3ad36d9b312538398a8a9242b77d896e964ce5b Mon Sep 17 00:00:00 2001 From: "James Z.M. Gao" Date: Wed, 6 Apr 2022 08:35:23 +0800 Subject: [PATCH 1/3] [MSHADE-323] compatible with the flatten plugin The runtime injected pom will be set via `MavenProject::setFile` or `MavenProject::setPomFile`, including this plugin and the flatten plugin. So to chain these kind of plugins, when generating the d-r-p pom, this pr first try to parse a model directly from `MavenProject::getFile` and fallback to a clone of `MavenProject::getOriginalModel` if not set the pom file. --- .../pom.xml | 84 +++++++++++++++++++ .../src/main/java/A.java | 20 +++++ .../verify.groovy | 26 ++++++ .../maven/plugins/shade/mojo/ShadeMojo.java | 9 +- 4 files changed, 137 insertions(+), 2 deletions(-) create mode 100644 src/it/projects/MSHADE-323-compatible-with-flatten-pom/pom.xml create mode 100644 src/it/projects/MSHADE-323-compatible-with-flatten-pom/src/main/java/A.java create mode 100644 src/it/projects/MSHADE-323-compatible-with-flatten-pom/verify.groovy diff --git a/src/it/projects/MSHADE-323-compatible-with-flatten-pom/pom.xml b/src/it/projects/MSHADE-323-compatible-with-flatten-pom/pom.xml new file mode 100644 index 00000000..6f9c41fb --- /dev/null +++ b/src/it/projects/MSHADE-323-compatible-with-flatten-pom/pom.xml @@ -0,0 +1,84 @@ + + + + + + 4.0.0 + + org.apache.maven.its.shade.flatten + test + 1.0 + + MSHADE-323 + + Test that integration of shade and flatten plugins. + + + + 0.1 + true + + + + + org.apache.maven.its.shade.drp + a + ${ver} + + + + + + + org.codehaus.mojo + flatten-maven-plugin + 1.2.5 + + + + flatten + + prepare-package + + + + + maven-jar-plugin + 3.2.2 + + + org.apache.maven.plugins + maven-shade-plugin + @project.version@ + + + + shade + + + true + target/dependency-reduced-pom.xml + + + + + + + diff --git a/src/it/projects/MSHADE-323-compatible-with-flatten-pom/src/main/java/A.java b/src/it/projects/MSHADE-323-compatible-with-flatten-pom/src/main/java/A.java new file mode 100644 index 00000000..3b244767 --- /dev/null +++ b/src/it/projects/MSHADE-323-compatible-with-flatten-pom/src/main/java/A.java @@ -0,0 +1,20 @@ +/* + * 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. + */ + +class A {} diff --git a/src/it/projects/MSHADE-323-compatible-with-flatten-pom/verify.groovy b/src/it/projects/MSHADE-323-compatible-with-flatten-pom/verify.groovy new file mode 100644 index 00000000..f550cfc9 --- /dev/null +++ b/src/it/projects/MSHADE-323-compatible-with-flatten-pom/verify.groovy @@ -0,0 +1,26 @@ +/* + * 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. + */ + +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.properties.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 d717253f..681a135c 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 @@ -25,6 +25,7 @@ import org.apache.maven.model.Dependency; import org.apache.maven.model.Exclusion; import org.apache.maven.model.Model; +import org.apache.maven.model.io.xpp3.MavenXpp3Reader; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugins.annotations.Component; @@ -57,6 +58,7 @@ import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolverException; import org.codehaus.plexus.util.IOUtil; import org.codehaus.plexus.util.WriterFactory; +import org.codehaus.plexus.util.xml.pull.XmlPullParserException; import java.io.File; import java.io.FileInputStream; @@ -1059,7 +1061,7 @@ private File shadedTestArtifactFile() // We need to find the direct dependencies that have been included in the uber JAR so that we can modify the // POM accordingly. private void createDependencyReducedPom( Set artifactsToRemove ) - throws IOException, DependencyGraphBuilderException, ProjectBuildingException + throws IOException, DependencyGraphBuilderException, ProjectBuildingException, XmlPullParserException { List dependencies = new ArrayList<>(); @@ -1090,7 +1092,10 @@ private void createDependencyReducedPom( Set artifactsToRemove ) origDeps = transitiveDeps; } - Model model = project.getOriginalModel(); + final Model model = project.getFile() == null + ? project.getOriginalModel().clone() + : new MavenXpp3Reader().read( new FileInputStream( project.getFile() ) ); + // MSHADE-185: We will remove all system scoped dependencies which usually // have some kind of property usage. At this time the properties within // such things are already evaluated. From a0f884844556ae669bce7e45061e7db1c24f703f Mon Sep 17 00:00:00 2001 From: "James Z.M. Gao" Date: Wed, 6 Apr 2022 20:16:36 +0800 Subject: [PATCH 2/3] fix description and style --- src/it/projects/MSHADE-323-compatible-with-flatten-pom/pom.xml | 2 +- .../MSHADE-323-compatible-with-flatten-pom/verify.groovy | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/it/projects/MSHADE-323-compatible-with-flatten-pom/pom.xml b/src/it/projects/MSHADE-323-compatible-with-flatten-pom/pom.xml index 6f9c41fb..f4b66151 100644 --- a/src/it/projects/MSHADE-323-compatible-with-flatten-pom/pom.xml +++ b/src/it/projects/MSHADE-323-compatible-with-flatten-pom/pom.xml @@ -28,7 +28,7 @@ under the License. MSHADE-323 - Test that integration of shade and flatten plugins. + Test the integration of Shade and Flatten plugins. diff --git a/src/it/projects/MSHADE-323-compatible-with-flatten-pom/verify.groovy b/src/it/projects/MSHADE-323-compatible-with-flatten-pom/verify.groovy index f550cfc9..c715ed64 100644 --- a/src/it/projects/MSHADE-323-compatible-with-flatten-pom/verify.groovy +++ b/src/it/projects/MSHADE-323-compatible-with-flatten-pom/verify.groovy @@ -20,7 +20,7 @@ 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 ns = new groovy.xml.Namespace( "http://maven.apache.org/POM/4.0.0" ) def pom = new XmlParser().parse( pomFile ) assert pom.properties.size() == 0 From 221c76098d3efe665fa7b7fa35568e09c149abc8 Mon Sep 17 00:00:00 2001 From: "James Z.M. Gao" Date: Wed, 6 Apr 2022 20:52:19 +0800 Subject: [PATCH 3/3] set ModelReader to parse pom file --- .../maven/plugins/shade/mojo/ShadeMojo.java | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) 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 681a135c..cfa6e3c4 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 @@ -25,7 +25,7 @@ import org.apache.maven.model.Dependency; import org.apache.maven.model.Exclusion; import org.apache.maven.model.Model; -import org.apache.maven.model.io.xpp3.MavenXpp3Reader; +import org.apache.maven.model.io.ModelReader; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugins.annotations.Component; @@ -58,7 +58,6 @@ import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolverException; import org.codehaus.plexus.util.IOUtil; import org.codehaus.plexus.util.WriterFactory; -import org.codehaus.plexus.util.xml.pull.XmlPullParserException; import java.io.File; import java.io.FileInputStream; @@ -142,6 +141,12 @@ public class ShadeMojo @Component protected ArtifactResolver artifactResolver; + /** + * Model reader for parsing pom file + */ + @Component + private ModelReader modelReader; + /** * Artifacts to include/exclude from the final artifact. Artifacts are denoted by composite identifiers of the * general form groupId:artifactId:type:classifier. Since version 1.3, the wildcard characters '*' and @@ -483,7 +488,7 @@ public void execute() List resourceTransformers = getResourceTransformers(); - if ( createDependencyReducedPom ) + if ( createDependencyReducedPom && project.getFile() != null ) { createDependencyReducedPom( artifactIds ); @@ -1061,7 +1066,7 @@ private File shadedTestArtifactFile() // We need to find the direct dependencies that have been included in the uber JAR so that we can modify the // POM accordingly. private void createDependencyReducedPom( Set artifactsToRemove ) - throws IOException, DependencyGraphBuilderException, ProjectBuildingException, XmlPullParserException + throws IOException, DependencyGraphBuilderException, ProjectBuildingException { List dependencies = new ArrayList<>(); @@ -1092,9 +1097,7 @@ private void createDependencyReducedPom( Set artifactsToRemove ) origDeps = transitiveDeps; } - final Model model = project.getFile() == null - ? project.getOriginalModel().clone() - : new MavenXpp3Reader().read( new FileInputStream( project.getFile() ) ); + final Model model = modelReader.read( project.getFile(), null ); // MSHADE-185: We will remove all system scoped dependencies which usually // have some kind of property usage. At this time the properties within