OSGi, Maven and Scala

Jan 10, 2008

Original blog post

 

 

 

I’m inspired by Neil Barlett’s experiment on writing OSGi bundles in scala. This blogpost will show you how to compile OSGi bundles written in scala using pax-construct.

First here’s the modified source from Neil’s blog post:

// ------------------------ An OSGi service interface ------------------------------
package com.raverun.scala;

trait ExampleService
{
    def hello():String
}

// -------------------- The service implementation ------------------
package com.raverun.scala.internal;

import com.raverun.scala._

class ExampleServiceImpl extends ExampleService
{
    def hello():String = {
        return "Hello"
    }
}

// -------------------- our bundle activator --------------------------------
package com.raverun.scala.internal;

import org.osgi.framework._
import com.raverun.scala.ExampleService

class Activator extends BundleActivator
{
    var m_registration:ServiceRegistration

    def start( context: BundleContext )
    {
         var bundleNames = context.getBundles()  
                  .map   (b => b.getSymbolicName())  
                  .filter(b => b != context.getBundle());  
         Console.printf("Installed bundles: {0}",  
                         bundleNames.toString());  
   
         // register our service  
         m_registration = context.registerService( "com.raverun.scala.ExampleService",  
                                                   new ExampleServiceImpl(),  
                                                   null );  
    }  

    def stop( context: BundleContext )
    {
        if( m_registration != null )
            m_registration.unregister
    }
}

Except for the service definition, all the rest is lifted directly from Neil's blogpost. Software Prerequisites:

Create the pax-construct project

Before we can create bundles, it is mandatory to create a "project".

$ pax-create-project -g my.osgi.project -a scalaosgi

$ cd scalaosgi

For the rest of the article, I will refer to scalaosgi as PROJECTDIR

Next, we add the maven repositories for scala tools.

$ pax-add-repository -i scala-tools.org -u http://scala-tools.org/repo-releases

$ pax-add-repository -i scala-tools.org -u http://scala-tools.org/repo-releases -- -DpluginRepo

To round it up, add the bundle for the scala runtime into our provisioning setup.

$ pax-import-bundle -g org.scala-lang -a scala-library -v 2.6.1

Create a test bundle

Staying in PROJECTDIR, execute the following:

$ pax-create-bundle -p com.raverun.scala -n hello

$ cd hello

You are now inside the directory hello, which we'll refer to as BUNDLEDIR from now on.

Add the following dependencies to the pom.xml file.

    
      org.scala-lang
      scala-library
      ${scala.version}
      provided
    
    
      junit
      junit
      3.8.1
      test
    

Next, remove the generated java source code, and drop in the scala sources (found at the top of this article) into src/main/scala

$ rm -rf src/main/java

$ mkdir -p src/main/scala/com/raverun/scala/internal

Inform maven how to compile your scala sources

Change into directory PROJECTDIR/poms/compiled

Edit pom.xml and add the following snippets directly after the build element:

  
    src/main/scala
    src/test/scala
    
      org.scala-tools
      maven-scala-plugin
      
        
        
          compile
        
        
      
      
        ${scala.version}
      
    

    -----snipped--o<-----
  

Add maven settings

Change directory to ~/.m2/

Create a settings.xml file with the following contents.


  
    
      scalaosgi
      
        true
      
      
        2.6.1
      
    
  


Finishing touches

Change directory to PROJECTDIR

Compile and start the bundle

$ mvn install pax:provision

By default, it will start Apache felix.

Welcome to Felix.
=================

-> Installed bundles: Array(System Bundle, org.osgi.r4.compendium, org.apache.felix.shell, 
   org.apache.felix.shell.tui, my.osgi.project.scalaosgi.hello, scala_library)
-> ps
START LEVEL 6
   ID   State         Level  Name
[   0] [Active     ] [    0] System Bundle (1.0.1)
[   1] [Active     ] [    1] org.osgi.r4.compendium (1.0.0)
[   2] [Active     ] [    1] Apache Felix Shell Service (1.0.0)
[   3] [Active     ] [    1] Apache Felix Shell TUI (1.0.0)
[   4] [Active     ] [    5] my.osgi.project.scalaosgi.hello [com.raverun.scala] (1.0.0.SNAPSHOT)
[   5] [Active     ] [    5] Scala Library Bundle (2.6.1)
->
Creative Commons License
OSGi, Maven and Scala by Gavin Bong est mis à disposition selon les termes de la licence Creative Commons Paternité-Pas d'Utilisation Commerciale-Partage des Conditions Initiales à l'Identique 3.0 Unported.