当库使用gradle使用spi时,如何正确输出meta-inf?

vsikbqxv  于 2021-05-30  发布在  Hadoop
关注(0)|答案(1)|浏览(416)

我正在尝试使用gradle和java制作一个测试应用程序,它使用几个使用java服务提供者接口的库。我想这意味着我需要修改 META-INF 但我不知道该怎么做。
我得到的错误是 An SPI class of type org.apache.lucene.codecs.codec with name lucene50公司 does not exist. You need to add the corresponding JAR file supporting this SPI to your classpath. The current classpath supports the following names [ SimpleText] 我想我需要把spi信息转换成meta-inf,但我不知道该如何处理gradle。
具体来说,我尝试在以下构建文件中使用lucene和hadoop JAR:

  1. apply plugin: 'java'
  2. sourceCompatibility = 1.8
  3. version = '1.0'
  4. repositories {
  5. mavenCentral()
  6. }
  7. dependencies {
  8. compile group:'org.apache.lucene', name:'lucene-core', version:'5.0.0'
  9. compile group:'org.apache.lucene', name:'lucene-queryparser', version:'5.0.0'
  10. compile group:'org.apache.lucene', name:'lucene-analyzers-common', version:'5.0.0'
  11. compile group:'org.apache.lucene', name:'lucene-facet', version:'5.0.0'
  12. compile group:'org.apache.lucene', name:'lucene-codecs', version:'5.0.0'
  13. compile group:'org.apache.hadoop', name:'hadoop-hdfs', version:'2.6.0'
  14. compile group:'org.apache.hadoop', name:'hadoop-core', version:'1.2.1'
  15. compile group:'org.apache.hadoop', name:'hadoop-common', version:'2.6.0'
  16. }
  17. jar
  18. {
  19. from {configurations.compile.collect {it.isDirectory() ?it:zipTree(it) }}
  20. manifest
  21. {
  22. attributes 'Main-Class': 'LuceneTest'
  23. }
  24. }
yh2wf1be

yh2wf1be1#

两个 lucene-core 以及 lucene-codecs 图书馆提供 org.apache.lucene.codecs.Codec 实现,所以它们都有 META-INF/services/org.apache.lucene.codecs.Codec 服务文件。当合并所有依赖项时,两个文件都被添加到jar文件中,但是lucene只看到 lucene-codecs 一个。您可以在中手动合并服务文件 jar 任务,如本文中所述,它基本上查找所有服务文件并将它们组合在一起。更简单的解决方案可能是使用gradleshadow插件。
如果你把这个加到 build.gradle ,使用 shadowJar 任务而不是 jar 任务应该做你想做的。

  1. buildscript {
  2. repositories { jcenter() }
  3. dependencies {
  4. classpath 'com.github.jengelman.gradle.plugins:shadow:1.2.1'
  5. }
  6. }
  7. apply plugin: 'com.github.johnrengelman.shadow'
  8. shadowJar {
  9. mergeServiceFiles()
  10. }

相关问题