You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by mi...@apache.org on 2021/06/17 19:17:17 UTC

[maven-shade-plugin] 01/01: [MSHADE-36] Add option to include dependency reduced POM instead of original one

This is an automated email from the ASF dual-hosted git repository.

michaelo pushed a commit to branch MSHADE-36
in repository https://gitbox.apache.org/repos/asf/maven-shade-plugin.git

commit 83c123d1f9c5f6927af2aca12ee322b5168a7c63
Author: Niels Basjes <nb...@bol.com>
AuthorDate: Tue Aug 27 11:31:38 2019 +0200

    [MSHADE-36] Add option to include dependency reduced POM instead of original one
    
    This closes #25
---
 .../invoker.properties                             | 18 ++++
 .../pom.xml                                        | 95 ++++++++++++++++++++++
 .../src/main/java/com/example/Main.java            | 33 ++++++++
 .../verify.bsh                                     | 73 +++++++++++++++++
 .../apache/maven/plugins/shade/mojo/ShadeMojo.java | 28 +++++--
 .../shade/resource/UseDependencyReducedPom.java    | 55 +++++++++++++
 6 files changed, 297 insertions(+), 5 deletions(-)

diff --git a/src/it/projects/MSHADE-36-inject-dep-reduced-pom-in-final/invoker.properties b/src/it/projects/MSHADE-36-inject-dep-reduced-pom-in-final/invoker.properties
new file mode 100644
index 0000000..a644cb7
--- /dev/null
+++ b/src/it/projects/MSHADE-36-inject-dep-reduced-pom-in-final/invoker.properties
@@ -0,0 +1,18 @@
+# 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
diff --git a/src/it/projects/MSHADE-36-inject-dep-reduced-pom-in-final/pom.xml b/src/it/projects/MSHADE-36-inject-dep-reduced-pom-in-final/pom.xml
new file mode 100644
index 0000000..db4be43
--- /dev/null
+++ b/src/it/projects/MSHADE-36-inject-dep-reduced-pom-in-final/pom.xml
@@ -0,0 +1,95 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+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.
+-->
+
+<project>
+  <modelVersion>4.0.0</modelVersion>
+
+  <parent>
+    <groupId>org.apache.maven.plugins.shade.its</groupId>
+    <artifactId>shade-parent</artifactId>
+    <version>1.0</version>
+    <relativePath>../setup-parent</relativePath>
+  </parent>
+
+  <groupId>org.apache.maven.its.shade.drp</groupId>
+  <artifactId>test</artifactId>
+  <version>1.0</version>
+  <packaging>jar</packaging>
+
+  <name>MSHADE-36</name>
+  <description>
+    Test to see that the dependency-reduced-pom.xml is injected into the final jar instead of the original pom.xml
+  </description>
+
+  <dependencies>
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <version>3.8.2</version>
+    </dependency>
+    <dependency>
+      <groupId>org.codehaus.plexus</groupId>
+      <artifactId>plexus-utils</artifactId>
+      <version>1.4.1</version>
+    </dependency>
+  </dependencies>
+
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-compiler-plugin</artifactId>
+        <version>2.0.2</version>
+      </plugin>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-shade-plugin</artifactId>
+        <version>@project.version@</version>
+        <executions>
+          <execution>
+            <id>attach-shade</id>
+            <phase>package</phase>
+            <goals>
+              <goal>shade</goal>
+            </goals>
+            <configuration>
+              <minimizeJar>true</minimizeJar>
+              <createDependencyReducedPom>true</createDependencyReducedPom>
+              <useDependencyReducedPomInJar>true</useDependencyReducedPomInJar>
+              <relocations>
+                <relocation>
+                  <pattern>org.codehaus.plexus</pattern>
+                  <shadedPattern>com.example.shaded.org.codehaus.plexus</shadedPattern>
+                </relocation>
+              </relocations>
+              <artifactSet>
+                <includes>
+                  <include>junit:junit</include>
+                  <include>org.codehaus.plexus:plexus-utils</include>
+                </includes>
+              </artifactSet>
+            </configuration>
+          </execution>
+        </executions>
+      </plugin>
+    </plugins>
+  </build>
+</project>
diff --git a/src/it/projects/MSHADE-36-inject-dep-reduced-pom-in-final/src/main/java/com/example/Main.java b/src/it/projects/MSHADE-36-inject-dep-reduced-pom-in-final/src/main/java/com/example/Main.java
new file mode 100644
index 0000000..9d5cf11
--- /dev/null
+++ b/src/it/projects/MSHADE-36-inject-dep-reduced-pom-in-final/src/main/java/com/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 com.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/projects/MSHADE-36-inject-dep-reduced-pom-in-final/verify.bsh b/src/it/projects/MSHADE-36-inject-dep-reduced-pom-in-final/verify.bsh
new file mode 100644
index 0000000..ae48cd9
--- /dev/null
+++ b/src/it/projects/MSHADE-36-inject-dep-reduced-pom-in-final/verify.bsh
@@ -0,0 +1,73 @@
+/*
+ * 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 =
+{
+    "com/example/Main.class",
+    "junit/swingui/icons/error.gif",
+    "com/example/shaded/org/codehaus/plexus/util/StringUtils.class",
+};
+
+String[] unwanted =
+{
+    "junit/swingui/TestRunner.class",
+    "org/codehaus/plexus/util/StringUtils.class",
+};
+
+JarFile jarFile = null;
+try
+{
+    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( "<groupId>org.codehaus.plexus</groupId>" ) )
+    {
+        throw new IllegalStateException( "The pom.xml still contains a reference to the org.codehaus.plexus dependency" );
+    }
+
+}
+finally
+{
+    if ( jarFile != null ) {
+        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 25b8330..7c524f8 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
@@ -76,6 +76,7 @@ import java.util.Map;
 import java.util.Set;
 
 import javax.inject.Inject;
+import static org.apache.maven.plugins.shade.resource.UseDependencyReducedPom.createPomReplaceTransformers;
 
 /**
  * Mojo that performs shading delegating to the Shader component.
@@ -286,6 +287,15 @@ public class ShadeMojo
     private boolean generateUniqueDependencyReducedPom;
 
     /**
+     * Add dependency reduced POM to the JAR instead of the original one provided by the project.
+     * If {@code createDependencyReducedPom} is {@code false} this parameter will be ignored.
+     *
+     * @since 3.3.0
+     */
+    @Parameter( defaultValue = "false" )
+    private boolean useDependencyReducedPomInJar;
+
+    /**
      * When true, dependencies are kept in the pom but with scope 'provided'; when false, the dependency is removed.
      */
     @Parameter
@@ -449,6 +459,19 @@ public class ShadeMojo
 
             List<ResourceTransformer> resourceTransformers = getResourceTransformers();
 
+            if ( createDependencyReducedPom )
+            {
+                createDependencyReducedPom( artifactIds );
+
+                if ( useDependencyReducedPomInJar )
+                {
+                    // In some cases the used implementation of the resourceTransformers is immutable.
+                    resourceTransformers = new ArrayList<>( resourceTransformers );
+                    resourceTransformers.addAll(
+                            createPomReplaceTransformers( project, dependencyReducedPomLocation ) );
+                }
+            }
+
             ShadeRequest shadeRequest = shadeRequest( "jar", artifacts, outputJar, filters, relocators,
                     resourceTransformers );
 
@@ -588,11 +611,6 @@ public class ShadeMojo
                         }
                     }
                 }
-
-                if ( createDependencyReducedPom )
-                {
-                    createDependencyReducedPom( artifactIds );
-                }
             }
         }
         catch ( Exception e )
diff --git a/src/main/java/org/apache/maven/plugins/shade/resource/UseDependencyReducedPom.java b/src/main/java/org/apache/maven/plugins/shade/resource/UseDependencyReducedPom.java
new file mode 100644
index 0000000..8e7071d
--- /dev/null
+++ b/src/main/java/org/apache/maven/plugins/shade/resource/UseDependencyReducedPom.java
@@ -0,0 +1,55 @@
+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.ArrayList;
+import java.util.List;
+
+/**
+ * Manually creates the resource processors needed to remove the original pom.xml and inject
+ * the dependency-reduced-pom.xml in its place in the shaded JAR.
+ */
+public class UseDependencyReducedPom
+{
+    public static List<ResourceTransformer> createPomReplaceTransformers(
+            MavenProject project,
+            File dependencyReducedPomLocation
+    )
+    {
+        String pomInFinalJarFilename =
+                "META-INF/maven/" + project.getGroupId() + "/" + project.getArtifactId() + "/pom.xml";
+
+        List<ResourceTransformer> resourceTransformers = new ArrayList<>();
+
+        DontIncludeResourceTransformer removePom = new DontIncludeResourceTransformer();
+        removePom.resource = pomInFinalJarFilename;
+        resourceTransformers.add( removePom );
+
+        IncludeResourceTransformer insertDependencyReducedPom = new IncludeResourceTransformer();
+        insertDependencyReducedPom.file = dependencyReducedPomLocation;
+        insertDependencyReducedPom.resource = pomInFinalJarFilename;
+        resourceTransformers.add( insertDependencyReducedPom );
+
+        return resourceTransformers;
+    }
+}