如何使用 Maven Shade 插件?

How to use Maven Shade plugin?

在我的项目中,我想避免 neo4j lucene indexer(使用 lucene 版本 - 3.6.2)和 apache lucene(lucene 版本 - 5.3.0)的版本冲突。为此,我想使用 Maven 阴影插件。实际上,我在我的项目 'pom.xml' 文件中添加了插件,但问题并未解决。我遇到异常 -

Exception in thread "main" java.lang.NoSuchMethodError: org.apache.lucene.analysis.standard.StandardAnalyzer: method <init>()V not found
    at com.sessa.col.spr.act.dictionary.DictionaryConfiguration.writerConfiguration(DictionaryConfiguration.java:124)
    at com.sessa.col.spr.act.process_flow.Flow.startProcess(Flow.java:59)
    at com.sessa.col.spr.act.process_flow.FlowHandler.main(FlowHandler.java:17)

看来又是版本冲突导致的。我想,我没有以正确的方式使用 Maven Shade 插件。应该如何使用?

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.sessa.col.spr.act</groupId>
<artifactId>Color-Spreading-Activation</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Color-Spreading-Activation</name>
<url>http://maven.apache.org</url>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <neo4j-version>2.2.5</neo4j-version>
</properties>



<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.4</version>
    </dependency>

    <dependency>
        <groupId>org.neo4j</groupId>
        <artifactId>neo4j</artifactId>
        <version>${neo4j-version}</version>
    </dependency>

    <dependency>
        <groupId>edu.stanford.nlp</groupId>
        <artifactId>stanford-corenlp</artifactId>
        <version>3.5.2</version>
    </dependency>

    <dependency>
        <groupId>edu.stanford.nlp</groupId>
        <artifactId>stanford-parser</artifactId>
        <version>3.5.2</version>
    </dependency>

    <dependency>
        <groupId>edu.stanford.nlp</groupId>
        <artifactId>stanford-corenlp</artifactId>
        <version>3.5.2</version>
        <classifier>models</classifier>
    </dependency>

    <dependency>
        <groupId>com.sparsity</groupId>
        <artifactId>sparkseejava</artifactId>
        <version>5.1.0</version>
    </dependency>

    <dependency>
        <groupId>org.apache.jena</groupId>
        <artifactId>jena-tdb</artifactId>
        <version>1.1.2</version>
    </dependency>

    <dependency>
        <groupId>org.apache.opennlp</groupId>
        <artifactId>opennlp-tools</artifactId>
        <version>1.5.3</version>
    </dependency>

    <dependency>
        <groupId>org.apache.lucene</groupId>
        <artifactId>lucene-core</artifactId>
        <version>5.3.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.lucene</groupId>
        <artifactId>lucene-analyzers-common</artifactId>
        <version>5.3.0</version>
    </dependency>

    <dependency>
        <groupId>org.apache.lucene</groupId>
        <artifactId>lucene-queryparser</artifactId>
        <version>5.3.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.lucene</groupId>
        <artifactId>lucene-queries</artifactId>
        <version>5.3.0</version>
    </dependency>

</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.3</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <createDependencyReducedPom>false</createDependencyReducedPom>
                        <relocations>
                            <relocation>
                                <pattern>org.apache.lucene</pattern>
                                <shadedPattern>shaded_lucene_3_6_2.org.apache.lucene</shadedPattern>
                            </relocation>
                        </relocations>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

<repositories>
    <repository>
        <id>neo4j-repo</id>
        <name>Neo4j Repository</name>
        <url>http://m2.neo4j.org/content/repositories/releases</url>
    </repository>
</repositories> 

我怀疑 maven-shade 能帮到你。您基本上希望同时使用同一个 jar 的多个版本 - lucene 3.6.2 和 5.x.y。

我知道的唯一解决方案是使用类加载器分离。

然而,通过将 Neo4j 和您的代码分离到单独的 JVM 中来重构架构以防止出现该问题可能是值得的。

  1. 将此项目添加到 eclipse -> https://github.com/lagodiuk/neo4j-uber-jar.
  2. 使用 mvn-install 并创建 ~SNAPSHOT.jar
  3. 将该 .jar 添加到您的项目(有冲突)
  4. 从该项目中删除 neo4j maven 依赖项。