Author: aheritier
Date: Sun Feb  7 14:27:22 2016
New Revision: 1728966

URL: http://svn.apache.org/viewvc?rev=1728966&view=rev
Log:
[MSHADE-182] Service file should be relocated.
Submitted by: Trask Stalnaker , Kanstantsin Shautsou

Added:
    maven/plugins/trunk/maven-shade-plugin/src/it/MSHADE-182/
    maven/plugins/trunk/maven-shade-plugin/src/it/MSHADE-182/pom.xml
    maven/plugins/trunk/maven-shade-plugin/src/it/MSHADE-182/src/
    maven/plugins/trunk/maven-shade-plugin/src/it/MSHADE-182/src/main/
    maven/plugins/trunk/maven-shade-plugin/src/it/MSHADE-182/src/main/resources/
    
maven/plugins/trunk/maven-shade-plugin/src/it/MSHADE-182/src/main/resources/META-INF/
    
maven/plugins/trunk/maven-shade-plugin/src/it/MSHADE-182/src/main/resources/META-INF/services/
    
maven/plugins/trunk/maven-shade-plugin/src/it/MSHADE-182/src/main/resources/META-INF/services/relocateme.Service
    maven/plugins/trunk/maven-shade-plugin/src/it/MSHADE-182/verify.groovy
Modified:
    maven/plugins/trunk/maven-shade-plugin/   (props changed)
    maven/plugins/trunk/maven-shade-plugin/pom.xml
    
maven/plugins/trunk/maven-shade-plugin/src/main/java/org/apache/maven/plugins/shade/resource/ServicesResourceTransformer.java
    
maven/plugins/trunk/maven-shade-plugin/src/test/java/org/apache/maven/plugins/shade/resource/ServiceResourceTransformerTest.java

Propchange: maven/plugins/trunk/maven-shade-plugin/
------------------------------------------------------------------------------
--- svn:ignore (original)
+++ svn:ignore Sun Feb  7 14:27:22 2016
@@ -6,5 +6,5 @@ target
 *.ipr
 *.iws
 bin
-
 .java-version
+.idea

Modified: maven/plugins/trunk/maven-shade-plugin/pom.xml
URL: 
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-shade-plugin/pom.xml?rev=1728966&r1=1728965&r2=1728966&view=diff
==============================================================================
--- maven/plugins/trunk/maven-shade-plugin/pom.xml (original)
+++ maven/plugins/trunk/maven-shade-plugin/pom.xml Sun Feb  7 14:27:22 2016
@@ -202,6 +202,9 @@
               
<exclude>src/it/MSHADE-133/src/main/resources/myConfig.yml</exclude>
               
<exclude>src/it/rerun-with-reloc/src/main/resources/some-ordinary-resource.txt</exclude>
               
<exclude>src/it/rerun-without-reloc/src/main/resources/some-ordinary-resource.txt</exclude>
+              
<exclude>src/it/MSHADE-182/src/main/resources/META-INF/services/relocateme.Service</exclude>
+              
<exclude>src/it/MSHADE-182/target/classes/META-INF/services/relocateme.Service</exclude>
+              <exclude>src/it/MSHADE-182/build.log</exclude>
             </excludes>
           </configuration>
         </plugin>

Added: maven/plugins/trunk/maven-shade-plugin/src/it/MSHADE-182/pom.xml
URL: 
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-shade-plugin/src/it/MSHADE-182/pom.xml?rev=1728966&view=auto
==============================================================================
--- maven/plugins/trunk/maven-shade-plugin/src/it/MSHADE-182/pom.xml (added)
+++ maven/plugins/trunk/maven-shade-plugin/src/it/MSHADE-182/pom.xml Sun Feb  7 
14:27:22 2016
@@ -0,0 +1,59 @@
+<?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 xmlns="http://maven.apache.org/POM/4.0.0"; 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd";>
+    <modelVersion>4.0.0</modelVersion>
+
+    <groupId>org.apache.maven.its.shade</groupId>
+    <artifactId>MSHADE-182</artifactId>
+    <version>1.0-SNAPSHOT</version>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-shade-plugin</artifactId>
+                <version>@project.version@</version>
+                <executions>
+                    <execution>
+                        <id>shade</id>
+                        <phase>package</phase>
+                        <goals>
+                            <goal>shade</goal>
+                        </goals>
+                        <configuration>
+                            <relocations>
+                                <relocation>
+                                    <pattern>relocateme</pattern>
+                                    <shadedPattern>relocated</shadedPattern>
+                                </relocation>
+                            </relocations>
+                            <transformers>
+                                <transformer
+                                        
implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer">
+                                </transformer>
+                            </transformers>
+                        </configuration>
+                    </execution>
+                </executions>
+            </plugin>
+        </plugins>
+    </build>
+</project>

Added: 
maven/plugins/trunk/maven-shade-plugin/src/it/MSHADE-182/src/main/resources/META-INF/services/relocateme.Service
URL: 
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-shade-plugin/src/it/MSHADE-182/src/main/resources/META-INF/services/relocateme.Service?rev=1728966&view=auto
==============================================================================
--- 
maven/plugins/trunk/maven-shade-plugin/src/it/MSHADE-182/src/main/resources/META-INF/services/relocateme.Service
 (added)
+++ 
maven/plugins/trunk/maven-shade-plugin/src/it/MSHADE-182/src/main/resources/META-INF/services/relocateme.Service
 Sun Feb  7 14:27:22 2016
@@ -0,0 +1 @@
+relocateme.ServiceImpl

Added: maven/plugins/trunk/maven-shade-plugin/src/it/MSHADE-182/verify.groovy
URL: 
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-shade-plugin/src/it/MSHADE-182/verify.groovy?rev=1728966&view=auto
==============================================================================
--- maven/plugins/trunk/maven-shade-plugin/src/it/MSHADE-182/verify.groovy 
(added)
+++ maven/plugins/trunk/maven-shade-plugin/src/it/MSHADE-182/verify.groovy Sun 
Feb  7 14:27:22 2016
@@ -0,0 +1,23 @@
+/*
+ * 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.
+*/
+
+def jarFile = new java.util.jar.JarFile(new File(basedir, 
"/target/MSHADE-182-1.0-SNAPSHOT.jar"))
+def jarEntry = jarFile.getJarEntry("META-INF/services/relocated.Service")
+def content = new java.io.BufferedReader(new 
java.io.InputStreamReader(jarFile.getInputStream(jarEntry)))
+assert content.readLine() == "relocated.ServiceImpl"

Modified: 
maven/plugins/trunk/maven-shade-plugin/src/main/java/org/apache/maven/plugins/shade/resource/ServicesResourceTransformer.java
URL: 
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-shade-plugin/src/main/java/org/apache/maven/plugins/shade/resource/ServicesResourceTransformer.java?rev=1728966&r1=1728965&r2=1728966&view=diff
==============================================================================
--- 
maven/plugins/trunk/maven-shade-plugin/src/main/java/org/apache/maven/plugins/shade/resource/ServicesResourceTransformer.java
 (original)
+++ 
maven/plugins/trunk/maven-shade-plugin/src/main/java/org/apache/maven/plugins/shade/resource/ServicesResourceTransformer.java
 Sun Feb  7 14:27:22 2016
@@ -19,10 +19,13 @@ package org.apache.maven.plugins.shade.r
  * under the License.
  */
 
+import java.io.BufferedReader;
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.PrintWriter;
 import java.io.StringReader;
 import java.util.HashMap;
 import java.util.List;
@@ -32,7 +35,6 @@ import java.util.jar.JarOutputStream;
 
 import org.apache.commons.io.IOUtils;
 import org.apache.maven.plugins.shade.relocation.Relocator;
-import org.codehaus.plexus.util.IOUtil;
 
 import com.google.common.io.LineReader;
 
@@ -51,6 +53,8 @@ public class ServicesResourceTransformer
 
     private Map<String, ServiceStream> serviceEntries = new HashMap<String, 
ServiceStream>();
 
+    private List<Relocator> relocators;
+
     public boolean canTransformResource( String resource )
     {
         if ( resource.startsWith( SERVICES_PATH ) )
@@ -86,6 +90,11 @@ public class ServicesResourceTransformer
             }
             fout.append( relContent + "\n" );
         }
+
+        if ( this.relocators == null )
+        {
+            this.relocators = relocators;
+        }
     }
     public boolean hasTransformedResource()
     {
@@ -100,8 +109,51 @@ public class ServicesResourceTransformer
             String key = entry.getKey();
             ServiceStream data = entry.getValue();
 
+            if ( relocators != null )
+            {
+                key = key.substring( SERVICES_PATH.length() + 1 );
+                for ( Relocator relocator : relocators )
+                {
+                    if ( relocator.canRelocateClass( key ) )
+                    {
+                        key = relocator.relocateClass( key );
+                        break;
+                    }
+                }
+
+                key = SERVICES_PATH + '/' + key;
+            }
+
             jos.putNextEntry( new JarEntry( key ) );
-            IOUtil.copy( data.toInputStream(), jos );
+
+
+            //read the content of service file for candidate classes for 
relocation
+            PrintWriter writer = new PrintWriter( jos );
+            InputStreamReader streamReader = new InputStreamReader( 
data.toInputStream() );
+            BufferedReader reader = new BufferedReader( streamReader );
+            String className;
+
+            while ( ( className = reader.readLine() ) != null )
+            {
+
+                if ( relocators != null )
+                {
+                    for ( Relocator relocator : relocators )
+                    {
+                        //if the class can be relocated then relocate it
+                        if ( relocator.canRelocateClass( className ) )
+                        {
+                            className = relocator.applyToSourceContent( 
className );
+                            break;
+                        }
+                    }
+                }
+
+                writer.println( className );
+                writer.flush();
+            }
+
+            reader.close();
             data.reset();
         }
     }

Modified: 
maven/plugins/trunk/maven-shade-plugin/src/test/java/org/apache/maven/plugins/shade/resource/ServiceResourceTransformerTest.java
URL: 
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-shade-plugin/src/test/java/org/apache/maven/plugins/shade/resource/ServiceResourceTransformerTest.java?rev=1728966&r1=1728965&r2=1728966&view=diff
==============================================================================
--- 
maven/plugins/trunk/maven-shade-plugin/src/test/java/org/apache/maven/plugins/shade/resource/ServiceResourceTransformerTest.java
 (original)
+++ 
maven/plugins/trunk/maven-shade-plugin/src/test/java/org/apache/maven/plugins/shade/resource/ServiceResourceTransformerTest.java
 Sun Feb  7 14:27:22 2016
@@ -52,7 +52,8 @@ public class ServiceResourceTransformerT
         String content = "org.foo.Service\n";
         byte[] contentBytes = content.getBytes( "UTF-8" );
         InputStream contentStream = new ByteArrayInputStream( contentBytes );
-        String contentResource = "META-INF/services/org.something.another";
+        String contentResource = "META-INF/services/org.foo.something.another";
+        String contentResourceShaded = 
"META-INF/services/borg.foo.something.another";
 
         ServicesResourceTransformer xformer = new 
ServicesResourceTransformer();
         xformer.processResource( contentResource, contentStream, relocators );
@@ -67,7 +68,7 @@ public class ServiceResourceTransformerT
             jos.close();
             jos = null;
             JarFile jarFile = new JarFile( tempJar );
-            JarEntry jarEntry = jarFile.getJarEntry( contentResource );
+            JarEntry jarEntry = jarFile.getJarEntry( contentResourceShaded );
             assertNotNull( jarEntry );
             InputStream entryStream = jarFile.getInputStream( jarEntry );
             try {


Reply via email to