Skip to content

How to Create Portlet Application Using OSGi in Liferay 7?

Featured Image

Being a programmer, you must have faced a situation where you had to start over from scratch every time you start any new project. You have to write not only the code for project build but also the underlying codes that support the project. This makes it a tiring task. So to eliminate this redundancy, there is a platform that offers a baseline set of features that would help in giving a head start on the entire repetitive code which is termed as Liferay Portal. Liferay Portal offers the developers a platform for building web apps, mobile apps and web services quickly using the features and frameworks designed for the rapid development and ease of use. It also offers some of the best tools to make the programming easier among which Blade CLI is one of the major tools.

Let us have a deeper understanding on what is Blade CLI and how it can be used with any IDE or development environment.

What is Blade CLI?

Blade CLI is a command line tool that is bootstrapped on to a Gradle based environment used for building Liferay 7.0 modules. It is one of the easiest ways for Liferay developers to create new Liferay portal solutions and modules. You can simply download blade tools JAR and once you’ve obtained the Blade Tools JAR, you can install Blade Tools through the Java Package Manager (JPM).

Using JAVA Package Manager (JPM) To Download Blade Tools:

1) Install JAVA Package Manager(JPM)

2) Once JPM is installed, we need to download com.liferay.blade.cli.jar

3) Next, we can install Blade tools from downloaded JAR with the use of below command.

(sudo) jpm install -fl [Downloads Directory]/com.liferay.blade.cli.jar

4) Alternate command to install blade tools.

jpm install -f https://liferay-test-01.ci.cloudbees.com/job/blade.tools/lastSuccessfulBuild/artifact/com.liferay.blade.cli/generated/com.liferay.blade.cli.jar

Verifying Blade CLI installation:

1) Verify your installation by writing “blade” command in command prompt. We can see below list of MAIN OPTIONS and available sub-commands for the installed blade tools.

MAIN OPTIONS:
[ -b, –base <string> ]  – Specify a new base directory(default working directory). [ -e, –exceptions ]   –  Print exception stack traces when they occur. [ -f, –failok <string;> ] –  Do not return error status for error that matches this given regular expression. [ -k, –key ]  –  Wait for a key press, might be useful when you want to see the result before it is overwritten by a next command [ -p, –pedantic ] – Be pedantic about all details. [ -t, –trace ] – Trace on. [ -w, –width <int> ]  -The output width, used for wrapping diagnostic output Available sub-commands: agent  – Install or uninstall remote agent in Liferay create   –  Creates a new Liferay module project from several available templates. deploy – Deploys a bundle to the Liferay module framework. gw  – Execute gradle command using the gradle wrapper if detected help – Get help on a specific command init – Initializes a new Liferay workspace migrateTheme  – Migrate a plugins sdk theme to new workspace theme project open  – Opens or imports a file or project in Liferay IDE. samples – Generate a sample project server – Start or stop server defined by your Liferay project sh – Connects to Liferay and executes gogo command and returns output. update – Update blade to latest version version   – Show version information about blade

2) We have installed Blade tools, now let’s understand its 2 important command: blade create and blade deploy

3) To understand how to use blade create, enter blade create command into terminal. Below is output that terminal produces.

NAME create   –   Creates a new Liferay module project from several available templates. SYNOPSIS create [options] <name> OPTIONS [ -c, –classname <string> ] – If a class is generated in the project, provide the name of the class to be generated. If not provided defaults to Project name. [ -d, –dir <file> ] – The directory where to create the new project. [ -h, –hostbundlebsn <string> ] – If a new jsp hook fragment needs to be created, provide the name of the host bundle symbolic name. [ -H, –hostbundleversion <string> ] – If a new jsp hook fragment needs to be created, provide the name of the host bundle version. [ -p, –packagename <string> ] – Package name [ -s, –service <string> ] – If a new DS component needs to be created, provide the name of the service to be implemented. [ -t, –template <template> ]  – The project template to use when creating the project. The following templates are available: activator, jsphook, mvcportlet, portlet, service, servicebuilder, servicewrapper

4) We need to make sure that our module is built before we execute blade deploy command as this command will be used to deploy application in Liferay.

Creating first Liferay Application with Blade Tools:

1) Let’s create liferay portlet application with the use of blade tools and MVC architecture. We will create MVC portlet here. In order to create MVC portlet, we have to run below command.

blade create -t mvcportlet -p com.azilen.app.mvcportlet -c SampleMvcPortlet sample-mvc-portlet-project

2) Above command will create MVC portlet, and create folder structure as shown below.

sample-mvc-portlet-project src main java com/azilen/app/mvcportlet SampleMvcPortlet.java resources content Language.properties META-INF resources init.jsp view.jsp bnd.bnd build.gradle

3) We are now ready to build our portlet. Run gradle build command to build your portlet.

4) If you get build successful message in your above step then we are ready to deploy our application on liferay. Before deploying our application in liferay we need to install remote agent with the use of blade agent install –l <Liferay home path> command. Below message will be displayed on screen if agent has successfully installed.

Installed remote agent successfully. Please wait 5-10 seconds for remote agent to be autodeployed, then use the “blade sh <gogo-command>”. At any time, uninstall the remote agent by using the “blade agent uninstall” command.

5) Blade needs gradle wrapper files in your module.

task wrapper(type: Wrapper) { gradleVersion = ‘2.0’ //version required }

To install gradle wrapper, we need to add above lines in our build.gradle file and then we can run gradle wrapper command from command prompt.

6) Once wrapper is installed, we can run blade deploy command from command prompt to deploy our application.

7) It will copy jar file into our <module directory>/build/libs and deploy your application in liferay

8) Login to your liferay instance and you can add your new application from applications.

This is how a portlet application can be created using OSGI modular framework. As Liferay was written by developers for the developers to get the work done faster and easily, the Blade CLI tools does the same while making programming easy and convenient.

Related Insights