Introduction#

Latest efforts relating Maven and Liferay have been focused in providing official maven artifacts for Liferay as well as porting our plugins sdk to Maven.

Our goal is to provide our CE releases through our own public repository as well as provide means for our EE customers to install the EE versions artifacts to their local maven repository.

If you have ever worked with enterprise projects using maven you already know how important a local maven repository and proxy is. For those not so familiar with Maven a proxy is a server that proxies your requests to public Maven repositories and caches the artifacts locally for faster and more reliable access. Most maven proxies can also host private repositories used for hosting your company's private artifacts. Having a local proxy / repository makes your maven builds much faster and more reliable than accessing remote repositories that might even sometimes be unavailable.

Installing a maven proxy / repository#

First step is to install and setup Nexus. Nexus is a open source maven repository manager that can proxy to other repositories as well as host repositories. If you just want to try things locally you can skip this step.

  1. Download latest Nexus such as nexus-webapp-1.4.0-bundle.zip
  2. Follow the installation directions of the Nexus book http://nexus.sonatype.org/documentation.html
  3. Startup nexus
  4. Open your browser to your newly created nexus (if you installed it locally it could be accessed by opening http://localhost:8080/nexus)
  5. Login as administrator (default login is admin / admin123)
  6. Go to Repositories and click Add -> Hosted Repository
  7. Give the repository following information and click save
Repository ID: liferay-ce-releases
Repostory Name: Liferay CE Release Repository
Provider: Maven2 Repository
Repository Policy: Release
  1. Create another hosted repository with following information
Repository ID: liferay-ce-snapshots
Repository Name: Liferay CE Snapshot Repository
Provider: Maven2 Repository
Repository Policy: Snapshot

Now you have a repository ready for Liferay's Maven artifacts. Next step is to configure your maven to be able to upload artifacts to that repository.

Configuring Maven Settings#

Open your $HOME/.m2/settings.xml (if the file does not exist create it). Add the servers segment to your settings.xml

<?xml version="1.0" encoding="UTF-8"?>
<settings>
     <servers>
          <server>
               <id>liferay</id>
               <username>admin</username>
               <password>admin123</password>
          </server>
     </servers>
</settings>

You might also want to make your Nexus as your maven proxy. To do that just add following xml segment to your settings.xml right before servers element.

<mirrors>
     <mirror>
          <id>local</id>
          <name>Local mirror repository</name>
          <url>http://localhost:8080/nexus/content/groups/public</url>
          <mirrorOf>*</mirrorOf>
     </mirror>
</mirrors>

Installing Liferay Artifacts to Repository#

Next we will install the Liferay Maven artifacts to your repository. First you need to checkout Liferay code from the SVN.

svn --username guest co svn://svn.liferay.com/repos/public/portal/trunk portal-trunk

Guest user does not require password.

Then create a release.${username}.properties file and add

maven.url=http://localhost:8080/nexus/content/repositories/liferay-ce-snapshots

Build Liferay artifacts by running

ant clean start jar

Now you can deploy the Liferay artifacts to your maven repository by running

ant -f build-maven.xml deploy-artifacts

If you only want to have them locally without a maven repository you can run the install task instead of deploy

ant -f build-maven.xml install-artifacts

Now you can add Liferay dependencies to your maven project. Following artifacts are available:

<dependency>
	<groupId>com.liferay.portal</groupId>
	<artifactId>portal-client</artifactId>
	<version>6.0-SNAPSHOT</version>
</dependency>
<dependency>
	<groupId>com.liferay.portal</groupId>
	<artifactId>portal-impl</artifactId>
	<version>6.0-SNAPSHOT</version>
	<scope>provided</scope>
</dependency>
<dependency>
	<groupId>com.liferay.portal</groupId>
	<artifactId>portal-kernel</artifactId>
	<version>6.0-SNAPSHOT</version>
	<scope>provided</scope>
</dependency>
<dependency>
	<groupId>com.liferay.portal</groupId>
	<artifactId>portal-service</artifactId>
	<version>6.0-SNAPSHOT</version>
	<scope>provided</scope>
</dependency>
<dependency>
	<groupId>com.liferay.portal</groupId>
	<artifactId>portal-web</artifactId>
	<version>6.0-SNAPSHOT</version>
	<scope>provided</scope>
</dependency>
<dependency>
	<groupId>com.liferay.portal</groupId>
	<artifactId>util-bridges</artifactId>
	<version>6.0-SNAPSHOT</version>
</dependency>
<dependency>
	<groupId>com.liferay.portal</groupId>
	<artifactId>util-java</artifactId>
	<version>6.0-SNAPSHOT</version>
</dependency>
<dependency>
	<groupId>com.liferay.portal</groupId>
	<artifactId>util-taglib</artifactId>
	<version>6.0-SNAPSHOT</version>
</dependency>

NOTE portal-impl and portal-web are provided for maven plugins and should never be added as dependency to your Liferay plugins.

Installing the Liferay Maven SDK#

To take full advantage of Maven we are porting the functionality of out ant based Plugins SDK to Maven. To use it you need to install it locally. To install the Liferay maven plugins and archetypes go into support-maven folder and run

mvn install

Now the Liferay Maven SDK is installed and ready to use. We've implemented a portlet archetype and deployer plugin.

Creating a Portlet Plugin#

Move to the folder where you want to create your portlet and run

mvn archetype:generate

From the list select liferay-portlet-archetype and provide your project groupId, artifactId and version for the portlet project.

You're portlet project's pom.xml has two properties liferay.auto.deploy.dir and liferay.version. These properties are usually moved to your parent pom.xml or settings.xml so that you don't have to adjust them for every single plugin you create. Set the liferay.auto.deploy.dir to point to the Liferay autodeploy directory of your Liferay bundle. This is where the deploy plugin will copy your portlet. Now you are ready deploy your newly created portlet. You can deploy it by running

mvn liferay:deploy

Future Plans#

We are also in the process of adding archetypes for themes, hooks and layouts as well as providing portlet archetypes for different types of portlets like JSP, Spring MVC, JSF etc.

If you are using 5.2.3 CE and want to take advantage of Maven for building Liferay portlets Milen Dyankov a Liferay community member has done also great work on a Maven SDK for 5.2.3 CE. You can find more about it from GitHub

(Extracted from Mika Koivisto's blog)

0 Attachments
19359 Views
Average (0 Votes)
Comments

Showing 20 Comments

jonathan doklovic
6/17/10 1:22 PM

Service builder doesn't work in the latest maven sdk.
It generates code fine, but it does not create the -services.jar or any of the sql scripts that should be packaged with the war.

Baptiste Grenier
6/23/10 4:10 AM

I was not able to use this notes with liferay portal 6.0.2 branche, more details in this forum thread: http://www.liferay.com/community/forums/-/message_boards/message/5147190

Sverker Abrahamsson
8/6/10 7:11 AM

Maven archetype for themes
I use maven to build themes, first I have a maven project which creates the base artifacts for _styled, _unstyled and classic themes. Then I create my theme project from an archetype which extend the desired theme.

Under \src\main\webapp\ in my project is the same as _diff in the traditional structure, I find this much easier to work with.

The files can be downloaded from here:
http://www.abrahamsson.com/theme-base.zip
http://www.abrahamsson.com/theme-a­rchetype-1.0.jar

Run first maven in the base project, adjust the path to liferay installation. Then generate the new theme project with the following command line:

mvn archetype:generate -DarchetypeGroupId=com.liferay -DarchetypeArtifactId=theme-archetype -DarchetypeVersion=1.0

Peter Mesotten
10/8/10 12:26 AM

Any update on the Spring/JSF archetypes?

Jakub Liska
10/20/10 4:08 PM

Hey Jonathan,

have you figured out why it wasn't generating the jar and sql scripts ? Cause I'm using current trunk now, and it still doesn't generate them.

Jakub Liska
10/20/10 4:52 PM

the maven-archetype doesn't create WEB-INF/sql directory and without it servicebuidler.java doesn't create them....

as to the libraries, in pom definition, portlet-service.jar is marked as provided and util-*.jar are not...in fact it should be the other way around, because during deployment util-*.jars are copied over, but portlet-service.jar should be shipped with portlet...

Jakub Liska
10/20/10 4:59 PM

That's not what I wanted to say :-) the "provided" thing, the problem is that the libraries are created with version stamp, and when they are deployed, hot deployer doesn't see them and copy over new util-*.jars to the portlet lib directory and they are doubled there

Brian Ko
11/1/10 6:14 PM

Hi there,
It works great. It created a basic structure for the liferay portlet projects. Now the issue is how we can use the service builder to create the java code which conforms to the maven folder structure so that we can build the war file from maven. Do you have a plan to provide the service builder for maven? Thank you.
Brian

Brian Ko
11/1/10 6:21 PM

Please ignore my previous email. I did not see the liferay-maven-plugin in pom.xml.

Brian Ko
11/3/10 8:16 AM

Jakub,

Were you able to run the service builder (mvn -P build-service install) on the project you created? I got build error as it cannot locate the liferay-lib-5.2.3-r2.pom.

Brian

Shinobi vn
1/13/11 11:36 PM

With Liferay CE 6.0.5, Maven service builder doesn't create the folder WEB-INF/sql, so you may have issue when deploying the portlet. Create the folder manually and call service builder again (mvn liferay:build-service) will fix this problem.

Petr Vlček
3/25/11 2:21 AM

Since instructions on this page are not complete (at least for version 6.0.6), I would like to summarize missing points found in related forum posts that helped me to get it working:

1. Install Maven, Ant and Nexus following instructions on this page.
2. Install GPG (http://www.gnupg.org)
3. Generate GPG key with gpg --gen-key
4. Create your release.username.properties with following contents:

lf.version=6.0.6
maven.url=http://path_to_your_repository
maven.repositor­y.id=liferay # as in settings.xml
maven.version=${lf.version}
gpg.keymame=XXXXX # to get keyname run command gpg --list-keys
gpg.passphrase=XXXXX

5. Build Liferay - ant clean start jar
6. Build javadoc packages - ant -f build-maven.xml jar-javadoc
7. Build source packages - ant -f build-maven.xml jar-sources
8. Deploy all to Nexus - ant -f build-maven.xml deploy-artifacts

Tom Mahy
6/2/11 1:40 AM

Thank you Petr.
Exactly what i was looking for.

This should be included in the doc.
btw: keymame --> keyname :-)

Lorin Sutton
6/2/11 12:42 PM

Hey all,

Hours of Googling on this has not been fruitful

So I'm trying the Maven SDK for the first time and just did a simple test:

mvn archetype:generate
selected : 10: remote -> liferay-portlet-archetype (Provides an archetype to create Liferay portlets.)

and did the remaining requested info. The project was created without error.

When I do a "mvn -P build-service compile" though, I continually get:

Profile with id: 'build-service' has not been activated.

mixed into the otherwise successful build process.

I was monkeying with my settings.xml file as was outlined in comments above but after a few failed attempts I realized it was more "monkeying" and less "knowing" so I stopped.

Can someone clarify what should be adjusted where so that the build-services profile will succeed?

Thanks much,
-Lorin

Jerry Thome
7/6/11 12:00 PM

Lorin, did you try...?

mvn liferay:build-service

Rollin Crittendon
12/2/11 6:51 AM

I followed the instructions Petr Vlček added, with the addition from Tom Mahy.

I am using 6.0.6. When I try to deploy I get the following after running "ant -f build-maven.xml deploy-artifacts".

Is there another Nexus configuration step that is needed?
=========================
[exec] org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-gpg-plugin:1.4:sign-and-deploy-file (default-cli) on project standalone-pom: Failed to deploy artifacts: Could not transfer artifact com.liferay.portal:portal-client:jar:6.0.6 from/to liferay (http://localhost:8081/nexus/content/repositories/liferay-ce-releases): Failed to transfer file: http://localhost:8081/nexus/content/repositories/liferay-ce-releases/com/liferay­/portal/portal-client/6.0.6/portal-client-6.0.6.jar. Return code is: 401
[exec] at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:217)
[exec] at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
=­========================

Rollin Crittendon
12/2/11 7:20 AM

I was also wondering for gpg keys do we use just the name, say "test" or the UID as the gpg.keyname, as is "test <allen@bcc.com>"?

Rollin Crittendon
12/2/11 8:11 AM

I found my maven settings file had a format issue.

As for the gpg.keyname, it is a hexadecimal value.

Gerald Perellano
12/12/11 5:42 AM

Hello Everyone

I'm testing for the first time the Liferay Maven SDK. And now I have a problem running the service-builder.

With mvn -P build-service package I've got build-service not found error and if
I use mvn liferay:build-service this error comes out:

-------------------
[ERROR] Failed to execute goal com.liferay.maven.plugins:liferay-maven-plugin:6.0.5:build-service (default-cli) on project inseratsearchpoc: Execution default-cli of goal com.liferay.maven.plugins:liferay-maven-plugin:6.0.5:build-service failed: An API incompatibility was encountered while executing com.liferay.maven.plugins:liferay-maven-plugin:6.0.5:build-service: java.lang.ExceptionInInitializerError: null
[ERROR] -----------------------------------------------------
[ERROR] realm = plugin>com.liferay.maven.plugins:liferay-maven-plugin:6.0.5
[ERROR] strategy = org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy
[ERROR] urls[0] = file:/C:/Users/gpe/.m2/repository/com/liferay/maven/plugins/liferay-maven-plugin­/6.0.5/liferaymaven-plugin-6.0.5.jar
[ERROR] urls[1] = file:/C:/Users/gpe/.m2/repository/com/liferay/portal/portal-impl/6.0.5/portal-im­pl-6.0.5.jar
[ERROR] urls[2] = file:/C:/Users/gpe/.m2/repository/com/liferay/portal/portal-service/6.0.5/portal­-service-6.0.5.jar
[ERROR] urls[3] = file:/C:/Users/gpe/.m2/repository/com/liferay/portal/util-java/6.0.5/util-java-6­.0.5.jar
[ERROR] urls[4] = file:/C:/Users/gpe/.m2/repository/org/codehaus/plexus/plexus-utils/1.5.5/plexus-­utils-1.5.5.jar
[ERROR] urls[5] = file:/C:/Users/gpe/.m2/repository/org/codehaus/plexus/plexus-interpolation/1.1/p­lexusinterpolation1.1.jar.......
---------------------------

Does anybody know how can I get it work?

Thanks
Gerald

David García González
3/19/12 3:07 AM

Hi everybody. Is there any documentation about the goals of liferay plugins?

Where could I read about the configuration of these goals. It could be very useful a page like this, where it is explained the goals of "war" with the options to generate the war:

http://maven.apache.org/plugins/maven-war-plugin/index.html