Maven-izing a Liferay IDE project

28 Apr 2011

Or more properly, Liferay-ing a Maven project.

I've been working on developing some Liferay portlets as part of researching converting our UI to a portal-based layout. Liferay provides a pretty decent integration between Eclipse projects and their server in the form of the Liferay IDE. The problem is that the Liferay IDE only supports their own project layout in Liferay projects and not the standard Maven layout. But it's much easier to do Spring development in particular and Web development in general with Maven integration (my humble opinion of course!).

Now, before I knew that Liferay didn't support Maven layout in their projects, I had already modified a project created through their new project wizard to work in a Maven layout. So I knew that it wasn't that it couldn't work, just that they didn't have any way to do the migration of existing Maven projects into Liferay projects.

I then undertook to get one of my Maven projects working as a Liferay project. This was quite easy to do just by looking for instances of liferay in the various Eclipse files in an existing Liferay project. Below, I've described what I needed to change to get my Maven project working as a Liferay project, but I'd suggest you do the same thing I did while following along this script: open the same files in an existing Liferay project and just copy the entries into the corresponding files in your Maven project. I've also provided a working sample project.

All the references below are to modifications in the Maven project you're converting.

  1. Delete the existing entry for your project in your Eclipse workspace. Make sure you don't check the delete contents on disk option!
  2. Move or copy your existing project into the portlets folder in your Liferay SDK. I'd suggest copying it to keep a safe working version of the project.
  3. Modify the .classpath file:
    1. Remove the existing JRE_CONTAINER reference and replace it with the Liferay v6.0 CE (Tomcat 6) entry:
      <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/Liferay v6.0 CE (Tomcat 6) JRE">
          <attributes>
              <attribute name="owner.project.facets" value="java"/>
          </attributes>
      </classpathentry>
    2. Add the following additional entries (if you already have something like these, you'll need to make a judgment call on whether you replace the existing ones or modify them to work like this):
      <classpathentry kind="con" path="org.eclipse.jst.server.core.container/com.liferay.ide.eclipse.server.tomcat.runtimeClasspathProvider/Liferay v6.0 CE (Tomcat 6)">
          <attributes>
              <attribute name="owner.project.facets" value="jst.web"/>
          </attributes>
      </classpathentry>
      <classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.module.container"/>
      <classpathentry kind="con" path="com.liferay.ide.eclipse.server.plugin.container/portlet">
          <attributes>
              <attribute name="org.eclipse.jst.component.nondependency" value=""/>
          </attributes>
      </classpathentry>
    3. Save and close the .classpath file.
  4. Modify the .project file:
    1. Make sure that you have the org.eclipse.wst.common.project.facet.core.builder buildCommand specified. Most Maven projects already have this, I think, so you should be set. If not, add it in:
      <buildCommand>
          <name>org.eclipse.wst.common.project.facet.core.builder</name>
          <arguments>
          </arguments>
      </buildCommand>
    2. You should also have the org.eclipse.wst.common.project.facet.core.nature nature element specified already as well. Again, if not, add it in:
      <nature>org.eclipse.wst.common.project.facet.core.nature</nature>
    3. Save and close the .project file.
  5. Modify or create the .settings/org.eclipse.wst.common.project.facet.core.prefs.xml file. I had to create it, but you may already have it. The full file that I have looks like this:
    <root>
        <facet id="liferay.portlet">
            <node name="libprov">
                <attribute name="provider-id" value="com.liferay.ide.eclipse.plugin.portlet.libraryProvider"/>
            </node>
            <node name="liferay-plugin-project">
                <attribute name="sdk-name" value="liferay-plugins-sdk-6.0.5"/>
            </node>
        </facet>
    </root>
  6. Modify the .settings/org.eclipse.wst.common.project.facet.core.xml file. I believe you'll always have this file, but if not you can create it based on what I have here. The contents of this file are:
    <?xml version="1.0" encoding="UTF-8"?>
    <faceted-project>
        <runtime name="Liferay v6.0 CE (Tomcat 6)"/>
        <fixed facet="jst.web"/>
        <fixed facet="java"/>
        <fixed facet="wst.jsdt.web"/>
        <fixed facet="liferay.portlet"/>
        <installed facet="java" version="1.6"/>
        <installed facet="jst.web" version="2.4"/>
        <installed facet="liferay.portlet" version="6.0"/>
        <installed facet="wst.jsdt.web" version="1.0"/>
    </faceted-project>
  7. The last thing to do is to get your modified project into your workspace. Just go ahead and import the modified project as an existing project in your Eclipse workspace.

From this point, your project should either be built automatically, if you have that set, or should be buildable through the standard build mechanisms. You should also be able to do Run As->Maven package and get a deployable war file. But of course the real test is running on Liferay within the Eclipse IDE. Give that a try by selecting Run As->Run on server or Debug As->Debug on Server. That should get you going in the server as with projects created directly with Liferay IDE.

Please let me know if you find any errors in this post or differences in various deployment configurations. For reference, I'm using SpringSource Tool Suite 2.6.0.SR1, which basically means Eclipse Helios, with JDK 1.6 on 64-bit Windows 7 and 64-bit CentOS 5.5.