Create and deploy an OSGi application to Liberty in the cloud

We show how to deploy an OSGi application to Liberty in Cloud Foundry. We use an existing OSGi application tutorial to help you create the OSGi web application and then we describe how to deploy it.

Deploying to the cloud means you can share your application with the rest of the world without having to go through all the hassle of setting up all the infrastructure. You bring the application and the cloud does the hard work for you. So let’s get started!

Setting up the development environment

We are going to base these instructions on using the Eclipse IDE so install Liberty Profile and WebSphere Developer Tools:

  1. Download and install the WebSphere Application Server Liberty Profile
  2. Download and install the WebSphere Application Server Developer Tools for Eclipse

Getting started with Cloud Foundry

To use the Cloud Foundry cloud you will need an account and the command line tool:

  1. Get an account on Cloud Foundry.
  2. Install the cf command line tool to interact with Cloud Foundry.

Your environment is now ready for you to create your OSGi web application.

Creating an OSGi web application

To create your first OSGi web application, follow Lesson 1 to Lesson 5, which provides detailed instructions on how to create a simple counter application. The application will display a simple message of “counter=1″ in the browser and every time the page is reloaded it will increment the counter. When the instructions ask you to to choose the “Target runtime” you’ll want to use the Liberty Profile server you’ve just installed.

Once you have finished Lessons 1 through 5, you will be ready to export an EBA (the archive format for OSGi applications used by Liberty Profile) file and deploy that EBA to Cloud Foundry.

Deploying the OSGi web application to Liberty in the cloud

Cloud Foundry has the concept of build packs that allow different runtimes to be enabled for the Cloud Foundry environment. Cloud Foundry has a Liberty Build Pack for running applications on Liberty in the Cloud. This includes support for the OSGi web application that you created in the earlier steps.

To deploy your application to the cloud:

  1. Export the CounterApp application you created above as an EBA:
    1. Right-click the CounterApp project; click Export > OSGi Application (EBA).
    2. In the dialog, clear the Replace qualifier with time stamp option.
    3. In the To EBA File: field type in counterapp.
    4. Click Browse next to the To EBA File: field and create a CounterApp/dropins folder on your desktop.
    5. Click Finish.
    6. The CounterApp.eba file should be created in the CounterApp/dropins folder
  2. Export the server.xml file into the CounterApp folder:
    1. In the Enterprise Explorer view, expand WebSphere Application Server V8.5 Liberty > servers > defaultServer.
    2. Right-click server.xml and click Export.
    3. In the dialog that opens, expand General and select File System then click Next.
    4. In the To directory: field, click Browse and select the CounterApp folder you created earlier, then click Finish.
  3. Edit the server.xml file:
    1. Using your favorite editor, open the server.xml file you just exported.
    2. Delete the following tags:
      • <applicationMonitor> (delete the entire tag line)
      • <osgiApplication> (delete the entire tag line)
      • These lines need to be removed as they are specific to the eclipse environment that was created in the tutorial and they will not work properly in the cloud environment.

    3. Save the file.
  4. In a terminal or command window, change directories to the CounterApp directory and issue the following command:
    1. cf push --name counterapp --buildpack https://github.com/cloudfoundry/ibm-websphere-liberty-buildpack
    2. Accept the default values for all the questions that are asked during the push process.

Your OSGi web-based application is pushed to the cloud and, when it has completed, a URL to open your application will be displayed. For the CounterApp application, go to http://counterapp.cfapps.io/CounterWebBundle/CounterServlet.