Developing modules for Liferay Portal requires:

  • Creating a folder structure: A good folder structure facilitates evolving and maintaining code, especially in collaboration. Popular tools use pre-defined folder structures familiar to developers.

  • Writing code and configuration files: A manifest, Java classes, and resources. Modules stubbed out with them let developers focus on implementing logic.

  • Compilation: Acquiring dependencies and building the module. Common build tools that manage dependencies include Gradle, Maven, and Ant/Ivy.

  • Deployment: Interacting with the runtime environment to install, monitor, and modify modules.

There are several good build tools for developing modules on Liferay Portal. This tutorial demonstrates starting a new module using Liferay Workspace. It’s Liferay’s opinionated build environment based on Gradle and BndTools that simplifies module development and automates much of it.

Here are the steps for starting module development:

  1. Set up a Liferay Workspace

  2. Create a module

  3. Build and deploy the module

On completing this tutorial you’ll have created a module and deployed it to a local Liferay Portal bundle.

Setting up a Liferay Workspace

Creating and configuring a Liferay Workspace (Workspace) is straightforward using a tool called Blade CLI (Blade). Blade is a command line tool that creates Workspaces and performs common tasks.

Follow the steps in this tutorial to install Blade if you don’t already have it.

The blade executable is now in the system path.

You can create a Workspace in the current directory by executing this command:

blade init <workspaceName>

You’ve created a Workspace! Its directory structure looks like the one shown in the figure below.

starting-module-dev-workspace-structure.png

Figure 1: Liferay Workspace aggregates projects so they can leverage the Gradle build environment.

Workspace can be configured to use a Liferay Portal installation bundle anywhere on the local file system. The liferay.workspace.home.dir property in gradle.properties sets the default bundle location to a folder <workspace>/bundles (not yet created). For convenience it’s suggested to install a Liferay Portal bundle there. If you install it to a different location, uncomment the liferay.workspace.home.dir property and set it to that location.

The Workspace is ready for creating modules.

Creating a Module

Blade provides module templates and module samples. The templates stub out files for different types of modules. The samples can be generated in a Workspace and demonstrate many module types. Developers can use templates and samples to develop modules.

Using Module Templates

The Blade command blade create -l lists the module templates.

starting-module-dev-blade-templates.png

Figure 2: Blade’s create command generates a module based on a template. Executing create -l lists the template names.

Here’s the command syntax for creating a module:

blade create [options] moduleName

The blade help commandName command describes the specified command. To describe the create command, for example, execute this:

blade help create

Here’s an example of creating a Liferay MVC Portlet module:

blade create -t mvc-portlet -p com.liferay.docs.mymodule -c MyMvcPortlet my-module

Module projects are created in the modules folder by default.

Here’s the module project anatomy:

  • src/main/java/ → Java package root

  • src/main/resources/content/ (optional) → Language resource bundle root

  • src/main/resources/META-INF/resources/ (optional) → Root for UI templates, such as JSPs

  • bnd.bnd → Specifies essential OSGi module manifest headers

  • build.gradleConfigures dependencies and more using Gradle

The figure below shows an MVC portlet module project.

starting-module-dev-module-structure.png

Figure 3: Liferay modules use the standard Maven directory structure.

Sample modules are another helpful development resource.

Using Module Samples

An alternative to creating a module from a template is to generate a sample module. Developers can examine or modify sample modules as desired.

This command lists the sample names:

blade samples

The figure below shows the listing.

starting-module-dev-blade-samples.png

Figure 4: The blade samples command lists the names of sample modules developers can create, examine, and modify to meet their needs.

Here’s the Blade samples command syntax:

blade samples <sampleName>

It creates the sample project in a subfolder of the current folder.

Building a module and deploying it to Liferay Portal is easy.

Building and Deploying a Module

Liferay Workspace provides Gradle tasks for building and deploying modules. Blade’s blade gw command solves a common need in Gradle projects: invoking the Gradle wrapper from any project directory. You can use blade gw just as you would invoke gradlew, without having to specify the wrapper path.

In a module folder, execute this command to list the Gradle tasks available:

blade gw tasks

Workspace uses BndTools to generate the module’s OSGi MANIFEST.MF file and package it in the module JAR. To compile the module and generate the module JAR, execute the jar Gradle task:

blade gw jar

The generated JAR is in the module project’s build/libs folder and ready for deployment to Liferay Portal.

Start your Liferay Portal server, if you haven’t already started it.

Blade can deploy modules to any local Liferay Portal server. It communicates with Liferay Portal’s OSGi framework using Felix Gogo shell and deploys modules directly to the OSGi container using Felix File Install commands. The command uses the default port 11311.

To deploy the module, execute this command:

blade deploy

Also Blade lets developers deploy all modules in the current folder tree. To deploy all modules in a Workspace’s modules folder, for example, execute blade deploy in the <workspace>/modules folder.

If you deployed a portlet module, it’s available in the Liferay Portal UI under the application category and name you specified via the portlet component’s com.liferay.portlet.display-category and javax.portlet.display-name properties in the @Component annotation.

starting-module-dev-portlet.png

Figure 6: Here’s a bare-bones portlet based on one of Liferay’s module templates.

Redeploying Module Changes Automatically

Blade lets developers set a watch on changes to a module project’s output files. If they’re modified, Blade redeploys the module automatically. To set a watch on a module at deployment, execute this command in the module project:

blade deploy -w

Here’s output from deploying (and watching) a module named com.liferay.docs.mymodule:

E:\workspaces\my-liferay-workspace\modules\my-module-project>blade deploy -w

:modules:my-module-project:compileJava UP-TO-DATE
:modules:my-module-project:buildCSS UP-TO-DATE
:modules:my-module-project:processResources UP-TO-DATE
:modules:my-module-project:transpileJS SKIPPED
:modules:my-module-project:configJSModules SKIPPED
:modules:my-module-project:classes UP-TO-DATE
:modules:my-module-project:jar UP-TO-DATE
:modules:my-module-project:assemble UP-TO-DATE
:modules:my-module-project:build

BUILD SUCCESSFUL

Total time: 2.962 secs
install file:/E:/workspaces/my-liferay-workspace/modules/my-module-project/build/libs/com.liferay.docs.mymodule-1.0.0.jar
Bundle ID: 505
start 505

Scanning E:\workspaces\my-liferay-workspace\modules\my-module-project

...

Waiting for changes to input files of tasks... (ctrl-d then enter to exit)

Output from the blade deploy -w command indicates that the module is installed and started, reports the module’s OSGi bundle ID, and stands ready to redeploy the module if its output files change.

Congratulations on a great start to developing your module!

Related Articles

Configuring Dependencies

Liferay Workspace

Tooling

OSGi Basics for Liferay Development

Portlets

0 (0 Votes)
Metal.js Previous