elasticsearch引用jar包與其他jar衝突解決辦法
阿新 • • 發佈:2019-02-14
問題提出:
比如你的專案中引用的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)