Liferay 7 将第三方库包 (JsonPath) 包含到 portlet 模块中

Liferay 7 Including third party library package (JsonPath) into portlet module

我正在尝试将 JsonPath 库包含到我的 Liferay MVC Portlet 中。

我在 Liferay 帮助中心找到了帖子:

https://help.liferay.com/hc/en-us/articles/360028710272-Resolving-Third-Party-Library-Package-Dependencies

但我仍然不知道该怎么做。

我读到我应该在 build.gradle 文件中使用 compileInclude,因为它还包括我想要的库的依赖项。

原来是这样

#build.gradle

dependencies {
    compileOnly group: "com.liferay.portal", name: "release.portal.api"
    cssBuilder group: "com.liferay", name: "com.liferay.css.builder", version: "3.0.2"
    compileInclude group: 'com.jayway.jsonpath', name: 'json-path', version: '2.4.0'
}

当我构建一个部署到 Liferay 的 *.jar 时,我遇到了那个错误。

2021-07-15 06:56:16.994 INFO  [com.liferay.portal.kernel.deploy.auto.AutoDeployScanner][AutoDeployDir:272] Processing mycustomportlet2-1.0.0.jar
2021-07-15 06:56:26.140 ERROR [fileinstall-directory-watcher][DirectoryWatcher:1159] Unable to start bundle: file:/opt/liferay/osgi/modules/mycustomportlet2-1.0.0.jar
com.liferay.portal.kernel.log.LogSanitizerException: org.osgi.framework.BundleException: Could not resolve module: mycustomportlet2 [1411]_  Unresolved requirement: Import-Package: com.google.gson_ [Sanitized]
    at org.eclipse.osgi.container.Module.start(Module.java:444) ~[org.eclipse.osgi.jar:?]
    at org.eclipse.osgi.internal.framework.EquinoxBundle.start(EquinoxBundle.java:428) ~[org.eclipse.osgi.jar:?]
    at com.liferay.portal.file.install.internal.DirectoryWatcher._startBundle(DirectoryWatcher.java:1142) [bundleFile:?]
    at com.liferay.portal.file.install.internal.DirectoryWatcher._startBundles(DirectoryWatcher.java:1175) [bundleFile:?]
    at com.liferay.portal.file.install.internal.DirectoryWatcher._startAllBundles(DirectoryWatcher.java:1120) [bundleFile:?]
    at com.liferay.portal.file.install.internal.DirectoryWatcher._process(DirectoryWatcher.java:1032) [bundleFile:?]
    at com.liferay.portal.file.install.internal.DirectoryWatcher.run(DirectoryWatcher.java:272) [bundleFile:?]

我尝试在 build.gradle 中也包含 gson,但随后错误显示另一个库有未解决的要求。

我正在使用 Liferay Portal 7.4-ga2 docker 图片 Java 11.

如果您知道如何在不手动包含很多依赖项的情况下执行此操作,我将非常感谢为此提供的解决方案。

编辑

我创建了 build.gradle 文件,其中包含工作所需的所有内容。看起来是这样的:

dependencies {
    compileOnly group: "com.liferay.portal", name: "release.portal.api"
    cssBuilder group: "com.liferay", name: "com.liferay.css.builder", version: "3.0.2"
    compileInclude group: 'com.jayway.jsonpath', name: 'json-path', version: '2.6.0'
    compileInclude group: 'com.google.code.gson', name: 'gson', version: '2.8.7'
    compileInclude  group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.12.3'
    compileInclude group: 'org.apache.tapestry', name: 'tapestry-json', version: '5.7.2'
    compileInclude group: 'org.apache.tapestry', name: 'commons', version: '5.7.2'
    compileInclude group: 'org.codehaus.jettison', name: 'jettison', version: '1.4.1'
    compileInclude group: 'org.json', name: 'json', version: '20210307'
    compileInclude group: 'org.slf4j.impl', name: 'log4j12', version: '1.7.2'
}

repositories {
    maven {
        url "https://maven.averbis.com/m2/"
    }
}

如果您 compileInclude 外部资源(这是可能的,但应该是您最后的选择),不幸的是您还需要包括 所有传递依赖项。您包含 jayway/jsonpath,但缺少 gson。所以你需要 compileInclude gson。正如您所说,当您这样做时,会丢失一个不同的库 - 因此您也需要包含它。

这就是为什么这应该是你最后的选择的部分原因。

另一种方法是:检查 jayway/jsonpath 或 gson 本身是否是 OSGi 捆绑包 - 在这种情况下,您可以将它们放入 Liferay 的 deploy 文件夹中,它们将被动态解析。当然,在这种情况下,它们的传递依赖性也需要是可解析的,因此您可能需要部署比这两个更多的包。但是这样一来,所有使用这些库的模块都将共享同一个包。

无论哪种方式,您都可以检查包的 MANIFEST.mf 导入以确定它们所依赖的内容。注意:那里有强制和可选的依赖项。您需要满足您正在使用的必填项和可选项。如果有问题的库不是捆绑包,那么它们会以不同的方式管理它们的依赖关系。我至少会建议项目团队对他们的包进行 OSGi 化 - 但这是长期的修复 运行.

在 Liferay 大学的(免费,需要注册)课程 OSGi Basics 上有一章是关于这个的,叫做“Bringing along your dependencies”(免责声明:由你真实提供),我仍然喜欢动画特效可视化compileInclude 的选项及其对文件大小的影响)