本文介绍了“未解决的要求:导入包装”对于不在我的build.gradle中的模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Liferay 7 SP4 FP30模块中使用Elasticsearch的Client Java类,所以我写了 build.gradle

 依赖项{
compileOnly组:com.liferay,名称:com.liferay.portal.search.elasticsearch,版本:2.1.14
compileOnly组:com.liferay,名称:org.elasticsearch,版本:2.2.0.LIFERAY-PATCHED-1

compileOnly组:biz.aQute。 bnd,名称:biz.aQute.bndlib,版本:3.1.0
compileOnly组:com.liferay,名称:com.liferay.osgi.util,版本:3.0。 0
compileOnly组:com.liferay,名称:com.liferay.portal.spring.extender,版本:2.0.0
compileOnly组:com.liferay.portal ,名称:com.liferay.portal.kernel,版本:2.0.0
compileOnly组:com.liferay,名称:com.liferay.portal.security.audit.api,版本:2.0.0
compileOnly组:com.liferay,名称:com.liferay.portal.configuration.metatype,版本:2.0.0
compileOnly group:org.osgi,name:org.osgi.compendium,version:5.0.0
}

...以及包含代码的Java类,如 import com.liferay.portal.search.elasticsearch.connection.ElasticsearchConnectionManager; 客户端客户端= elasticsearchConnectionManager.getClient();



它可以正常工作。 b
$ b

但是,当我尝试启动模块时,发生此错误:

  org.osgi .framework.BundleException:无法解析模块:mymodule [548] 
未解析的需求:导入包:com.liferay.portal.search.elasticsearch.connection

为什么会发生这种情况?我的 build.gradle 没有提及以 .connection 结尾的和。

解决方案

@gjoranv是正确的,只是因为你在 gradle.build 中,它并不意味着它会在你的环境。首先,这个错误是由于缺少一个使用过的包,这是Java的传统意义上的。因此,您将需要一个模块,如jar文件所示,这个模块是公开的。



由于liferay对于Elastic Search依赖于版本,并且依赖在事故版本中,您可能会使用未暴露的软件包,并强制曝光,通常通过Uber模块。

如果你感觉幸运,你也可以使用compileInclude而不是compileOnly。以这种方式包括库可能会弄得一团糟,因为它会将jar包嵌入到你的jar文件中,并公开所有的包。



另一种可能性通常不那么激进嵌入jar,并在你的bundle中设置类路径。要做到这一点,你只需要将你的依赖声明为compile,并在你的bnd.bnd文件中添加类路径。 (听起来比它更难,它应该是一个微不足道的过程)。

另一个需要考虑的问题是与ElasticSearch和Liferay部署的对齐:2.2-2.4 .x,但这只是因为如果您的对象被其他包使用或与旧ES接口时,您可能会遇到类转换异常和API不匹配。
$ b

嵌入示例:

gradle.build

  compileorg.apache.httpcomponents:httpclient
compileorg.apache.httpcomponents:httpcore



bnd.bnd

  -includeresource:lib / httpclient.jar = httpclient-4.5.3.jar ,\ 
lib / httpcore.jar = httpcore-4.4.6.jar

Bundle-ClassPath:。,lib / httpclient.jar,lib / httpcore.jar


I want to use Elasticsearch's Client Java class within a Liferay 7 SP4 FP30 module, so I wrote this build.gradle:

dependencies {
    compileOnly group: "com.liferay", name: "com.liferay.portal.search.elasticsearch", version: "2.1.14"
    compileOnly group: "com.liferay", name: "org.elasticsearch", version: "2.2.0.LIFERAY-PATCHED-1"

    compileOnly group: "biz.aQute.bnd", name: "biz.aQute.bndlib", version: "3.1.0"
    compileOnly group: "com.liferay", name: "com.liferay.osgi.util", version: "3.0.0"
    compileOnly group: "com.liferay", name: "com.liferay.portal.spring.extender", version: "2.0.0"  
    compileOnly group: "com.liferay.portal", name: "com.liferay.portal.kernel", version: "2.0.0"
    compileOnly group: "com.liferay", name: "com.liferay.portal.security.audit.api", version: "2.0.0"
    compileOnly group: "com.liferay", name: "com.liferay.portal.configuration.metatype", version: "2.0.0"
    compileOnly group: "org.osgi", name: "org.osgi.compendium", version: "5.0.0"
}

... and a Java class containing code such as import com.liferay.portal.search.elasticsearch.connection.ElasticsearchConnectionManager; and Client client = elasticsearchConnectionManager.getClient();

It builds fine.

But when I try to start the module, this error happens:

org.osgi.framework.BundleException: Could not resolve module: mymodule [548]
  Unresolved requirement: Import-Package: com.liferay.portal.search.elasticsearch.connection

Why is this happening? My build.gradle does not mention this module ending in .connection, and Maven does not seem to have any such module.

解决方案

@gjoranv is correct, just because you in is on your gradle.build it does not mean it will be in your environment.

First things first, the error is due to the lack of a used package, in Java's conventional sense. So you will need a module, as represented by a jar file, that makes this package public.

As liferay is pretty version dependent when it comes to Elastic Search, and relies on accident versions, you might get away with using not exposed packages, and forcing the exposure, normally through a Uber module.

If you are feeling lucky, you can also use compileInclude, instead of compileOnly. Including the library this way will possibly make a mess, as it will embed the jar inside your jar and expose all packages.

Another possibility, which normally is way less aggressive is to embed the jar, and set the classpath inside your bundle. To do this you just need to declare your dependency as compile, and add the classpath in your bnd.bnd file. (it sounds harder than it is, it should be a trivial process)

Another issue to have in mind is the alignment with your ElasticSearch and you liferay deployment:2.2-2.4.x but this is just because you might fall into class conversion exceptions and API mismatch if your objects are used by other bundles or when interfacing with an old ES.


Embedding example:

gradle.build

compile "org.apache.httpcomponents:httpclient"
compile "org.apache.httpcomponents:httpcore"

bnd.bnd

-includeresource: lib/httpclient.jar=httpclient-4.5.3.jar,\
                  lib/httpcore.jar=httpcore-4.4.6.jar

Bundle-ClassPath: ., lib/httpclient.jar, lib/httpcore.jar

这篇关于“未解决的要求:导入包装”对于不在我的build.gradle中的模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 16:03