-
Book Overview & Buying
-
Table Of Contents
Liferay 6.x Portal Enterprise Intranets Cookbook
By :
This recipe is very specific, because it shows how to generate a new portlet, install it on Liferay, and import it to the Eclipse IDE. Many recipes from this book assume that the user knows how to generate a new plugin, such as portlet, hook, or web. We will show you how to generate a new portlet using Apache Maven archetypes. The whole book assumes that you use Apache Maven to compile and deploy new portlets.
In order to correctly generate a new portlet, you need to have the following software stack:
We also assume that you properly set the developer's environment, which was described in the previous recipe.
There are three phases to achieve our goal: generating a new portlet, compiling it, and deploying and importing it to the Eclipse IDE.
The first thing we need to do is to create a Maven project. In order to generate it, follow these steps:
${liferay.home}/workspace folder.mvn archetype:generate -Dfilter=liferay-portlet-archetype.com.liferay.maven.archetypes:liferay-portlet-archetype. In our list, it is number 1:Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): : 1
24.Define value for property 'groupId': : com.packtpub.portlet Define value for property 'artifactId': : first-portlet Define value for property 'version': 1.0-SNAPSHOT: : Define value for property 'package': com.packtpub.portlet: : Confirm properties configuration: groupId: com.packtpub.portlet artifactId: first-portlet version: 1.0-SNAPSHOT package: com.packtpub.portlet Y: : y
workspace folder, a portlet called first-portlet should be generated.With Apache Maven, it is easy to compile and deploy a portlet. Before invoking the Maven command, users have to set specific properties in the pom.xml file.
${liferay.home}/workspace/first-portlet folder and edit the pom.xml file.<build> section, add the following properties definition:<properties>
<liferay.version>6.2.2</liferay.version>
<liferay.maven.plugin.version>6.2.2</liferay.maven.plugin.version>
<liferay.auto.deploy.dir>${liferay.home}/deploy</liferay.auto.deploy.dir>
<liferay.app.server.deploy.dir>${liferay.home}/tomcat-7.0.42/webapps</liferay.app.server.deploy.dir>
<liferay.app.server.lib.global.dir>${liferay.home}/tomcat-7.0.42/lib/ext</liferay.app.server.lib.global.dir>
<liferay.app.server.portal.dir>${liferay.home}/tomcat-7.0.42/webapps/ROOT</liferay.app.server.portal.dir>
</properties>Replace ${liferay.home} with the real path to your folders.
pom.xml file.mvn clean install
mvn liferay:deploy command and follow the catalina.out logfile. You should see a similar message:[PortletHotDeployListener:343] Registering portlets for first-portlet [PortletHotDeployListener:490] 1 portlet for first-portlet is available for use
After successfully generating sources by the Maven archetype plugin, the sources of our portlet can be imported to our Eclipse IDE. To import them, follow these steps:
${liferay.home}/workspace/first-portlet folder.mvn eclipse:clean eclipse:eclipse command.first-portlet as a project by going to File | Import | General | Existing Projects into Workspace.A portlet project created from com.liferay.maven.archetypes:liferay-portlet-archetype has ready-to-use portlet implementation. In fact, it is very basic, but the entire folder's structure and configuration files are correctly created. Each portlet has four configuration files: portlet.xml, liferay-portlet.xml, liferay-display.xml, and liferay-plugin-package.properties. All of these files are placed in the first-portlet/src/main/webapp/WEB-INF folder.
The portlet.xml file is a portlet descriptor. It contains a portlet definition, such as name, portlet class, and so on.
The liferay-portlet.xml file is a kind of extension of portlet.xml. It is only understood by Liferay Portal. It gives additional information such as portlet's icon, path to the css and js files, and so on.
The liferay-display.xml file tells us in which section our portlet will be available. We will describe it later in the book.
The liferay-plugin-package.properties file is a metric of our portlet. This is a good place to specify version, tags, page URL, author, and license.
Detailed information on portlets is available in the JSR-168 and JSR-286 specification. There are many examples on how to use portlets, how to establish communication between portlets, or what is a portlet request lifecycle.
For more information on portlets, refer to the following recipes:
Change the font size
Change margin width
Change background colour