Liferay SDK shared plugins

 

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.
Blogues
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.
nice and helpful . but my query is .
for example i just shared some classes and lib. and used in a hook. so my lib is copied into my hook. so is it copied lib to every plugin in which i used shared plugin ? i mean lib is duplicated or referring one common lib ?