4

I am trying shade aws-java-sdk in order to resolve library conflicts as per the recommendation mentioned here. But I see that maven-shade-plugin, the entries in resource files (text) are not getting updated. For example contents of the request.handler2s and request.handlers are not getting changed as per the relocation pattern.

Can maven-shade-plugin update these files (resource files)? If not what are other options?

The pom.xml

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.4.3</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <filters>
                                <filter>
                                    <artifact>*:*</artifact>
                                    <excludes>
                                        <exclude>META-INF/*.SF</exclude>
                                        <exclude>META-INF/*.DSA</exclude>
                                        <exclude>META-INF/*.RSA</exclude>
                                    </excludes>
                                </filter>
                            </filters>
                            <relocations>
                                <relocation>
                                    <pattern>com.amazonaws</pattern>
                                    <shadedPattern>com.XX.YY.shaded.com.amazonaws</shadedPattern>
                                </relocation>
                            </relocations>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
anaray
  • 309
  • 1
  • 8

2 Answers2

2

Something you could try is adding the ServiceResourceTransformer to your configuration:

<transformers>
     <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
</transformers>
Chesnay Schepler
  • 1,250
  • 7
  • 9
  • Try updating the shade-plugin version to 3.0.0, previous versions have known issues with relocating services. – Chesnay Schepler Nov 27 '17 at 11:01
  • 2
    org.apache.maven.plugins.shade.resource.ServicesResourceTransformer relocates the files under META-INF/services only. AWS resource.handler files are in different path (com/amazonaws/services/sqs/resource.handler). Has anyone got the solution to change this file during relocation? Facing the same issue. Thanks. – popcoder Jan 18 '18 at 14:25
  • @popcoder, Had you got any fix for this issue? struggling with this issue. – Vicky Jun 14 '21 at 07:28
1

I've experienced the same issue in gradle. Too bad it is not as easy to implement a fix inline with maven. Below is the code I added to my build.gradle to allow me to properly shade com.amazonaws:

shadowJar {
  zip64 true
  mergeServiceFiles()
  relocate('com.amazonaws', 'shaded.com.amazonaws')
  transform(RelocateAmazonawsServiceRequestHandlers.class)
}

import com.github.jengelman.gradle.plugins.shadow.transformers.Transformer
import com.github.jengelman.gradle.plugins.shadow.transformers.TransformerContext
import shadow.org.apache.tools.zip.ZipOutputStream
import shadow.org.apache.tools.zip.ZipEntry

class RelocateAmazonawsServiceRequestHandlers implements Transformer {
  private Map<String, String> transformedResources = new HashMap<>()

  @Override
  boolean canTransformResource(FileTreeElement fileTreeElement) {
    return (fileTreeElement.getName() ==~ /com\/amazonaws\/services\/.*\/request.handlers/)
  }

  @Override
  void transform(TransformerContext transformerContext) {
    String text = transformerContext.is.text
    transformerContext.relocators.each {
      r -> text = r.applyToSourceContent(text)
    }
    transformedResources.put(transformerContext.path, text)
  }

  @Override
  boolean hasTransformedResource() {
    return transformedResources.size() > 0
  }

  @Override
  void modifyOutputStream(ZipOutputStream zipOutputStream) {
    transformedResources.each { key, value ->
      zipOutputStream.putNextEntry(new ZipEntry(key))
      zipOutputStream.write(value.getBytes())
      zipOutputStream.flush()
    }
  }
}
JohnRudolfLewis
  • 1,692
  • 1
  • 18
  • 33
  • Doesn't work for me. Could you share the full contents of the file? Your piece of code is missing some essentials as the version for example how you imported id 'com.github.johnrengelman.shadow' - what version etc? – bde.dev Apr 07 '21 at 17:23