Maven, and an easy way to use it with XPages

For some time now the Domino community, with good reason, has been very focused on using Java inside of Domino, specifically with XPages.  There are some great reasons to use Java, but one of the foremost reasons is support from the Java community in both word (Stackoverflow) and deed (Third party libraries).   These third party libraries are a huge reason to be a Java developer.  They come in all shapes and sizes but in the end it means less code that you have to write that you can trust.  I’m sure that if you have been using Java for any length of time, you have probably searched for a library to do something, found it, and went to the download page, only to be presented with some XML tags instead of a download link.  These XML Tags might look like this:

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.1.1</version>
</dependency>

This is the typical structure to add a Maven dependency to your project.

Maven is an application that can do many things as listed below, however in this case we are using Maven as a package tool so that you when you go to build your project it will download any dependencies and (here is the key) any dependencies the dependencies have.  For Instance I might add the Social Business Toolkit as a dependency to my project.  When maven runs on my project it will download the apache http client library because its a dependency for the sbt.  I don’t have to mention the apache http client anywhere, Maven looks at the projects configuration and figure out its dependencies.

  • Making the build process easy
  • Providing a uniform build system
  • Providing quality project information
  • Providing guidelines for best practices development
  • Allowing transparent migration to new features

So with Domino we are use to having dependencies as JAR files instead of as a list in an XML file.  There is a way to make Maven build all of the Dependencies you have as a single JAR file.  Basically this will allow you to work inside of your project as you always have without adding a dependency on Maven and hopefully will make your life easier.

Its really pretty simple also.

Maven is an install-able executable that you can get from here:

https://maven.apache.org

Once installed on your PC, you just have to make sure the mvn.exe is in your PATH environment variable

So every project that is to be build with Maven requires a pom.xml this file describes what dependencies the project has among other things.

So if you add a dependency like the apache http client above, when maven builds your project it will download the appropriate version for you.

If you use the maven-assembly-plugin it will then build put all of the jars together into a single jar that you can add to an agent or an xpages project.

So How do you do this you might be wondering. First you need a pom.xml file inside of an empty directory.  It needs some basic information about your project, Below is the a sample

 

<project xmlns=”http://maven.apache.org/POM/4.0.0&#8243; xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221;
xsi:schemaLocation=”http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”&gt;
<modelVersion>4.0.0</modelVersion>
<groupId>com.tobysamples.test</groupId>
<artifactId>tsamples</artifactId>
<version>0.1.0</version>
<packaging>jar</packaging>
<name>test</name>
<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.1.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
</project>

 

So you can create a new pom.xml file with the above in it in an otherwise empty directory.

Then all you have to do is change to that directory using the command line then run “mvn package”

This will try to package all of the dependencies together as a single jar and save them in your directory.

If you look in you will now see a Target directory that holds the jar that you can attach to your project.  Hopefully this will help some of you not get stuck in dependency hell that Java can be sometimes.

Maven can get a little complex and is a whole new set of skills to learn, but they can and will make your life easier if you get your head wrapped around it.

Advertisements

One thought on “Maven, and an easy way to use it with XPages

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s