Liferay allows you to create different types of plugins (portlets, hook, theme, ...) and each of these has its own specific folder inside the SDK.
But there is a kind of plugin, that does not produce a WAR file to deploy, but it is extremely useful because it allows you to create distributable and reusable libraries: it's the shared plugin, and now we'll see how to create and use it.
It should be specified that, at the time of writing this post, Liferay IDE (the Eclipse plugin which support development) does not provide any kind of support to the creation of shared plugin, so we must do everything by hand.
First run Eclipse and create a new simple Java Project:
- project name suffix must be
-shared
, i.e.my-libray-shared
; - the folder in which to create the project must be the Liferay SDK
shared
folder; - let's add also the
src
andlib
folder; - I suggest to call the folder for compiled file as
classes
instead ofbin
.
Once the project has been created, create the build.xml
Ant file inside with the following content:
<?xml version="1.0"?> <!DOCTYPE project> <project name="my-library-shared" basedir="." default="deploy"> <property name="plugin.version" value="1" /> <import file="../build-common-shared.xml" /> </project>
Then open the build.xml
file inside the Eclipse Ant view and run the setup-eclipse
Ant target which will configure all Java project dependencies; some of these dependencies (portal-service.jar
, util-bridges.jar
, util-java.jar
, util-taglib.jar
) are related to your specific application server and will not be resolved but if you're using Tomcat you will find theme inside the webapps/ROOT/WEB-INF/lib
folder.
Shared plugin configuration is ended, now you can write your own utility classes.
Ok, but how can we use it? Simply add, inside the build.xml
file of each custom plugin that needs the shared dependency (typically portlet plugins), the following property that contains the list (comma separated) of all the required shared plugins:
<property name="import.shared" value="my-libray-shared" />
At this point, every time you launch the compile
Ant target of the custom plugin, the SDK will automatically create the shared plugin JAR file and will include it statically in the plugin dependencies. Pay attention that I'm not talking about the Eclipse auto compilation feature but only the compile
Ant target of the SDK.
Alternatively, you can just use the compile-import-shared
target that will simply create the shared plugin JAR, without recompiling the entire custom plugin that uses it.