You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by rf...@apache.org on 2019/04/13 09:37:26 UTC

[maven-shade-plugin] 02/11: Keep Services

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

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

commit 4f8bcef2c35ecad36c883983a72d537bc428f9c2
Author: Markus KARG <ma...@headcrashing.eu>
AuthorDate: Thu Mar 14 07:27:59 2019 +0100

    Keep Services
    
    Signed-off-by: Markus KARG <ma...@headcrashing.eu>
---
 pom.xml                                            |  3 +
 src/it/MSHADE-313/invoker.properties               | 18 +++++
 src/it/MSHADE-313/pom.xml                          | 77 ++++++++++++++++++++++
 src/it/MSHADE-313/src/main/java/Main.java          | 22 +++++++
 src/it/MSHADE-313/verify.bsh                       | 51 ++++++++++++++
 .../maven/plugins/shade/filter/MinijarFilter.java  | 67 ++++++++++++++++++-
 .../apache/maven/plugins/shade/mojo/ShadeMojo.java | 13 +++-
 7 files changed, 248 insertions(+), 3 deletions(-)

diff --git a/pom.xml b/pom.xml
index cb10a84..84644cd 100644
--- a/pom.xml
+++ b/pom.xml
@@ -81,6 +81,9 @@
     <contributor>
       <name>Fabiano Cipriano de Oliveira</name>
     </contributor>
+    <contributor>
+      <name>Markus Karg</name>
+    </contributor>
   </contributors>
 
   <dependencies>
diff --git a/src/it/MSHADE-313/invoker.properties b/src/it/MSHADE-313/invoker.properties
new file mode 100644
index 0000000..c8b7b3c
--- /dev/null
+++ b/src/it/MSHADE-313/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.java.version = 1.8+
diff --git a/src/it/MSHADE-313/pom.xml b/src/it/MSHADE-313/pom.xml
new file mode 100644
index 0000000..ac8310a
--- /dev/null
+++ b/src/it/MSHADE-313/pom.xml
@@ -0,0 +1,77 @@
+<?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>
+
+  <groupId>org.apache.maven.its.shade.mj</groupId>
+  <artifactId>test</artifactId>
+  <version>1.0</version>
+  <packaging>jar</packaging>
+
+  <name>MSHADE-313</name>
+  <description>
+    Prevent minimizeJar from excluding classes that are used by services.
+  </description>
+
+  <dependencies>
+    <dependency>
+      <groupId>jdom</groupId>
+      <artifactId>jdom</artifactId>
+      <version>1.0</version>
+    </dependency>
+
+    <dependency>
+      <groupId>org.glassfish.jaxb</groupId>
+      <artifactId>jaxb-runtime</artifactId>
+      <version>2.3.2</version>
+    </dependency>
+  </dependencies>
+
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-compiler-plugin</artifactId>
+        <version>2.3.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>
+              <shadedArtifactAttached>false</shadedArtifactAttached>
+              <minimizeJar>true</minimizeJar>
+              <keepServices>true</keepServices>
+            </configuration>
+          </execution>
+        </executions>
+      </plugin>
+    </plugins>
+  </build>
+</project>
diff --git a/src/it/MSHADE-313/src/main/java/Main.java b/src/it/MSHADE-313/src/main/java/Main.java
new file mode 100644
index 0000000..293e02c
--- /dev/null
+++ b/src/it/MSHADE-313/src/main/java/Main.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 Main
+{
+}
diff --git a/src/it/MSHADE-313/verify.bsh b/src/it/MSHADE-313/verify.bsh
new file mode 100644
index 0000000..8542a85
--- /dev/null
+++ b/src/it/MSHADE-313/verify.bsh
@@ -0,0 +1,51 @@
+/*
+ * 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.*;
+
+String[] wanted =
+{
+    "Main.class",
+    "com/sun/xml/bind/v2/runtime/JAXBContextImpl.class"
+};
+
+String[] unwanted =
+{
+    "org/jdom/Document.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 );
+    }
+}
+
+jarFile.close();
diff --git a/src/main/java/org/apache/maven/plugins/shade/filter/MinijarFilter.java b/src/main/java/org/apache/maven/plugins/shade/filter/MinijarFilter.java
index bccf1b4..0029a34 100644
--- a/src/main/java/org/apache/maven/plugins/shade/filter/MinijarFilter.java
+++ b/src/main/java/org/apache/maven/plugins/shade/filter/MinijarFilter.java
@@ -20,6 +20,7 @@ package org.apache.maven.plugins.shade.filter;
  */
 
 import org.apache.maven.artifact.Artifact;
+import org.apache.maven.artifact.DependencyResolutionRequiredException;
 import org.apache.maven.plugin.logging.Log;
 import org.apache.maven.project.MavenProject;
 import org.codehaus.plexus.util.IOUtil;
@@ -27,15 +28,20 @@ import org.vafer.jdependency.Clazz;
 import org.vafer.jdependency.Clazzpath;
 import org.vafer.jdependency.ClazzpathUnit;
 
+import java.io.BufferedReader;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.InputStreamReader;
 import java.util.Collections;
+import java.util.Enumeration;
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Set;
+import java.util.jar.JarEntry;
+import java.util.jar.JarFile;
 import java.util.zip.ZipException;
 
 /**
@@ -73,17 +79,18 @@ public class MinijarFilter
     public MinijarFilter( MavenProject project, Log log )
         throws IOException
     {
-        this( project, log, Collections.<SimpleFilter>emptyList() );
+        this( project, log, Collections.<SimpleFilter>emptyList(), false );
     }
 
     /**
      * @param project {@link MavenProject}
      * @param log {@link Log}
      * @param simpleFilters {@link SimpleFilter}
+     * @param keepServices don't eliminate classes needed by {@code META-INF/services}
      * @throws IOException in case of errors.
      * @since 1.6
      */
-    public MinijarFilter( MavenProject project, Log log, List<SimpleFilter> simpleFilters )
+    public MinijarFilter( MavenProject project, Log log, List<SimpleFilter> simpleFilters, final boolean keepServices )
         throws IOException
     {
       this.log = log;
@@ -111,9 +118,65 @@ public class MinijarFilter
             removable.removeAll( artifactUnit.getTransitiveDependencies() );
             removeSpecificallyIncludedClasses( project,
                 simpleFilters == null ? Collections.<SimpleFilter>emptyList() : simpleFilters );
+
+            if ( keepServices )
+            {
+                removeServices( project, cp );
+            }
+        }
+    }
+
+    private void removeServices( final MavenProject project, final Clazzpath cp )
+    {
+        try
+        {
+            for ( final String fileName : project.getRuntimeClasspathElements() )
+            {
+                try ( final JarFile jar = new JarFile( fileName ) )
+                {
+                    for ( final Enumeration<JarEntry> entries = jar.entries(); entries.hasMoreElements(); )
+                    {
+                        final JarEntry jarEntry = entries.nextElement();
+                        if ( !jarEntry.getName().startsWith( "META-INF/services/" ) )
+                        {
+                            continue;
+                        }
+
+                        try ( final BufferedReader bufferedReader =
+                           new BufferedReader( new InputStreamReader( jar.getInputStream( jarEntry ) ) ) )
+                        {
+                            for ( String className = bufferedReader.readLine(); className != null;
+                                className = bufferedReader.readLine() )
+                            {
+                               log.info( className + " was not removed because it is a service" );
+                               removeClass( cp , className );
+                            }
+                        }
+                        catch ( final IOException e )
+                        {
+                            log.warn( e.getMessage() );
+                        }
+                    }
+                }
+                catch ( final IOException e )
+                {
+                    log.warn( e.getMessage() );
+                }
+            }
+        }
+        catch ( final DependencyResolutionRequiredException e )
+        {
+            log.warn( e.getMessage() );
         }
     }
 
+    private void removeClass( final Clazzpath clazzPath, final String className )
+    {
+        final Clazz clazz = clazzPath.getClazz( className );
+        removable.remove( clazz );
+        removable.removeAll( clazz.getTransitiveDependencies() );
+    }
+
     private ClazzpathUnit addDependencyToClasspath( Clazzpath cp, Artifact dependency )
         throws IOException
     {
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 c99ed42..48a4874 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
@@ -332,6 +332,17 @@ public class ShadeMojo
     private boolean minimizeJar;
 
     /**
+     * When {@code true}, classes will be safe from getting stripped down by {@code minimizeJar} if they are
+     * services (i. e. referenced by a text file contained in {@code META-INF/services}.
+     *
+     * @see #minimizeJar
+     *
+     * @since 3.2.2
+     */
+    @Parameter
+    private boolean keepServices;
+
+    /**
      * The path to the output file for the shaded artifact. When this parameter is set, the created archive will neither
      * replace the project's main artifact nor will it be attached. Hence, this parameter causes the parameters
      * {@link #finalName}, {@link #shadedArtifactAttached}, {@link #shadedClassifierName} and
@@ -821,7 +832,7 @@ public class ShadeMojo
 
             try
             {
-                filters.add( new MinijarFilter( project, getLog(), simpleFilters ) );
+                filters.add( new MinijarFilter( project, getLog(), simpleFilters, keepServices ) );
             }
             catch ( IOException e )
             {