Liferay 7-OSGI Module – MVC Portlet by Maven

osgi-module-by-maven-liferay7


aimUsing Maven we can easily create OSGI module in Liferay 7. Liferay has already published many Maven Archetypes for creating OSGI based application. In this article we will show how to create a basic MVC Portlet using Maven as build tool. The archetype is capable of generating necessary source code compliance with OSGI specification. The portlet will be packaged as OSGI module which is nothing but a JAR file at the end.


NoteWhats Maven Archetype???

For different types of projects there are different types of directory structures. For Spring based web application the directory structure may be different from normal Servlet based web application. Similarly for Struts  application the directory structure may be something else. For each framework or technology there are specific folder structure.

 A Maven archetype is simply a plugin which create a project structure. For example there is maven archetype for Spring MVC which create project structure for Spring based application, similarly there are Archetypes for liferay, hibernate, wicket, struts and son. Each Archetype create different project structure. A Maven Archetype generate project skeleton which saves our development time.


 Creating the Portlet:

Open your command prompt and execute the below command in a single line.All the parameters starting with -D must have space.  Before executing the below you must have to install Maven in your machine.

mvn archetype:generate
 -DarchetypeGroupId=com.liferay 
 -DarchetypeArtifactId=com.liferay.project.templates.mvc.portlet
 -DgroupId=com.proliferay
 -DartifactId=com.proliferay.sample.maven.portlet 
 -DinteractiveMode=false 
 -DclassName=Sample

Note: 

In maven everything is plugin. So Archetype is also a plugin. Each plugin is identified by Group Id, Artifact Id and version. In the above command we have used an Archetype Plugin. Its Group Id is com.liferay and artifactId is com.liferay.project.templates.mvc.portlet

The above command will create project structure as well as sample code. The generated directory structure looks like this

directory-structure

 

After creating the project you can import it as maven project in the eclipse.

Some important generated Source Files:-

pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.proliferay</groupId>
	<artifactId>com.proliferay.sample.maven.portlet</artifactId>
	<version>1.0-SNAPSHOT</version>
	<packaging>jar</packaging>
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>
	<dependencies>
		<dependency>
			<groupId>com.liferay.portal</groupId>
			<artifactId>com.liferay.portal.kernel</artifactId>
			<version>2.0.0</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>com.liferay.portal</groupId>
			<artifactId>com.liferay.util.taglib</artifactId>
			<version>2.0.0</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>javax.portlet</groupId>
			<artifactId>portlet-api</artifactId>
			<version>2.0</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>3.0.1</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>jstl</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.osgi</groupId>
			<artifactId>org.osgi.compendium</artifactId>
			<version>5.0.0</version>
			<scope>provided</scope>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.1</version>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-jar-plugin</artifactId>
				<version>2.6</version>
				<configuration>
					<archive>
						<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
					</archive>
					<excludes>
						<exclude>**/META-INF/resources/**/.sass-cache/</exclude>
					</excludes>
				</configuration>
			</plugin>
			<plugin>
				<groupId>biz.aQute.bnd</groupId>
				<artifactId>bnd-maven-plugin</artifactId>
				<version>3.2.0</version>
				<executions>
					<execution>
						<id>default-bnd-process</id>
						<goals>
							<goal>bnd-process</goal>
						</goals>
					</execution>
				</executions>
				<dependencies>
					<dependency>
						<groupId>biz.aQute.bnd</groupId>
						<artifactId>biz.aQute.bndlib</artifactId>
						<version>3.2.0</version>
					</dependency>
					<dependency>
						<groupId>com.liferay</groupId>
						<artifactId>com.liferay.ant.bnd</artifactId>
						<version>2.0.28</version>
					</dependency>
				</dependencies>
			</plugin>
			<plugin>
				<groupId>com.liferay</groupId>
				<artifactId>com.liferay.css.builder</artifactId>
				<version>1.0.20</version>
				<executions>
					<execution>
						<id>default-build-css</id>
						<phase>generate-sources</phase>
						<goals>
							<goal>build-css</goal>
						</goals>
					</execution>
				</executions>
				<configuration>
					<portalCommonPath>/</portalCommonPath>
					<docrootDirName>src/main/resources</docrootDirName>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

SamplePortlet.java

package com.proliferay.sample.maven.portlet;

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

import javax.portlet.Portlet;

import org.osgi.service.component.annotations.Component;

@Component(
	immediate = true,
	property = {
		"com.liferay.portlet.display-category=Proliferay",
		"com.liferay.portlet.instanceable=true",
		"javax.portlet.display-name=Sample Maven Portlet",
		"javax.portlet.init-param.template-path=/",
		"javax.portlet.init-param.view-template=/view.jsp",
		"javax.portlet.resource-bundle=content.Language",
		"javax.portlet.security-role-ref=power-user,user"
	},
	service = Portlet.class
)
public class SamplePortlet extends MVCPortlet {
}

bnd.bnd

Bundle-Name: com.proliferay.sample.maven.portlet
Bundle-SymbolicName: com.proliferay.sample.maven.portlet
Bundle-Version: 1.0-SNAPSHOT
-jsp: *.jsp,*.jspf
-plugin.jsp: com.liferay.ant.bnd.jsp.JspAnalyzerPlugin
-plugin.resourcebundle: com.liferay.ant.bnd.resource.bundle.ResourceBundleLoaderAnalyzerPlugin
-plugin.sass: com.liferay.ant.bnd.sass.SassAnalyzerPlugin
-sass: *

build.gradle

buildscript {
	dependencies {
		classpath group: "com.liferay", name: "com.liferay.gradle.plugins", version: "3.0.23"
	}

	repositories {
		mavenLocal()

		maven {
			url "https://cdn.lfrs.sl/repository.liferay.com/nexus/content/groups/public"
		}
	}
}

apply plugin: "com.liferay.plugin"

dependencies {
	compileOnly group: "com.liferay.portal", name: "com.liferay.portal.kernel", version: "2.0.0"
	compileOnly group: "com.liferay.portal", name: "com.liferay.util.taglib", version: "2.0.0"
	compileOnly group: "javax.portlet", name: "portlet-api", version: "2.0"
	compileOnly group: "javax.servlet", name: "javax.servlet-api", version: "3.0.1"
	compileOnly group: "jstl", name: "jstl", version: "1.2"
	compileOnly group: "org.osgi", name: "org.osgi.compendium", version: "5.0.0"
}

repositories {
	mavenLocal()

	maven {
		url "https://cdn.lfrs.sl/repository.liferay.com/nexus/content/groups/public"
	}
}

Build and Deploy the portlet:

Go to the folder com.proliferay.sample.maven.portlet and execute mvn clean package. After successful build it will generate com.proliferay.sample.maven.portlet-1.0-SNAPSHOT.jar in the target folder. Just copy this JAR file into Liferay deploy folder. That’s all and once its deployed the portlet will be ready to use.

osgi-module-jar

Download Source Code:

com-proliferay-sample-maven-portlet

About The Author

1 thought on “Liferay 7-OSGI Module – MVC Portlet by Maven”

  1. Pingback: Configurable Portlet In Liferay 7 / Liferay DXP - Pro Liferay

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top