传递依赖:在 AEM 中使用 Elasticsearch Rest High Client 问题

Tad*_*lić 6 osgi maven elasticsearch aem elasticsearch-api

我正在尝试在Adobe Experience Manager 中使用Java High Level Rest Client来完成Lucene、Solr 和 Elasticsearch 搜索引擎之间比较项目。

我在执行 elasticsearh时遇到了一些问题。这是代码:

  • 父pom.xml中的依赖(核心pom.xml中定义相同)

    <!-- Elasticseach dependencies -->
    <dependency>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>elasticsearch-rest-high-level-client</artifactId>
        <version>7.4.0</version>
    </dependency>
    
    Run Code Online (Sandbox Code Playgroud)
  • 我使用的唯一一行代码来自上面的依赖项

    try (RestHighLevelClient client = new 
    RestHighLevelClient(RestClient.builder(new HttpHost(server, port, 
    protocol),
      new HttpHost(server, secondPort, protocol)));)
    {
    
    }
    catch (ElasticsearchException e)
    {
        LOG.error("Exception: " + e);
    }
    
    Run Code Online (Sandbox Code Playgroud)

协议 = “http”,服务器 = “本地主机”,端口 = 9200,第二端口 = 9201

  • 错误

在此处输入图片说明

  • 来自 IntelliJ 的依赖

在此处输入图片说明

我知道依赖版本通常有问题,但在这种情况下都是7.4.0。此外,elasticsearch 7.4.0v 也在 3 个节点上本地运行。

这个项目是在We.Retail完成的所以很容易复制。此外,这里提供了所有带有此错误的代码:https : //github.com/tadijam64/search-engines-comparison-on-we-retail/tree/elasticsearch-integration AEM 6.4v。

任何信息或想法表示赞赏。

更新 我尝试添加以下内容以在外部嵌入这些依赖项,因为它们不是 OSGi 依赖项:

<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>
Run Code Online (Sandbox Code Playgroud)

错误仍然存​​在。我也尝试将它添加到“导出包”中,但没有任何帮助。

根据Elasticsearch 文档,我需要使用 Elasticsearch

<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
    <version>7.4.0</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

但随后NoClassDefFoundErrors发生。这似乎是传递依赖的问题。不确定,但任何想法都值得赞赏。

可以在此处找到其他一些建议:https : //forums.adobe.com/thread/2653586

我也尝试添加它的传递依赖项,如 org.elasticsearch 和 org.elasticsearch.client,但它不起作用。同样的错误,只是其他类。

AEM 6.4 版,Java 版:jdk1.8.0_191.jdk

Tad*_*lić 4

所以我的猜测是正确的,尽管存在传递依赖关系,但不包括在内<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>
    
    Run Code Online (Sandbox Code Playgroud)

将所有第三方依赖项添加为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>
Run Code Online (Sandbox Code Playgroud)

需要注意的重要事项:

  • 所有第三方依赖项(OSGi 之外的依赖项)必须包含在“Embed-Dependency”中
  • “Embed-Transitive”必须设置为 true 才能包含传递依赖项
  • “Import-Package”必须包含“*;resolution:=Optional”以排除所有无法解析的依赖项,以便程序可以正常运行
  • 由于某种原因,添加“elasticsearch”依赖项时编译时出现错误,这对于此任务并不重要,因此我决定以这种方式忽略它:
<_fixupmessages>"Classes found in the wrong directory";is:=warning</_fixupmessages>
Run Code Online (Sandbox Code Playgroud)

虽然充满挑战,但我终于解决了。Google 上有很多类似或相同的问题,所以我希望这对某人有所帮助。感谢所有试图提供帮助的人。