Maven包装失败Lucene,SPI不存在Exception

时间:2016-04-21 09:59:12

标签: java maven lucene spi

我构建了一个用于索引的Java应用程序。我正在使用Apache Lucene 5.3.1。 当我在IDE(IntelJ IDEA 2016.1.1 Community Edition)中运行应用程序时,它运行正常。当我尝试从jar文件运行应用程序时,我得到了这个异常

Exception in thread "main" java.util.ServiceConfigurationError: Cannot instantiate SPI class: org.apache.lucene.codecs.lucene53.Lucene53Codec
    at org.apache.lucene.util.NamedSPILoader.reload(NamedSPILoader.java:77)
    at org.apache.lucene.util.NamedSPILoader.<init>(NamedSPILoader.java:47)
    at org.apache.lucene.util.NamedSPILoader.<init>(NamedSPILoader.java:37)
    at org.apache.lucene.codecs.Codec$Holder.<clinit>(Codec.java:47)
    at org.apache.lucene.codecs.Codec.getDefault(Codec.java:140)
    at org.apache.lucene.index.LiveIndexWriterConfig.<init>(LiveIndexWriterConfig.java:120)
    at org.apache.lucene.index.IndexWriterConfig.<init>(IndexWriterConfig.java:140)
   .
   .
Caused by: java.lang.IllegalArgumentException: An SPI class of type org.apache.lucene.codecs.PostingsFormat 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: [IDVersion]
    at org.apache.lucene.util.NamedSPILoader.lookup(NamedSPILoader.java:109)
    at org.apache.lucene.codecs.PostingsFormat.forName(PostingsFormat.java:112)
    at org.apache.lucene.codecs.lucene53.Lucene53Codec.<init>(Lucene53Codec.java:160)
    at org.apache.lucene.codecs.lucene53.Lucene53Codec.<init>(Lucene53Codec.java:80)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at java.lang.Class.newInstance(Unknown Source)
    at org.apache.lucene.util.NamedSPILoader.reload(NamedSPILoader.java:67)
    ... 13 more

有一个类似的问题here,但使用maven-shade-plugin的解决方案没有帮助。 这是与pom.xml相关的段。

<dependency>
            <groupId>org.apache.lucene</groupId>
            <artifactId>lucene-core</artifactId>
            <version>5.3.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.lucene</groupId>
            <artifactId>lucene-analyzers-common</artifactId>
            <version>5.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.lucene</groupId>
            <artifactId>lucene-queryparser</artifactId>
            <version>5.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.lucene</groupId>
            <artifactId>lucene-codecs</artifactId>
            <version>5.3.1</version>
        </dependency>
    ...
    <transformers>
        <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
            <resource>META-INF/services/org.apache.lucene.analysis.util.CharFilterFactory</resource>
        </transformer>
        <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
            <resource>META-INF/services/org.apache.lucene.analysis.util.TokenFilterFactory</resource>
        </transformer>
        <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
            <resource>META-INF/services/org.apache.lucene.analysis.util.TokenizerFactory</resource>
        </transformer>
        <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
            <resource>META-INF/services/org.apache.lucene.codecs.Codec</resource>
        </transformer>
        <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
            <resource>META-INF/services/org.apache.lucene.codecs.DocValuesFormat</resource>
        </transformer>
        <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
            <resource>META-INF/services/org.apache.lucene.codecs.PostingsFormat</resource>
        </transformer>
</transformers>

问题在于,由于某种原因,它试图从Lucene50加载一些东西。

任何帮助都很有价值!

1 个答案:

答案 0 :(得分:2)

这就是我对这个问题的解决方案(Maven)

`<plugin>
    <artifactId>maven-shade-plugin</artifactId>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
            <filters>
                <filter>
                    <artifact>*:*</artifact>
                    <excludes>
                        <exclude>META-INF/*.RSA</exclude>
                        <exclude>META-INF/*.SF</exclude>
                    </excludes>
                </filter>
            </filters>
                <transformers>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                        <resource>META-INF/services/org.apache.lucene.codecs.Codec</resource>
                    </transformer>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                        <resource>META-INF/services/org.apache.lucene.codecs.DocValuesFormat</resource>
                    </transformer>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                        <resource>META-INF/services/org.apache.lucene.codecs.PostingsFormat</resource>
                    </transformer>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass>mainClassName</mainClass>
                    </transformer>
                </transformers>
            </configuration>
        </execution>
    </executions>
</plugin>
`

文件org.apache.lucene.codecs.Codec,org.apache.lucene.codecs.DocValuesFormat,org.apache.lucene.codecs.PostingsFormat包含有关项目中使用的编解码器的信息。它们应该放在META-INF / services目录中(如果不存在则应该创建)。在我的情况下问题出现在几个postsFormats,Codecs和DocValuesFormats中,我将它们添加到文件中,问题就解决了。

org.apache.lucene.codecs.Codec文件的示例:

org.apache.lucene.codecs.simpletext.SimpleTextCodec
org.apache.lucene.codecs.appending.AppendingCodec
org.apache.lucene.codecs.lucene40.Lucene40Codec
org.apache.lucene.codecs.lucene410.Lucene410Codec
org.apache.lucene.codecs.lucene41.Lucene41Codec

org.apache.lucene.codecs.DocValuesFormat文件的示例:

org.apache.lucene.codecs.memory.MemoryDocValuesFormat
org.apache.lucene.codecs.memory.DirectDocValuesFormat
org.apache.lucene.codecs.lucene410.Lucene410DocValuesFormat

org.apache.lucene.codecs.PostingsFormat文件的示例:

org.apache.lucene.codecs.bloom.BloomFilteringPostingsFormat
org.apache.lucene.codecs.memory.DirectPostingsFormat
org.apache.lucene.codecs.memory.FSTOrdPostingsFormat
org.apache.lucene.codecs.memory.FSTPostingsFormat
org.apache.lucene.codecs.memory.MemoryPostingsFormat
org.apache.lucene.codecs.lucene40.Lucene40PostingsFormat
org.apache.lucene.codecs.lucene41.Lucene41PostingsFormat

在你的情况下,你应该对Lucene50做同样的事。