maven - Transitive Dependency: Using Elasticsearch Rest High Client problem in AEM
问题描述
I am trying to use Java High Level Rest Client in Adobe Experience Manager to finish project of comparison between Lucene, Solr and Elasticsearch search engines.
I am having some problems with elasticsearh implementation. Here is the code:
Dependency in the parent pom.xml (the same is defined in core pom.xml)
<!-- Elasticseach dependencies --> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>7.4.0</version> </dependency>
The only line of code that I am using that is from dependencies above
try (RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost(server, port, protocol), new HttpHost(server, secondPort, protocol)));) { } catch (ElasticsearchException e) { LOG.error("Exception: " + e); }
protocol = "http", server = "localhost", port = 9200, secondPort = 9201
- Error
- Dependencies from IntelliJ
I know that there is usually problem with dependencies versions, but all are 7.4.0 in this case. Also elasticsearch 7.4.0v is running locally on 3 nodes.
This project is done on We.Retail project so it is easy to replicate. Also all the code with this error is available here: https://github.com/tadijam64/search-engines-comparison-on-we-retail/tree/elasticsearch-integration AEM 6.4v.
Any info or idea is appreciated.
UPDATE I tried with adding the following to embed these dependencies externally since they are not OSGi dependencies:
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-scr-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<Embed-Dependency>org.apache.servicemix.bundles.solr-solrj, log4j, noggit, zookeeper,
elasticsearch-rest-high-level-client
</Embed-Dependency>
<Embed-Transitive>true</Embed-Transitive>
<Embed-Directory>OSGI-INF/lib</Embed-Directory>
<Export-Package>we.retail.core.model*</Export-Package>
<Import-Package>
*;resolution:=optional
</Import-Package>
<Private-Package>we.retail.core*</Private-Package>
<Sling-Model-Packages>
we.retail.core.model
</Sling-Model-Packages>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
The error remains. I also tried adding it to the "export-package", but nothing helps.
And by Elasticsearch documentation, all I need to use Elasticsearch is
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.4.0</version>
</dependency>
but then NoClassDefFoundErrors occurs. It seems like a problem with transitive dependencies maybe. Not sure, but any idea is appreciated.
Some other suggestions can be found here: https://forums.adobe.com/thread/2653586
I have also tried adding it's transitive dependencies like org.elasticsearch and org.elasticsearch.client, but it does not work. The same error, just other class.
AEM version 6.4, Java version: jdk1.8.0_191.jdk
解决方案
<Embed-Transitive>true</Embed-Transitive>
所以我的猜测是正确的,尽管存在传递依赖项,但不包括在内。
在AEM上运行elasticsearch作为搜索引擎时,需要执行以下操作:
我在 pom.xml 中添加了所有传递依赖项(版本在 parent/pom.xml 中定义):
<!-- Elasticsearch --> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> </dependency> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-client</artifactId> </dependency> <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> </dependency> <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch-x-content</artifactId> </dependency> <dependency> <groupId>org.elasticsearch.plugin</groupId> <artifactId>rank-eval-client</artifactId> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-imaging</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.elasticsearch.plugin</groupId> <artifactId>lang-mustache-client</artifactId> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpasyncclient</artifactId> </dependency>
在maven-bundle-plugin中将所有第三方依赖项添加为 < Embed-Dependency >非常重要,如下所示:
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<Embed-Dependency>org.apache.servicemix.bundles.solr-solrj, noggit,
elasticsearch-rest-high-level-client,
elasticsearch,
elasticsearch-rest-client,
elasticsearch-x-content,
elasticsearch-core,
rank-eval-client,
lang-mustache-client,
httpasyncclient;
</Embed-Dependency>
<Embed-Transitive>true</Embed-Transitive>
<Embed-Directory>OSGI-INF/lib</Embed-Directory>
<Export-Package>we.retail.core.model*</Export-Package>
<Import-Package>
*;resolution:=optional
</Import-Package>
<Private-Package>
we.retail.core*
</Private-Package>
<Sling-Model-Packages>
we.retail.core.model
</Sling-Model-Packages>
<_fixupmessages>"Classes found in the wrong directory";is:=warning</_fixupmessages>
</instructions>
</configuration>
</plugin>
需要注意的重要事项:
- 所有第三方依赖项(OSGi 之外的)必须包含在“嵌入依赖项”中
- "Embed-Transitive" 必须设置为 true 才能包含传递依赖项
- "Import-Package" 必须包含"*;resolution:=optional" 以排除所有无法解析的依赖项,以便程序可以正常运行
- 出于某种原因,在添加“elasticsearch”依赖项时编译时出现错误,这对该任务并不重要,所以我决定以这种方式忽略它:
<_fixupmessages>"Classes found in the wrong directory";is:=warning</_fixupmessages>
尽管具有挑战性,但我最终解决了它。Google上有很多类似或相同的问题,所以我希望这会对某人有所帮助。感谢所有试图提供帮助的人。
推荐阅读
- protocol-buffers - protobuf 是 grpc 中唯一使用的技术吗?
- python-3.x - 框架关闭 Tkinter 的问题
- r - 在终端中运行 R .ipynb 脚本
- javascript - 将事件侦听器添加到 javascript 文件
- r - 如何计算R中日期的频率?
- sql-server - SQL Server:根据一行的优先级从两行返回行
- ios - 如何根据模型的数组属性中索引的模型的值进行排序
- javascript - 分页数据导致异常行为?
- c++ - 在遍历链接列表以读取数据时,我的函数仅打印到倒数第二个节点,建议一种打印整个列表的方法
- ruby - 没有重复的哈希连接/合并并选择最大键值?