diff --git a/src/it/MSHADE-326-Hide-dependencies-from-reactor/commandline/pom.xml b/src/it/MSHADE-326-Hide-dependencies-from-reactor/commandline/pom.xml new file mode 100644 index 00000000..ce686c2d --- /dev/null +++ b/src/it/MSHADE-326-Hide-dependencies-from-reactor/commandline/pom.xml @@ -0,0 +1,65 @@ + + + + + + 4.0.0 + + + org.apache.maven.its.shade.drp + project-parent + 1.0 + + + commandline + jar + + MSHADE-36 + + Test to see that the dependency-reduced-pom.xml is injected into the final jar instead of the normal pom.xml + + + + + org.apache.maven.its.shade.drp + library + ${project.version} + + + + + + + org.apache.maven.plugins + maven-shade-plugin + @project.version@ + + + attach-shade + package + + shade + + + + + + + diff --git a/src/it/MSHADE-326-Hide-dependencies-from-reactor/commandline/src/main/java/nl/example/Main.java b/src/it/MSHADE-326-Hide-dependencies-from-reactor/commandline/src/main/java/nl/example/Main.java new file mode 100644 index 00000000..3d942485 --- /dev/null +++ b/src/it/MSHADE-326-Hide-dependencies-from-reactor/commandline/src/main/java/nl/example/Main.java @@ -0,0 +1,27 @@ +/* + * 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; + +public class Main +{ + public static void main( String[] args ) { + System.out.println( "Hello World!" + ( StringTester.isEmpty("foo") ? " is empty!" : " -- ") ); + } +} diff --git a/src/it/MSHADE-326-Hide-dependencies-from-reactor/invoker.properties b/src/it/MSHADE-326-Hide-dependencies-from-reactor/invoker.properties new file mode 100644 index 00000000..8c8f765b --- /dev/null +++ b/src/it/MSHADE-326-Hide-dependencies-from-reactor/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-326-Hide-dependencies-from-reactor/library/pom.xml b/src/it/MSHADE-326-Hide-dependencies-from-reactor/library/pom.xml new file mode 100644 index 00000000..1bad1b65 --- /dev/null +++ b/src/it/MSHADE-326-Hide-dependencies-from-reactor/library/pom.xml @@ -0,0 +1,82 @@ + + + + + + 4.0.0 + + + org.apache.maven.its.shade.drp + project-parent + 1.0 + + + library + jar + + MSHADE-36 + + Test to see that the dependency-reduced-pom.xml is injected into the final jar instead of the normal pom.xml + + + + + org.codehaus.plexus + plexus-utils + 1.4.1 + + + + + + + org.apache.maven.plugins + maven-shade-plugin + @project.version@ + + + attach-shade + package + + shade + + + true + true + true + true + + + org.codehaus.plexus + nl.example.shaded.org.codehaus.plexus + + + + + org.codehaus.plexus:plexus-utils + + + + + + + + + diff --git a/src/it/MSHADE-326-Hide-dependencies-from-reactor/library/src/main/java/nl/example/StringTester.java b/src/it/MSHADE-326-Hide-dependencies-from-reactor/library/src/main/java/nl/example/StringTester.java new file mode 100644 index 00000000..7d635125 --- /dev/null +++ b/src/it/MSHADE-326-Hide-dependencies-from-reactor/library/src/main/java/nl/example/StringTester.java @@ -0,0 +1,29 @@ +/* + * 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 StringTester +{ + public static boolean isEmpty( String input ) { + return StringUtils.isEmpty( input ); + } +} diff --git a/src/it/MSHADE-326-Hide-dependencies-from-reactor/library/src/test/java/nl/example/TestStringTester.java b/src/it/MSHADE-326-Hide-dependencies-from-reactor/library/src/test/java/nl/example/TestStringTester.java new file mode 100644 index 00000000..f225775c --- /dev/null +++ b/src/it/MSHADE-326-Hide-dependencies-from-reactor/library/src/test/java/nl/example/TestStringTester.java @@ -0,0 +1,36 @@ +/* + * 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.junit.Test; + +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertFalse; + +public class TestStringTester +{ + @Test + public void runTest() + { + assertTrue(StringTester.isEmpty("")); + assertFalse(StringTester.isEmpty("FooBar")); + } + +} diff --git a/src/it/MSHADE-326-Hide-dependencies-from-reactor/pom.xml b/src/it/MSHADE-326-Hide-dependencies-from-reactor/pom.xml new file mode 100644 index 00000000..1c91bf46 --- /dev/null +++ b/src/it/MSHADE-326-Hide-dependencies-from-reactor/pom.xml @@ -0,0 +1,70 @@ + + + + + + 4.0.0 + + + org.apache.maven.plugins.shade.its + shade-parent + 1.0 + ../setup-parent + + + org.apache.maven.its.shade.drp + project-parent + pom + + MSHADE-36 + + Test to see that the dependency-reduced-pom.xml is injected into the final jar instead of the normal pom.xml + + + + 1.8 + 1.8 + + + + + junit + junit + 4.12 + test + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.0 + + + + + + library + commandline + + + diff --git a/src/it/MSHADE-326-Hide-dependencies-from-reactor/verify.bsh b/src/it/MSHADE-326-Hide-dependencies-from-reactor/verify.bsh new file mode 100644 index 00000000..df39fb8f --- /dev/null +++ b/src/it/MSHADE-326-Hide-dependencies-from-reactor/verify.bsh @@ -0,0 +1,99 @@ +/* + * 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.*; + +System.err.println("x1"); + +String[] libraryWanted = +{ + "nl/example/StringTester.class", + "nl/example/shaded/org/codehaus/plexus/util/StringUtils.class", +}; + +String[] libraryUnwanted = +{ + "org/codehaus/plexus/util/StringUtils.class", +}; + + System.err.println("x2"); +JarFile libraryJarFile = new JarFile( new File( basedir, "library/target/library-1.0.jar" ) ); + System.err.println("x3"); +for ( String path : libraryWanted ) +{ + if ( libraryJarFile.getEntry( path ) == null ) + { + throw new IllegalStateException( "Library: wanted path is missing: " + path ); + } +} + System.err.println("x4"); +for ( String path : libraryUnwanted ) +{ + if ( libraryJarFile.getEntry( path ) != null ) + { + throw new IllegalStateException( "Library: unwanted path is present: " + path ); + } +} + System.err.println("x5"); +JarEntry jarEntry = libraryJarFile.getEntry( "META-INF/maven/org.apache.maven.its.shade.drp/library/pom.xml" ); +String pomFile = IOUtil.toString( libraryJarFile.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"); +} + System.err.println("x6"); +libraryJarFile.close(); + System.err.println("x7"); +// ===================== + +String[] commandlineWanted = +{ + "nl/example/Main.class", + "nl/example/StringTester.class", + "nl/example/shaded/org/codehaus/plexus/util/StringUtils.class", +}; + +String[] commandlineUnwanted = +{ + "org/codehaus/plexus/util/Base64.class", + "org/codehaus/plexus/util/StringUtils.class", +}; + +JarFile commandlineJarFile = new JarFile( new File( basedir, "commandline/target/commandline-1.0.jar" ) ); + System.err.println("x8"); +for ( String path : commandlineWanted ) +{ + if ( commandlineJarFile.getEntry( path ) == null ) + { + throw new IllegalStateException( "Commandline: wanted path is missing: " + path ); + } +} + +for ( String path : commandlineUnwanted ) +{ + if ( commandlineJarFile.getEntry( path ) != null ) + { + throw new IllegalStateException( "Commandline: unwanted path is present: " + path ); + } +} + +commandlineJarFile.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 c98c6bfc..510d4423 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 @@ -299,6 +299,15 @@ public class ShadeMojo @Parameter( defaultValue = "false" ) private boolean useDependencyReducedPomInJar; + /** + * Do we hide the shaded/removed dependencies from the rest of the reactor build. + * Requires both createDependencyReducedPom and useDependencyReducedPomInJar to be true. + * + * @since 3.2.2 + */ + @Parameter( defaultValue = "false" ) + private boolean hideReducedDependenciesInReactor; + /** * When true, dependencies are kept in the pom but with scope 'provided'; when false, the dependency is removed. */ @@ -463,6 +472,24 @@ public void execute() // In some cases the used implementation of the resourceTransformers is immutable. resourceTransformers = new ArrayList<>( resourceTransformers ); resourceTransformers.addAll( createPomXmlReplaceTransformers( project, dependencyReducedPomLocation ) ); + + if ( hideReducedDependenciesInReactor ) + { + if ( !artifactIds.isEmpty() ) + { + getLog().info( "Hiding these shaded dependencies from the rest of the reactor build:" ); + getLog().info( artifactIds.toString() ); + List dependencies = new ArrayList<>(); + for ( Dependency dependency : project.getDependencies() ) + { + if ( !artifactIds.contains( getId( dependency ) ) ) + { + dependencies.add( dependency ); + } + } + project.setDependencies( dependencies ); + } + } } ShadeRequest shadeRequest = shadeRequest( artifacts, outputJar, filters, relocators, resourceTransformers );