Thursday, November 24, 2011

Starting with CMIS and Maven

This post aims to be an short how-to for setting up a CMIS development environment based on Maven and Apache Chemistry, specifically the OpenCMIS Java API, part of the Chemistry project.

I won't cover Maven installation and configuration here, so I assume you have Maven 2 or 3 up and running. With Maven you'll be independent from any specific IDE, so that you can manage your development cycle from the command line only.

Glossary
  • CMIS (Content Management Interoperability Services) =>"is a specification for improving interoperability between Enterprise Content Management systems. OASIS, a web standards consortium, approved CMIS as an OASIS Specification on May 1, 2010. CMIS provides a common data model covering typed files, folders with generic properties that can be set or read. In addition there may be an access control system, and a checkout and version control facility, and the ability to define generic relations. There is a set of generic services for modifying and querying the data model, and several protocol bindings for these services, including SOAP and Representational State Transfer (REST), using the Atom convention. The model is based on common architectures of document management systems."
  • Apache Chemistry => "Apache Chemistry provides open source implementations of the Content Management Interoperability Services (CMIS) specification.
  • OpenCMIS => "Apache Chemistry OpenCMIS is a collection of Java libraries, frameworks and tools around the CMIS specification. The goal of OpenCMIS is to make CMIS simple for Java client and server developers. It hides the binding details and provides APIs and SPIs on different abstraction levels. It also includes test tools for content repository developers and client application developers."
  • Apache Maven => "Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project's build, reporting and documentation from a central piece of information."
Ingredients
  1. A simple text editor or any decent Java IDE 
  2. Maven 2 or 3
  3. A CMIS server for real-world testing
In my case I'm using IntelliJ IDEA, which is excellent. I'm an old Netbeans guy and both IDEs offer superior Maven integration, but it happens that I'm just having a look at IntelliJ these days.

To cover point # 3 I have selected the reference CMIS server implementation so far, which is Alfresco.  OpenCMIS offers a basic CMIS server implementation for your self-contained unit tests, but for end-to-end integration testing I prefer to link to a real ECM system.

You can download the latest Alfresco Community Edition for free from here. At present the brand new 4.0 is available.

Setup

Note: I won't cover Alfresco's installation and configuration here because it's not in the scope of this post. You can already find plenty of excellent online resources for that.

Just open a shell, place into a folder and run the following Maven command, to create a very basic Java project through the quickstart archetype:

mvn archetype:generate -DgroupId=com.myapps \
                       -DartifactId=my-first-cmis \
                       -Dversion=1.0-SNAPSHOT \
                       -DarchetypeArtifactId=maven-archetype-quickstart \
                       -DinteractiveMode=false

You'll end having the following usual project structure:

project
|-- pom.xml
`-- src
    |-- main
    |   `-- java
    |       `-- App.java
    `-- test
        `-- java
            `-- AppTest.java

The pom.xml file is the center of the Maven's universe. We need to edit it for adding a few lines of XML so that we can build with OpenCMIS libraries.
Here is the default pom.xml created by the archetype:

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.myapps</groupId>
  <artifactId>my-first-cmis</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>my-first-cmis</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

Now we need to put the following XML snippet into pom.xml to activate the OpenCMIS libraries:

<dependency>
   <groupId>org.apache.chemistry.opencmis</groupId>
   <artifactId>chemistry-opencmis-client-impl</artifactId>
   <version>0.6.0</version>
</dependency>

At present the latest stable OpenCMIS release is 0.6.0, you can modify the pom.xml file accordingly whenever a new version is released.

This is the final POM file:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.myapps</groupId>
  <artifactId>my-first-cmis</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>my-first-cmis</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>org.apache.chemistry.opencmis</groupId>
      <artifactId>chemistry-opencmis-client-impl</artifactId>
      <version>0.6.0</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

Now that your development environment is ready and you can build both from the shell and the IDE, you can start exploring some examples.

Issuing a mvn clean compile command in your shell will start the process.
If it's the first time you run Maven then it will try to download many dependencies, but don't worry and be patient, all successive runs will be very fast.


No comments:

Post a Comment