我正在尝试着色aws-java-sdk,以便根据提到的here建议解决库冲突。但是我看到maven-shade-plugin,资源文件(文本)中的条目没有得到更新。例如,request.handler2s和request.handlers的内容不会根据重定位模式进行更改。
maven-shade-plugin可以更新这些文件(资源文件)吗?如果没有其他选择?
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>
答案 0 :(得分:1)
我在gradle中也遇到了同样的问题。太糟糕了,用maven内联实现修复并不容易。下面是我添加到build.gradle中的代码,以使我可以正确遮罩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()
}
}
}
答案 1 :(得分:0)
您可以尝试的是将ServiceResourceTransformer添加到您的配置中:
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
</transformers>