Gradle 说 "MyPortlet is not assignable to specified service com.liferay.portal.kernel.model.Portlet"

Gradle says "MyPortlet is not assignable to specified service com.liferay.portal.kernel.model.Portlet"

我得到这个 Gradle 错误:

> Task :jar
[Class my.MyPortlet is not assignable to specified service com.liferay.portal.kernel.model.Portlet]

...对于这个极简主义的 Liferay 7 portlet(the official documentation 的简化):

package my;

import com.liferay.portal.kernel.model.Portlet;
import com.liferay.portal.kernel.portlet.bridges.mvc.MVCPortlet;
import org.osgi.service.component.annotations.Component;

@Component(
    service = Portlet.class
)
public class MyPortlet extends MVCPortlet {
}

运行 Gradle 在调试模式下没有给我更多线索:

[aQute.bnd.osgi.Builder] parseClassFile(): path=javax.portlet.PortletRequest resource=:/home/nico/.gradle/caches/modules-2/files-2.1/javax.portlet/portlet-api/2.0/1cd72f2a37fcf8ab9893a9468d7ba71c85fe2653/portlet-api-2.0.jar(javax/portlet/PortletRequest.class):
[aQute.bnd.osgi.Builder] end
[aQute.bnd.osgi.Builder] begin com.liferay.ant.bnd.npm.NpmAnalyzerPlugin@39735dcf
[aQute.bnd.osgi.Builder] end
[aQute.bnd.osgi.Builder] begin com.liferay.ant.bnd.resource.bundle.ResourceBundleLoaderAnalyzerPlugin@4e811a19
[aQute.bnd.osgi.Builder] end
[aQute.bnd.osgi.Builder] parseClassFile(): path=my/MyPortlet.class resource=/home/nico/my/build/classes/java/main/my/MyPortlet.class
[aQute.bnd.osgi.Builder] begin com.liferay.ant.bnd.sass.SassAnalyzerPlugin@526cd630
[aQute.bnd.osgi.Builder] end
[aQute.bnd.osgi.Builder] begin com.liferay.ant.bnd.service.ServiceAnalyzerPlugin@a73dc4
[aQute.bnd.osgi.Builder] end
[aQute.bnd.osgi.Builder] begin com.liferay.ant.bnd.social.SocialAnalyzerPlugin@2399853a
[aQute.bnd.osgi.Builder] end
[aQute.bnd.osgi.Builder] begin aQute.lib.spring.SpringComponent@650e1282
[aQute.bnd.osgi.Builder] end
[aQute.bnd.osgi.Builder] begin com.liferay.ant.bnd.spring.SpringDependencyAnalyzerPlugin@51e79545
[aQute.bnd.osgi.Builder] end
[aQute.bnd.osgi.Builder] parseClassFile(): path=my/MyPortlet.class resource=/home/nico/my/build/classes/java/main/my/MyPortlet.class
[aQute.bnd.osgi.Verifier] Trying pre-plugin com.liferay.ant.bnd.jsp.JspAnalyzerPlugin
[aQute.bnd.osgi.Verifier] Trying pre-plugin com.liferay.ant.bnd.npm.NpmAnalyzerPlugin
[aQute.bnd.osgi.Verifier] Trying pre-plugin com.liferay.ant.bnd.resource.bundle.ResourceBundleLoaderAnalyzerPlugin
[aQute.bnd.osgi.Verifier] Trying pre-plugin com.liferay.ant.bnd.sass.SassAnalyzerPlugin
[aQute.bnd.osgi.Verifier] Trying pre-plugin com.liferay.ant.bnd.service.ServiceAnalyzerPlugin
[aQute.bnd.osgi.Verifier] Trying pre-plugin com.liferay.ant.bnd.social.SocialAnalyzerPlugin
[aQute.bnd.osgi.Verifier] Trying pre-plugin aQute.lib.spring.SpringComponent
[aQute.bnd.osgi.Verifier] Trying pre-plugin com.liferay.ant.bnd.spring.SpringDependencyAnalyzerPlugin
[org.dm.gradle.plugins.bundle.JarBuilder] [Class my.MyPortlet is not assignable to specified service com.liferay.portal.kernel.model.Portlet]

Google 对于 "is not assignable to specified service" 只有 9 个结果。

我做错了什么?

aQute.bnd.component.AnnotationReader tries to parse the service = Portlet.class part and the problem is that aQute.bnd.osgi.Analyzer.assignable(MyPortlet, com.liferay.portal.kernel.model.Portlet) returns 错误。

让我们遵循 class 层次结构:

题目中的portlet实现

com.liferay.portal.kernel.portlet.bridges.mvc.MVCPortlet

com.liferay.portal.kernel.portlet.LiferayPortlet

javax.portlet.GenericPortlet

javax.portlet.Portlet

这提示我们导入语句是错误的。应该是:

import javax.portlet.Portlet;

... 而不是:

import com.liferay.portal.kernel.model.Portlet;

修复后,构建成功

使用您的源导入:

import com.liferay.portal.kernel.model.Portlet;

然后:

@Component( service = Portlet.class )

您断言您的 class MyPortlet 实现了 com.liferay.portal.kernel.model.Portlet。但事实并非如此。 com.liferay.portal.kernel.portlet.bridges.mvc.MVCPortlet 实施 javax.portlet.Portlet.

将源导入从 com.liferay.portal.kernel.model.Portlet 更改为 javax.portlet.Portlet