diff --git a/src/it/MSHADE-36-inject-dep-reduced-pom-in-final/invoker.properties b/src/it/MSHADE-36-inject-dep-reduced-pom-in-final/invoker.properties new file mode 100644 index 00000000..8c8f765b --- /dev/null +++ b/src/it/MSHADE-36-inject-dep-reduced-pom-in-final/invoker.properties @@ -0,0 +1,19 @@ +# 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.goals = clean package +invoker.java.version = 1.8+ diff --git a/src/it/MSHADE-36-inject-dep-reduced-pom-in-final/pom.xml b/src/it/MSHADE-36-inject-dep-reduced-pom-in-final/pom.xml new file mode 100644 index 00000000..eba55bd2 --- /dev/null +++ b/src/it/MSHADE-36-inject-dep-reduced-pom-in-final/pom.xml @@ -0,0 +1,100 @@ + + + + + + 4.0.0 + + + org.apache.maven.plugins.shade.its + shade-parent + 1.0 + ../setup-parent + + + org.apache.maven.its.shade.drp + test + 1.0 + jar + + MSHADE-36 + + Test to see that the dependency-reduced-pom.xml is injected into the final jar instead of the normal pom.xml + + + + 1.4 + 1.4 + + + + + junit + junit + 3.8.2 + + + org.codehaus.plexus + plexus-utils + 1.4.1 + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 2.0.2 + + + org.apache.maven.plugins + maven-shade-plugin + @project.version@ + + + attach-shade + package + + shade + + + true + true + true + + + org.codehaus.plexus + nl.example.shaded.org.codehaus.plexus + + + + + junit:junit + org.codehaus.plexus:plexus-utils + + + + + + + + + diff --git a/src/it/MSHADE-36-inject-dep-reduced-pom-in-final/src/main/java/nl/example/Main.java b/src/it/MSHADE-36-inject-dep-reduced-pom-in-final/src/main/java/nl/example/Main.java new file mode 100644 index 00000000..12bbae4e --- /dev/null +++ b/src/it/MSHADE-36-inject-dep-reduced-pom-in-final/src/main/java/nl/example/Main.java @@ -0,0 +1,33 @@ +/* + * 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 nl.example; + +import org.codehaus.plexus.util.StringUtils; + +public class Main +{ + public static void main( String[] args ) { + System.out.println( "Hello World!" + ( isEmpty("foo") ? " is empty!" : " -- ") ); + } + + public static boolean isEmpty( String input ) { + return StringUtils.isEmpty( input ); + } +} diff --git a/src/it/MSHADE-36-inject-dep-reduced-pom-in-final/verify.bsh b/src/it/MSHADE-36-inject-dep-reduced-pom-in-final/verify.bsh new file mode 100644 index 00000000..a3b6d442 --- /dev/null +++ b/src/it/MSHADE-36-inject-dep-reduced-pom-in-final/verify.bsh @@ -0,0 +1,63 @@ +/* + * 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.io.*; +import java.util.jar.*; +import java.util.Arrays; +import org.codehaus.plexus.util.*; + +String[] wanted = +{ + "nl/example/Main.class", + "junit/swingui/icons/error.gif", + "nl/example/shaded/org/codehaus/plexus/util/StringUtils.class", +}; + +String[] unwanted = +{ + "junit/swingui/TestRunner.class", + "org/codehaus/plexus/util/StringUtils.class", +}; + +JarFile jarFile = new JarFile( new File( basedir, "target/test-1.0.jar" ) ); + +for ( String path : wanted ) +{ + if ( jarFile.getEntry( path ) == null ) + { + throw new IllegalStateException( "wanted path is missing: " + path ); + } +} + +for ( String path : unwanted ) +{ + if ( jarFile.getEntry( path ) != null ) + { + throw new IllegalStateException( "unwanted path is present: " + path ); + } +} + +JarEntry jarEntry = jarFile.getEntry( "META-INF/maven/org.apache.maven.its.shade.drp/test/pom.xml" ); +String pomFile = IOUtil.toString( jarFile.getInputStream( jarEntry ), "UTF-8" ); + +if ( pomFile.contains( "org.codehaus.plexus" ) ) { + throw new IllegalStateException( "The pom.xml still contains a reference to the org.codehaus.plexus dependency"); +} + +jarFile.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 30d1b6ea..c98c6bfc 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 @@ -80,6 +80,8 @@ import java.util.Map; import java.util.Set; +import static org.apache.maven.plugins.shade.resource.UseDependencyReducedPomXml.createPomXmlReplaceTransformers; + /** * Mojo that performs shading delegating to the Shader component. * @@ -289,6 +291,14 @@ public class ShadeMojo @Parameter( defaultValue = "false" ) private boolean generateUniqueDependencyReducedPom; + /** + * Do we put the dependency reduced pom in the jar instead of the jar file provided by the project. + * + * @since 3.2.2 + */ + @Parameter( defaultValue = "false" ) + private boolean useDependencyReducedPomInJar; + /** * When true, dependencies are kept in the pom but with scope 'provided'; when false, the dependency is removed. */ @@ -437,6 +447,24 @@ public void execute() List resourceTransformers = getResourceTransformers(); + if ( createDependencyReducedPom ) + { + createDependencyReducedPom( artifactIds ); + } + + if ( useDependencyReducedPomInJar ) + { + if ( !createDependencyReducedPom ) + { + throw new MojoExecutionException( + "Cannot use the dependency-reduced-pom.xml if it is not created." ); + } + + // In some cases the used implementation of the resourceTransformers is immutable. + resourceTransformers = new ArrayList<>( resourceTransformers ); + resourceTransformers.addAll( createPomXmlReplaceTransformers( project, dependencyReducedPomLocation ) ); + } + ShadeRequest shadeRequest = shadeRequest( artifacts, outputJar, filters, relocators, resourceTransformers ); shader.shade( shadeRequest ); @@ -532,11 +560,6 @@ else if ( !renamed ) projectHelper.attachArtifact( project, "jar", "tests", shadedTests ); } - - if ( createDependencyReducedPom ) - { - createDependencyReducedPom( artifactIds ); - } } } } diff --git a/src/main/java/org/apache/maven/plugins/shade/resource/UseDependencyReducedPomXml.java b/src/main/java/org/apache/maven/plugins/shade/resource/UseDependencyReducedPomXml.java new file mode 100644 index 00000000..59a2ef5f --- /dev/null +++ b/src/main/java/org/apache/maven/plugins/shade/resource/UseDependencyReducedPomXml.java @@ -0,0 +1,54 @@ +package org.apache.maven.plugins.shade.resource; + +/* + * 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 org.apache.maven.project.MavenProject; + +import java.io.File; +import java.util.Arrays; +import java.util.List; + +/** + * Manually creates the resource processors needed to remove the original pom.xml and inject + * the dependency-reduced-pom.xml in it's place in the shaded JAR. + */ +public class UseDependencyReducedPomXml +{ + public static List createPomXmlReplaceTransformers( + MavenProject project, + File dependencyReducedPomLocation + ) + { + String pomXmlInFinalJarFilename = + "META-INF/maven/" + project.getGroupId() + "/" + project.getArtifactId() + "/pom.xml"; + + DontIncludeResourceTransformer removePomXML = new DontIncludeResourceTransformer(); + removePomXML.resource = pomXmlInFinalJarFilename; + + IncludeResourceTransformer insertDependencyReducedPomXML = new IncludeResourceTransformer(); + insertDependencyReducedPomXML.file = dependencyReducedPomLocation; + insertDependencyReducedPomXML.resource = pomXmlInFinalJarFilename; + + return Arrays.asList( + removePomXML, + insertDependencyReducedPomXML + ); + } +}