从源构建 lucene:编解码器不存在

Build lucene from sources: Codec does not exist

我导入了 Lucene 源代码并成功构建。 但是当我尝试使用任何 Lucene 类 时,我得到

A SPI class of type org.apache.lucene.codecs.Codec with name 'Lucene410' does not exist
The current classpath supports the following names: []

我试图通过

获取到类的路径
String path = Lucene410Codec.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath();

并且得到了正确的路径,所以错误的jar文件没有问题。

问题是我在导入项目时错过了 META-INF 文件夹。 我已将 META-INF/services 文件夹及其内容 - 编解码器文件(我从 lucene.core.jar 中获取)手动添加到源并配置了正确的构建路径。

add something to resources in eclipse

现在我可以使用 Lucene 了。

对于面临此类问题的人,我有另一个看起来不错的解决方案。发生此类问题的主要原因是一些 JAR 文件(在本例中为 Lucene)提供了一些接口的实现,附带 'META-DATA/service' 目录。该目录然后将接口映射到它们的实现 classes 以供服务定位器查找。因此,解决方案将是重新定位这些实现 class 的 class 名称,并将同一接口的多个实现合并到一个服务条目中。

Maven 的 shade 插件提供了一个名为 ServiceResourceTransformer 的资源转换器来执行此类重定位。所以在实践中我会定义插件如下:

<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>
        <transformers>
          <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
            <mainClass>main.class.of.your.app.MainClass</mainClass>
          </transformer>
          <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
        </transformers>
      </configuration>
    </execution>
  </executions>
</plugin>