elasticsearch引用jar包与其他jar冲突解决办法

问题提出:

官网bloghttps://www.elastic.co/blog/to-shade-or-not-to-shade

比如你的项目中引用的Joda 2.1,而elasticsearch 2.2.0引用的Joda 2.8,则在使用elasticsearch的时候以为会先找到Joda 2.1,导致elasticsearch用不起来,所以怎么解决呢

1.新建一个maven项目进行如下配置

<properties>
<elasticsearch.version>2.0.0-beta2</elasticsearch.version>
</properties>
<dependencies>
<dependency>
    <groupId>org.elasticsearch</groupId>
    <artifactId>elasticsearch</artifactId>
    <version>${elasticsearch.version}</version>
</dependency>
<dependency>
    <groupId>org.elasticsearch.plugin</groupId>
    <artifactId>shield</artifactId>
    <version>${elasticsearch.version}</version>
</dependency>
<dependency>
    <groupId>joda-time</groupId>
    <artifactId>joda-time</artifactId>
    <version>2.1</version>
</dependency>
</dependencies>

2.隐藏elasticsearch

<groupId>my.elasticsearch.test</groupId>
<artifactId>es-shaded</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<elasticsearch.version>2.0.0-beta2</elasticsearch.version>
</properties>
<dependencies>
<dependency>
    <groupId>org.elasticsearch</groupId>
    <artifactId>elasticsearch</artifactId>
    <version>${elasticsearch.version}</version>
</dependency>
<dependency>
    <groupId>org.elasticsearch.plugin</groupId>
    <artifactId>shield</artifactId>
    <version>${elasticsearch.version}</version>
</dependency>
</dependencies>
<repositories>
<repository>
    <id>elasticsearch-releases</id>
    http://maven.elasticsearch.org/releases
    <releases>
        <enabled>true</enabled>
        <updatePolicy>daily</updatePolicy>
    </releases>
    <snapshots>
        <enabled>false</enabled>
    </snapshots>
</repository>
</repositories>

3.将elasticsearch中冲突的jar包 

<build>
<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>2.4.1</version>
        <executions>
            <execution>
                <phase>package</phase>
                <goals>
                    <goal>shade</goal>
                </goals>
                <configuration>
                    <relocations>
                        <relocation>
                            <pattern>org.joda</pattern>
                            <shadedPattern>my.elasticsearch.joda</shadedPattern>
                        </relocation>
                    </relocations>
                    <transformers>
                        <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer" />
                    </transformers>
                </configuration>
            </execution>
        </executions>
    </plugin>
</plugins>
</build>

4.你的项目中引用

<!--
This artifact contains all elasticsearch libs including
the shaded version of Joda and Shield
 -->
<dependency>
<groupId>my.elasticsearch.test</groupId>
<artifactId>es-shaded</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
        <!--
         You still use your own Joda version
        -->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.1</version>
</dependency>

5.使用 elasticsearch

TransportClient client = TransportClient.builder()

          .settings(Settings.builder()

                  .put("path.home", ".")

                  .put("shield.user", "username:password")

                  .put("plugin.types", "org.elasticsearch.shield.ShieldPlugin")

          )

          .build();

client.addTransportAddress(new InetSocketTransportAddress(new InetSocketAddress("localhost", 9300)));

6说明


你可以使用org.joda.time.DateTime(2.1版本的),也可以使用my.elasticsearch.joda.time.DateTime(2.8版本的,不推荐用这个,而是直接用org.joda.time.DateTime)












  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值