« Back

Liferay SDK shared plugins

Company Blogs March 15, 2013 By Igor Beslic Staff

 

Hi all, I want to share with you something that is pretty cool for me and I found it super useful. It is shared type of Liferay SDK plugin we use to generate jar entry that can be referred by your other plugins as its library entry.

To show how shared plugin is useful lets just mention that liferay-plugin-package.properties already supports some widely used dependency declarations:

  1. dependency to Liferay Portal jars can be declared using
    • portal-dependency-jars - during compile phase it will use portal-dependency.jar but it will be excluded from created archive
    • portal-dependency-tlds
  2. dependency to other plugins auto-generated *-service.jar entry you declare using:
    • required-deployment-contexts - during compile phase it will fetch required-deployment-context-service.jar and include it in created archive

Why shared plugin?

Once in your life, your system design will assume more than one interdependent plugin (and even simple Liferay integration cases can't avoid it). Your team will end up with common code used in each plugin such are various utilities and own APIs. Good starting point for refactoring it is creation of shared type of plugin, extracting common code to it and declaring dependency to it in all other plugins code was used. I'll demonstrate it using always funny batman plugin invented by my coleague Igor:
  • Once you're inside your SDK go to shared folder and create structure for your new plugin:
mkdir batman-shared
cd to batman-shared
  • Create ant build file (you can copy an existing from other plugin):
vim build.xml
adopt it to suite your shared plugin:
<?xml version="1.0"?>
<!DOCTYPE project>
<project name="batman-shared" basedir="." default="deploy">
 <property name="plugin.version" value="1" />
 <import file="../build-common-shared.xml" />
</project>
  • Configure project files

mkdir src

mkdir lib (this is optional if your code doesn't need any additional jar file)

ant setup-eclipse

  • Put your packages and classes into src folder
ant jar
 
In this point you have plain java project and you can continue developing it.
 

How to refer shared plugin from other plugin?

Let say we have batman-portlet plugin. To refer batman-shared we need to update batman-portlet plugin build.xml with new line:
<property name="import.shared" value="batman-shared" />
If you have more than one shared plugin you can refer all of them:
<property name="import.shared" value="batman-shared,robin-shared" />
 
If ant compile was issued from batman-portlet plugin batman-shared.jar will be created and placed into plugins lib folder. Also it will be included in generated archive file.
Note if your shared plugin use own lib to declare additional jars, all those jars will be copied also. So if you care about your final product size be aware of these details.
 
Hope this will help you organise your code better - it helped us.
Threaded Replies Author Date
Hi Igor, In which version of the SDK is this... Pep A April 18, 2013 7:18 AM
Liferay SDK 6.1 GA3 Imad T. November 12, 2013 6:08 AM
This shared plugins work good when working with... Juan Camilo Carrillo June 12, 2014 3:25 PM
Thanks for sharing this valuable information. ... Firoza Saiyed August 19, 2015 10:31 PM

Hi Igor,

In which version of the SDK is this feature available?

I am not able to find this feature within the Liferay Plugins SDK 6.1 GA 2

Regards
Posted on 4/18/13 7:18 AM.
Posted on 11/12/13 6:08 AM in reply to Pep A.
This shared plugins work good when working with Java classes, but when trying to work for JSPs in hooks are not working. The cause seems that jsps are only copied to webapps/ROOT and are not running inside the plugin. For hook java classes works fine.
Posted on 6/12/14 3:25 PM.
Thanks for sharing this valuable information.

Thanks,
Firoza
Posted on 8/19/15 10:31 PM.