'Example of Liferay Portlet with CDI Injection
i have been trying to make in Eclipse a Liferay JSF Portlet with CDI Injection for Liferay 7.3. Unfortunately i have been very unsuccessfull. I have tried lots of examples online, even the instructions from Liferay. I dont know what i am doing wrong. Can someone help me with an example of a Liferay Project?
I have followed the instructions of this link after getting the latest maven in my pc: https://faces.liferay.dev/home/-/archetype-portlet/liferay-portal-version/7.3/jsf-version/2.2/component-suite/jsf/build-tool/maven/format/thick/cdi/enabled
I executed this command in prompt:
mvn archetype:generate \
-DarchetypeGroupId=com.liferay.faces.archetype \
-DarchetypeArtifactId=com.liferay.faces.archetype.jsf.portlet \
-DarchetypeVersion=6.1.0 \
-DgroupId=com.mycompany \
-DartifactId=com.mycompany.my.jsf.portlet
and get this error:
> D:\>mvn archetype:generate \ [INFO] Scanning for projects... [INFO]
> ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO]
> ------------------------------------------------------------------------ [INFO] Total time: 0.230 s [INFO] Finished at:
> 2022-02-26T19:40:52+01:00 [INFO]
> ------------------------------------------------------------------------ [ERROR] The goal you specified requires a project to execute but there
> is no POM in this directory (D:\). Please verify you invoked Maven
> from the correct directory. -> [Help 1] [ERROR] [ERROR] To see the
> full stack trace of the errors, re-run Maven with the -e switch.
> [ERROR] Re-run Maven using the -X switch to enable full debug logging.
> [ERROR] [ERROR] For more information about the errors and possible
> solutions, please read the following articles: [ERROR] [Help 1]
> http://cwiki.apache.org/confluence/display/MAVEN/MissingProjectException
>
> D:\> -DarchetypeGroupId=com.liferay.faces.archetype \ Der Befehl
> "-DarchetypeGroupId" ist entweder falsch geschrieben oder konnte nicht
> gefunden werden.
>
> D:\> -DarchetypeArtifactId=com.liferay.faces.archetype.jsf.portlet \
> Der Befehl "-DarchetypeArtifactId" ist entweder falsch geschrieben
> oder konnte nicht gefunden werden.
>
> D:\> -DarchetypeVersion=6.1.0 \ Der Befehl "-DarchetypeVersion" ist
> entweder falsch geschrieben oder konnte nicht gefunden werden.
>
> D:\> -DgroupId=com.mycompany \ Der Befehl "-DgroupId" ist entweder
> falsch geschrieben oder konnte nicht gefunden werden.
>
> D:\> -DartifactId=com.mycompany.my.jsf.portlet Der Befehl
> "-DartifactId" ist entweder falsch geschrieben oder konnte nicht
> gefunden werden.
Then i tried creating a Jsf Liferay Project with Liferay Dev Studio and then using this maven command from the link, and i only get a list of maven archetypes to choose from. I pick this one:
2353: remote -> org.jboss.weld.archetypes:jboss-jsf-weld-servlet-webapp (An archetype that generates a starter JSF webapp project with CDI capabilities for Servlet containers)
Then i enter the same groupId and artifactId from the created Project im Liferay Studio. Unfortunately the portlet doesnt work after that. I get this error in Liferay Dev studio, after deploying this portlet:
19:28:00,479 ERROR [ApplicationOSGiCDIImpl:65] org.jboss.weld.module.web.el.WeldELContextListener cannot be cast to javax.el.ELContextListener
java.lang.ClassCastException: org.jboss.weld.module.web.el.WeldELContextListener cannot be cast to javax.el.ELContextListener
at com.liferay.faces.util.osgi.internal.ApplicationOSGiCDIImpl.<init>(ApplicationOSGiCDIImpl.java:62)
...
2022-02-26 19:28:01.514 ERROR [main][WabBundleProcessor:244] Catastrophic initialization failure! Shutting down JSFWithCDI WAB due to: java.lang.LinkageError: loader constraint violation: loader (instance of org/eclipse/osgi/internal/loader/EquinoxClassLoader) previously initiated loading for a different type with name "javax/el/ExpressionFactory"
java.lang.RuntimeException: java.lang.LinkageError: loader constraint violation: loader (instance of org/eclipse/osgi/internal/loader/EquinoxClassLoader) previously initiated loading for a different type with name "javax/el/ExpressionFactory"
I try deployng it on a liferay tomcat 9 server and it doenst even deploy. I dont know what i am doing wrong.
This are my main config files for a jsf portlet
pom.xml:
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>JSFWithCDI</groupId>
<artifactId>JSFWithCDI</artifactId>
<version>1.0.0</version>
<packaging>war</packaging>
<name>JSFWithCDI</name>
<properties>
<owb.version>2.0.26</owb.version>
</properties>
<profiles>
<profile>
<id>cdi</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
</profiles>
<dependencies>
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.faces</groupId>
<artifactId>javax.faces-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.faces</artifactId>
<version>2.2.20</version>
<scope>${faces.impl.scope}</scope>
</dependency>
<dependency>
<groupId>com.liferay.faces</groupId>
<artifactId>com.liferay.faces.bridge.ext</artifactId>
<version>6.1.0</version>
<scope>${war.dependency.scope}</scope>
</dependency>
<dependency>
<groupId>com.liferay.faces</groupId>
<artifactId>com.liferay.faces.bridge.impl</artifactId>
<version>4.2.0</version>
<scope>${war.dependency.scope}</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
<scope>${war.dependency.scope}</scope>
</dependency>
<dependency>
<groupId>org.jboss.weld.servlet</groupId>
<artifactId>weld-servlet</artifactId>
<version>1.1.10.Final</version>
</dependency>
<dependency>
<groupId>com.liferay.cdi</groupId>
<artifactId>cdi-portlet-bridge-shared</artifactId>
<version>6.2.0.2</version>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-atinject_1.0_spec</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jcdi_2.0_spec</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-interceptor_1.2_spec</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-annotation_1.3_spec</artifactId>
<version>1.0</version>
</dependency>
<!-- CDI Impl -->
<dependency>
<groupId>org.apache.openwebbeans</groupId>
<artifactId>openwebbeans-impl</artifactId>
<version>${owb.version}</version>
</dependency>
<dependency>
<groupId>org.apache.openwebbeans</groupId>
<artifactId>openwebbeans-web</artifactId>
<version>${owb.version}</version>
</dependency>
<dependency>
<groupId>org.apache.openwebbeans</groupId>
<artifactId>openwebbeans-jsf</artifactId>
<version>${owb.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
and this is web.xml:
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<!-- Seting the JSF 2 PROJECT_STAGE to Development will cause the JSF implementation
will do the following at runtime: -->
<!-- 1. Log more verbose messages. -->
<!-- 2. Render tips and/or warnings in the view markup. -->
<!-- 3. Cause the default ExceptionHandler to display a developer-friendly
error page. -->
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<!-- JSF 2.2 now allows for composite components and resources to be hidden
under WEB-INF -->
<context-param>
<param-name>javax.faces.WEBAPP_RESOURCES_DIRECTORY</param-name>
<param-value>/WEB-INF/resources</param-value>
</context-param>
<!-- Although the FacesServlet will not be invoked by any portlet requests,
it is required to initialize JSF. -->
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<security-constraint>
<display-name>Prevent direct access to Facelet XHTML</display-name>
<web-resource-collection>
<web-resource-name>Facelet XHTML</web-resource-name>
<url-pattern>*.xhtml</url-pattern>
</web-resource-collection>
<auth-constraint />
</security-constraint>
<filter>
<filter-name>WeldCrossContextFilter</filter-name>
<filter-class>org.jboss.weld.servlet.WeldCrossContextFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>WeldCrossContextFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>INCLUDE</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>ERROR</dispatcher>
</filter-mapping>
<listener>
<listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
</listener>
<filter>
<filter-name>CDICrossContextFilter</filter-name>
<filter-class>com.liferay.cdi.portlet.bridge.CDICrossContextFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CDICrossContextFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>INCLUDE</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>ERROR</dispatcher>
</filter-mapping>
<listener>
<listener-class>com.liferay.cdi.portlet.bridge.CDIContextListener</listener-class>
</listener>
</web-app>
This is portlet.xml:
<?xml version="1.0"?>
<portlet-app
xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd"
version="2.0">
<portlet>
<portlet-name>JSFWithCDI</portlet-name>
<display-name>JSFWithCDI</display-name>
<portlet-class>javax.portlet.faces.GenericFacesPortlet</portlet-class>
<init-param>
<name>javax.portlet.faces.defaultViewId.view</name>
<value>/WEB-INF/views/view.xhtml</value>
</init-param>
<expiration-cache>0</expiration-cache>
<supports>
<mime-type>text/html</mime-type>
</supports>
<portlet-info>
<title>JSFWithCDI</title>
<short-title>JSFWithCDI</short-title>
<keywords>JSFWithCDI</keywords>
</portlet-info>
<security-role-ref>
<role-name>administrator</role-name>
</security-role-ref>
<security-role-ref>
<role-name>guest</role-name>
</security-role-ref>
<security-role-ref>
<role-name>power-user</role-name>
</security-role-ref>
<security-role-ref>
<role-name>user</role-name>
</security-role-ref>
</portlet>
<filter>
<filter-name>CDIPortletFilter</filter-name>
<filter-class>com.liferay.cdi.portlet.bridge.CDIPortletFilter</filter-class>
<lifecycle>ACTION_PHASE</lifecycle>
<lifecycle>EVENT_PHASE</lifecycle>
<lifecycle>RENDER_PHASE</lifecycle>
<lifecycle>RESOURCE_PHASE</lifecycle>
</filter>
<filter-mapping>
<filter-name>CDIPortletFilter</filter-name>
<portlet-name>JSFWithCDI</portlet-name>
</filter-mapping>
</portlet-app>
and i also have beans.xml and faces-config.xml: beans.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
</beans>
faces-config.xml:
<?xml version="1.0"?>
<faces-config version="2.2"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
>
<lifecycle>
<phase-listener>com.liferay.faces.util.lifecycle.DebugPhaseListener</phase-listener>
</lifecycle>
</faces-config>
This is the structure of my WEB-INF:

Here is my project im github: https://github.com/minigalli/JSFWithCDI
Thanks in Advance
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
