maven不更新资源文件中的条目

pobjuy32  于 2021-06-25  发布在  Flink
关注(0)|答案(2)|浏览(396)

我正在尝试使用aws-javasdk,以便按照这里提到的建议解决库冲突。但是我看到maven shade插件,资源文件(文本)中的条目没有得到更新。例如,request.handler2s和request.handler的内容不会根据重定位模式进行更改。
maven shade插件可以更新这些文件(资源文件)吗?如果没有,还有什么选择?
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>
ckx4rj1h

ckx4rj1h1#

我在grad尔也经历过同样的问题。可惜的是,在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()
    }
  }
}
y1aodyip

y1aodyip2#

您可以尝试将serviceresourcetransformer添加到配置中:

<transformers>
     <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
</transformers>

相关问题