如何使用Maven Shade插件?

时间:2015-09-01 17:49:19

标签: java maven neo4j maven-shade-plugin

在我的项目中,我想避免版本冲突neo4j lucene indexer(使用lucene版本 - 3.6.2)和apache lucene(lucene版本 - 5.3.0)。为此,我想使用Maven shade插件。实际上,我在我的项目'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> 

2 个答案:

答案 0 :(得分:1)

我怀疑maven-shade会帮助你。你基本上想要同一个jar的多个版本 - 同时使用lucene 3.6.2和5.x.y。

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

然而,通过将Neo4j和您的代码分离为单独的JVM,可能需要重构体系结构以防止出现此问题。

答案 1 :(得分:0)

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