Kamesh has posted 1 posts at DZone. View Full User Profile

How-To Mavenize Liferay Plugins SDK's Ant build.xml

08.15.2011
| 5672 views |
  • submit to reddit

Overview

When working with Liferay projects using Liferay IDE and Plugins SDK it becomes highly difficult to manage dependencies, as Liferay IDE or Plugins SDK by default uses Ant for building and deploying, though Liferay does support Maven based Plugins but as a seasoned Liferay Developer we will be inclined to use Ant preferred to Maven.

The problem rather I call it a difficulty arises when,

  • The number of dependencies for our Portlet project increases and we need to maintain a version based configuration
  • We need to use or integrate our Portlet application with an existing Maven based application from an Organizational build repository

In this article we will look at how we can use Maven dependency management in Plugins SDK by still using the default Ant based Liferay SDK build.

Technical Platform

I have tested this application on the following technical platform, this works irrespective of Operating System.

Software Version Download Link
Liferay CE Portal Server (Tomcat) 6.0.x http://sourceforge.net/projects/lportal/files/Liferay%20Portal/6.0.6/liferay-portal-tomcat-6.0.6-20110225.zip/download
Liferay CE Plugins SDK 6.0.x http://sourceforge.net/projects/lportal/files/Liferay%20Portal/6.0.6/liferay-plugins-sdk-6.0.6-20110225.zip/download
Apache Ant 1.7.x or above, preferred 1.8.xhttp://ant.apache.org/bindownload.cgi
Apache Maven2.2.1 or above, preferred 3.0.3http://maven.apache.org/download.html
Apache Maven Ant Tasks 2.2.1 or above http://maven.apache.org/ant-tasks/download.html

The Server distribution is not required for the subject of the article it’s preferred to have as the Liferay Plugins SDK might/will use some dependencies from the server distribution

Setup

Installation

Assuming that you have downloaded and installed the above mentioned softwares, for the sake of convenience we will use the following variable names for respective installation DIR’s,

  • $LIFERAY_HOME - the home directory where Liferay server is installed e.g. C:\lportal-6.0.6
  • $LIFERAY_SDK_HOME - the home directory where Liferay server is installed e.g. C:\liferay-plugins-sdk-6.0.6
  • $ANT_HOME - the home directory where ANT is installed e.g. C:\apache-ant-1.8.2
  • $M3_HOME - the home directory where Maven 2.2.x is installed e.g. C:\apache-maven-3.03

Now copy the maven-ant-tasks-2.1.3.jar to $ANT_HOME/lib

Implementation

Let’s use the Liferay IDE to create a new Portlet plug-in look at Using Liferay Maven SDK
Open the build.xml of the project that has been created, by default the project build template looks like

<?xml version="1.0"?>

<project name=" spring-demo-portlet " basedir="." default="deploy">
	<import file="../build-common-portlet.xml" />
</project>

Now to Mavenize the default ANT build.xml we do the following,

  1. Add the xmlns:artifact=” antlib:org.apache.maven.artifact.ant” to the <project> element of the orginal build.xml
  2. Create a new ant target called “copy-dependencies”, this step is crucial, you can have any name for the target but then what the target does is important. This target will download (only time only) and copy the dependencies to docroot/WEB-INF/lib folder
  3. Add an ant call to “deploy” target within the “copy-dependencies”, this will enable the build.xml to follow the default Portlet application building and deploying post copying the dependencies
  4. Make the copy dependencies as the default target for the project

Once you have done the above mentioned steps, your build file will look as follows,

<?xml version="1.0"?>

<project name="spring-demo-portlet" basedir="." xmlns:artifact="antlib:org.apache.maven.artifact.ant" default="copy-dependencies">

	<import file="../build-common-portlet.xml" />

	<property name="app.lib.dir" value="docroot/WEB-INF/lib" />

	<property name="spring.version" value="3.0.5.RELEASE" />

	<property name="jstl.version" value="1.2" />

	<property name="slf4j.version" value="1.5.11" />

	
	<!-- Include the Spring Dependencies -->
	<artifact:dependencies filesetId="app.cutom.deps" usescope="runtime">
		<dependency groupId="org.slf4j" artifactId="slf4j-log4j12" version="${slf4j.version}" scope="provided" />
		<dependency groupId="javax.servlet" artifactId="jstl" version="${jstl.version}" scope="compile" />
		<dependency groupId="org.springframework" artifactId="spring-webmvc-portlet" version="${spring.version}" scope="compile" />
		<dependency groupId="javax.validation" artifactId="validation-api" version="1.0.0.GA" scope="compile" />
		<dependency groupId="org.hibernate" artifactId="hibernate-validator" version="4.0.2.GA" scope="compile" />
		
	</artifact:dependencies>
	
   

	<!-- Just ensure that we copy all application sepecific libraries to the docroot/WEB-INF/lib folder so that its picked up by the Liferay build-->
	<target name="copy-dependencies">
		<copy todir="${app.lib.dir}">
			<fileset refid="app.cutom.deps" />
			<!-- This mapper strips off all leading directory information -->
			<mapper type="flatten" />
		</copy>

		<!-- Call the Liferay deploy target to deploy to the necessary ${auto.deploy.dir} - default build-->
		<antcall target="deploy" />
	</target>

</project>

Observe the artifact:dependencies task portions those are important pieces of mavenizing our ant build. We add the dependencies to the project the same way we add them in Maven pom.xml with little bit of variation to the syntax.
After this once you execute the build in Portlets plug-in directory, it will first download the dependency jars if it does not exist in to the local Maven repository and they will added to the docroot/WEB-INF/lib directory when copy-dependencies target is called, which also includes call to the Liferay default deploy target

Further reading

Conclusion

Voila! Now we are able to use the Maven dependency management with Liferay Plugins SDK default Ant build system. I hope this article has helped you. Please feel free to post your valuable feedback.

0
Published at DZone with permission of its author, Kamesh Sampath.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)