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:33 UTC

[maven-shade-plugin] 09/11: deep scan for services loading 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 783b2ac5f4ca94798329d55571c82b65e90759de
Author: Markus KARG <ma...@headcrashing.eu>
AuthorDate: Sun Apr 7 21:35:58 2019 +0000

    deep scan for services loading services
---
 .../{used-service => dependency-service}/pom.xml   |   2 +-
 .../src/main/java/DependencyReferencedClass.java}  |   6 +-
 .../src/main/java/DependencyServiceClass.java}     |   7 +-
 .../src/main/java/DependencyServiceInterface.java} |   6 +-
 .../main/java/DependencyUnreferencedClass.java}    |   6 +-
 .../META-INF/services/DependencyServiceInterface   |  20 +++++
 src/it/MSHADE-313/pom.xml                          |   1 +
 src/it/MSHADE-313/used-service/pom.xml             |   8 ++
 .../src/main/java/SomeReferencedClass.java         |   9 +-
 src/it/MSHADE-313/verify.bsh                       |   8 +-
 .../maven/plugins/shade/filter/MinijarFilter.java  | 100 +++++++++++----------
 11 files changed, 110 insertions(+), 63 deletions(-)

diff --git a/src/it/MSHADE-313/used-service/pom.xml b/src/it/MSHADE-313/dependency-service/pom.xml
similarity index 95%
copy from src/it/MSHADE-313/used-service/pom.xml
copy to src/it/MSHADE-313/dependency-service/pom.xml
index fe4edc0..9c589d5 100644
--- a/src/it/MSHADE-313/used-service/pom.xml
+++ b/src/it/MSHADE-313/dependency-service/pom.xml
@@ -23,6 +23,6 @@ under the License.
   <modelVersion>4.0.0</modelVersion>
 
   <groupId>org.apache.maven.its.shade.mj</groupId>
-  <artifactId>used-service</artifactId>
+  <artifactId>dependency-service</artifactId>
   <version>1.0</version>
 </project>
diff --git a/src/it/MSHADE-313/used-service/src/main/java/SomeReferencedClass.java b/src/it/MSHADE-313/dependency-service/src/main/java/DependencyReferencedClass.java
similarity index 94%
copy from src/it/MSHADE-313/used-service/src/main/java/SomeReferencedClass.java
copy to src/it/MSHADE-313/dependency-service/src/main/java/DependencyReferencedClass.java
index afc0f5f..4c0bc26 100644
--- a/src/it/MSHADE-313/used-service/src/main/java/SomeReferencedClass.java
+++ b/src/it/MSHADE-313/dependency-service/src/main/java/DependencyReferencedClass.java
@@ -6,9 +6,9 @@
  * 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
@@ -17,6 +17,6 @@
  * under the License.
  */
 
-public class SomeReferencedClass
+public class DependencyReferencedClass
 {
 }
diff --git a/src/it/MSHADE-313/used-service/src/main/java/SomeReferencedClass.java b/src/it/MSHADE-313/dependency-service/src/main/java/DependencyServiceClass.java
similarity index 84%
copy from src/it/MSHADE-313/used-service/src/main/java/SomeReferencedClass.java
copy to src/it/MSHADE-313/dependency-service/src/main/java/DependencyServiceClass.java
index afc0f5f..8820be7 100644
--- a/src/it/MSHADE-313/used-service/src/main/java/SomeReferencedClass.java
+++ b/src/it/MSHADE-313/dependency-service/src/main/java/DependencyServiceClass.java
@@ -6,9 +6,9 @@
  * 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
@@ -17,6 +17,7 @@
  * under the License.
  */
 
-public class SomeReferencedClass
+public class DependencyServiceClass implements DependencyServiceInterface
 {
+    private static DependencyReferencedClass anEssentialDependency;
 }
diff --git a/src/it/MSHADE-313/used-service/src/main/java/SomeReferencedClass.java b/src/it/MSHADE-313/dependency-service/src/main/java/DependencyServiceInterface.java
similarity index 94%
copy from src/it/MSHADE-313/used-service/src/main/java/SomeReferencedClass.java
copy to src/it/MSHADE-313/dependency-service/src/main/java/DependencyServiceInterface.java
index afc0f5f..b32026a 100644
--- a/src/it/MSHADE-313/used-service/src/main/java/SomeReferencedClass.java
+++ b/src/it/MSHADE-313/dependency-service/src/main/java/DependencyServiceInterface.java
@@ -6,9 +6,9 @@
  * 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
@@ -17,6 +17,6 @@
  * under the License.
  */
 
-public class SomeReferencedClass
+public interface DependencyServiceInterface
 {
 }
diff --git a/src/it/MSHADE-313/used-service/src/main/java/SomeReferencedClass.java b/src/it/MSHADE-313/dependency-service/src/main/java/DependencyUnreferencedClass.java
similarity index 94%
copy from src/it/MSHADE-313/used-service/src/main/java/SomeReferencedClass.java
copy to src/it/MSHADE-313/dependency-service/src/main/java/DependencyUnreferencedClass.java
index afc0f5f..20d838c 100644
--- a/src/it/MSHADE-313/used-service/src/main/java/SomeReferencedClass.java
+++ b/src/it/MSHADE-313/dependency-service/src/main/java/DependencyUnreferencedClass.java
@@ -6,9 +6,9 @@
  * 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
@@ -17,6 +17,6 @@
  * under the License.
  */
 
-public class SomeReferencedClass
+public class DependencyUnreferencedClass
 {
 }
diff --git a/src/it/MSHADE-313/dependency-service/src/main/resources/META-INF/services/DependencyServiceInterface b/src/it/MSHADE-313/dependency-service/src/main/resources/META-INF/services/DependencyServiceInterface
new file mode 100644
index 0000000..2b68806
--- /dev/null
+++ b/src/it/MSHADE-313/dependency-service/src/main/resources/META-INF/services/DependencyServiceInterface
@@ -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.
+#
+	DependencyServiceClass # Please do not remove comments or whitespace; they are part of the IT test.
+
diff --git a/src/it/MSHADE-313/pom.xml b/src/it/MSHADE-313/pom.xml
index 7907136..543e162 100644
--- a/src/it/MSHADE-313/pom.xml
+++ b/src/it/MSHADE-313/pom.xml
@@ -35,6 +35,7 @@ under the License.
   <modules>
     <module>unused-service</module>
     <module>used-service</module>
+    <module>dependency-service</module>
     <module>test</module>
   </modules>
 </project>
diff --git a/src/it/MSHADE-313/used-service/pom.xml b/src/it/MSHADE-313/used-service/pom.xml
index fe4edc0..342cf2d 100644
--- a/src/it/MSHADE-313/used-service/pom.xml
+++ b/src/it/MSHADE-313/used-service/pom.xml
@@ -25,4 +25,12 @@ under the License.
   <groupId>org.apache.maven.its.shade.mj</groupId>
   <artifactId>used-service</artifactId>
   <version>1.0</version>
+
+  <dependencies>
+    <dependency>
+      <groupId>org.apache.maven.its.shade.mj</groupId>
+      <artifactId>dependency-service</artifactId>
+      <version>1.0</version>
+    </dependency>
+  </dependencies>
 </project>
diff --git a/src/it/MSHADE-313/used-service/src/main/java/SomeReferencedClass.java b/src/it/MSHADE-313/used-service/src/main/java/SomeReferencedClass.java
index afc0f5f..6430d7f 100644
--- a/src/it/MSHADE-313/used-service/src/main/java/SomeReferencedClass.java
+++ b/src/it/MSHADE-313/used-service/src/main/java/SomeReferencedClass.java
@@ -6,9 +6,9 @@
  * 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
@@ -17,6 +17,11 @@
  * under the License.
  */
 
+import java.util.ServiceLoader;
+
 public class SomeReferencedClass
 {
+    {
+        ServiceLoader.load( DependencyServiceInterface.class );
+    }
 }
diff --git a/src/it/MSHADE-313/verify.bsh b/src/it/MSHADE-313/verify.bsh
index cf79b16..38014a8 100644
--- a/src/it/MSHADE-313/verify.bsh
+++ b/src/it/MSHADE-313/verify.bsh
@@ -24,14 +24,18 @@ String[] wanted =
     "Main.class",
     "SomeServiceInterface.class",
     "SomeServiceClass.class",
-    "SomeReferencedClass.class"
+    "SomeReferencedClass.class",
+    "DependencyServiceInterface.class",
+    "DependencyServiceClass.class",
+    "DependencyReferencedClass.class"
 };
 
 String[] unwanted =
 {
     "UnusedServiceInterface.class",
     "UnusedServiceClass.class",
-    "SomeUnreferencedClass.class"
+    "SomeUnreferencedClass.class",
+    "DependencyUnreferencedClass.class"
 };
 
 JarFile jarFile = new JarFile( new File( basedir, "test/target/test-1.0.jar" ) );
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 e04355e..e0a8e85 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
@@ -125,71 +125,79 @@ public class MinijarFilter
 
     private void removeServices( final MavenProject project, final Clazzpath cp )
     {
-        final Set<Clazz> neededClasses = cp.getClazzes();
-        neededClasses.removeAll( removable );
-        try
+        boolean repeatScan;
+        do
         {
-            for ( final String fileName : project.getRuntimeClasspathElements() )
+            repeatScan = false;
+            final Set<Clazz> neededClasses = cp.getClazzes();
+            neededClasses.removeAll( removable );
+            try
             {
-                try ( final JarFile jar = new JarFile( fileName ) )
+                for ( final String fileName : project.getRuntimeClasspathElements() )
                 {
-                    for ( final Enumeration<JarEntry> entries = jar.entries(); entries.hasMoreElements(); )
+                    try ( final JarFile jar = new JarFile( fileName ) )
                     {
-                        final JarEntry jarEntry = entries.nextElement();
-                        if ( jarEntry.isDirectory() || !jarEntry.getName().startsWith( "META-INF/services/" ) )
+                        for ( final Enumeration<JarEntry> entries = jar.entries(); entries.hasMoreElements(); )
                         {
-                            continue;
-                        }
+                            final JarEntry jarEntry = entries.nextElement();
+                            if ( jarEntry.isDirectory() || !jarEntry.getName().startsWith( "META-INF/services/" ) )
+                            {
+                                continue;
+                            }
 
-                        final String serviceClassName = jarEntry.getName().substring( "META-INF/services/".length() );
-                        final boolean isNeededClass = neededClasses.contains( cp.getClazz( serviceClassName ) );
-                        if ( !isNeededClass )
-                        {
-                            continue;
-                        }
+                            final String serviceClassName =
+                              jarEntry.getName().substring( "META-INF/services/".length() );
+                            final boolean isNeededClass = neededClasses.contains( cp.getClazz( serviceClassName ) );
+                            if ( !isNeededClass )
+                            {
+                                continue;
+                            }
 
-                        try ( final BufferedReader bufferedReader =
-                           new BufferedReader( new InputStreamReader( jar.getInputStream( jarEntry ), UTF_8 ) ) )
-                        {
-                            for ( String line = bufferedReader.readLine(); line != null;
-                                line = bufferedReader.readLine() )
+                            try ( final BufferedReader bufferedReader =
+                            new BufferedReader( new InputStreamReader( jar.getInputStream( jarEntry ), UTF_8 ) ) )
                             {
-                                final String className = line.split( "#", 2 )[0].trim();
-                                if ( className.isEmpty() )
+                                for ( String line = bufferedReader.readLine(); line != null;
+                                    line = bufferedReader.readLine() )
                                 {
-                                    continue;
+                                    final String className = line.split( "#", 2 )[0].trim();
+                                    if ( className.isEmpty() )
+                                    {
+                                        continue;
+                                    }
+
+                                    final Clazz clazz = cp.getClazz( className );
+                                    if ( clazz == null || !removable.contains( clazz ) )
+                                    {
+                                        continue;
+                                    }
+
+                                    log.info( className + " was not removed because it is a service" );
+                                    removeClass( cp, clazz );
+                                    repeatScan = true; // check whether the found classes use services in turn
                                 }
-
-                                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 IOException e )
-                {
-                    log.warn( e.getMessage() );
+                    catch ( final IOException e )
+                    {
+                        log.warn( e.getMessage() );
+                    }
                 }
             }
+            catch ( final DependencyResolutionRequiredException e )
+            {
+                log.warn( e.getMessage() );
+            }
         }
-        catch ( final DependencyResolutionRequiredException e )
-        {
-            log.warn( e.getMessage() );
-        }
+        while ( repeatScan );
     }
 
-    private void removeClass( final Clazzpath clazzPath, final String className )
+    private void removeClass( final Clazzpath clazzPath, final Clazz clazz )
     {
-        final Clazz clazz = clazzPath.getClazz( className );
-        if ( clazz == null )
-        {
-            return;
-        }
-
         removable.remove( clazz );
         removable.removeAll( clazz.getTransitiveDependencies() );
     }