Skip navigation.
Home

How to create a Spring bundle project in maven (and eclipse...)

The Spring-OSGi project supplies a maven archetype that will create a Spring bundle project for you all set up and ready to go.

To create a new project simply type the following:

mvn archetype:create   \
-DarchetypeGroupId=org.springframework.osgi \
-DarchetypeArtifactId=spring-osgi-bundle-archetype \
-DarchetypeVersion=1.0 \
-DgroupId=<your-project-groupId> \
-DartifactId=<your-project-artifactId> \
-Dversion=<your-project-version>

The result of this is a maven project that defines a simple bean, configures it using src/main/resources/META-INF/spring/bundle-context.xml and src/main/resources/META-INF/spring/bundle-context-osgi.xml, and provides unit and (out of container) integration tests. The project is packaged as an OSGi bundle.

There project does not contain a META-INF manifest yet - to generate one invoke the following command:

mvn package 
mvn org.apache.felix:maven-bundle-plugin:manifest

You should now see the MANIFEST under target/classes/META-INF/ folder (the upcoming 1.0.1 version will properly generate this folder under META-INF).

Note: if you haven't already downloaded and installed the spring-osgi artifacts (by running 'mvn install' on the spring-osgi tree) you'll need to add an additional parameter to the above command: -DremoteRepositories=http://s3.amazonaws.com/maven.springframework.org/milestone. If this parameter does not work for you first time, see the additional instructions here.

Second Note: Snapshots version are available from: -DremoteRepositories=http://s3.amazonaws.com/maven.springframework.org/snapshot

Simply 'cd' into your newly created project directory and type 'mvn install' and you are good to go!

Generated Project features at-a-glance:

  • Packaged as an OSGi bundle
  • src/main/resources/META-INF/MANIFEST.MF for OSGi bundle automatically generated (may need some hand-editing for bundle name and Import-Package entries).
  • Simple bean interface and implementation defined. Interface and implementation types are in different packages, only the interface package is exported by the bundle.
  • src/main/resources/META-INF/spring/bundle-context.xml is a spring configuration file that defines the simple bean.
  • src/main/resources/META-INF/spring/bundle-context-osgi.xml is a spring configuration file ready for you to add bean definitions from the osgi namespace (services, references etc.).
  • BeanImplTest case defined to unit test the simple bean
  • BeanIntegrationTest defined to fire up the non-osgi portions of the application context configuration and test outside of OSGi
  • .project, .classpath, and build.properties files created to enable use of this project directly inside eclipse as a PDE plugin project

Using the archetype from within Eclipse

You can make it really easy to create a new Spring-OSGi bundle project from within eclipse by defining a new "External Tools" launch configuration. Open the "External Tools" configuration dialog, select the "Program" category and click "new". Now you can define a new launch configuration as shown below:

The "${string_prompt}" entries cause eclipse to pop up a dialog asking you for a value when you run tool. You can also change the working directory to be something other than the workspace location (or even a ${folder_prompt} prompt if you like). This is the directory in which your new project will be created.

Now the creation of a new Spring-OSGi bundle project is as easy as:

Note: the generated eclipse project depends on a number of the spring-osgi bundle projects that it expects to find installed in your workspace. See the instructions here for setting these additional projects up (one-time task).

More refinements..

I got the archetype by follwing the instructions here, but it wouldn't build out of the box.  Eclipse was missing all the dependencies from .classpath.  The easiest way to get them is to run Maven on the command line first, but that didn't work either - I had to uncomment the repositories section in the pom.xml.  Why would that be disabled by default?  I also had to do "mvn eclipse:eclipse" again after the build worked on the command line to get all the dependencies into the .classpath.

That section of the pom contains some pretty dodgy looking stuff as well, including plugin snapshot repos and old spring repos that are not officially supported (should use the s3 repo now not the static.springframework one).  Can't it be pared down to what is actually needed?

And can't we use Spring 2.5 instead of those old manually packaged 2.1 snapshots?

The comment above "may need some hand-editing" should read "will need a lot of hand-editing".  Surely this can be done by the archetype plugin?

The pom is also a bit strange - it doesn't have the group and artifact id that was specified on the command line.

I finally got the solution

I finally got the solution:

mvn archetype:create
-DarchetypeGroupId=org.springframework.osgi
-DarchetypeArtifactId=spring-osgi-bundle-archetype
-DarchetypeVersion=1.0-SNAPSHOT
-DremoteRepositories= http://static.springframework.org/maven2-snapshots
-DgroupId=maventest
-DartifactId=maventest
-Dversion=1.0.0

this command runs correctly on my computer

A few minor corrections

Just noticed a few more things of note with regard to this OSGi archetype...

First, the "-DarchetypeId" parameter (next to the last one in Adrian's original post) should be "-DartifactId".

Also, I said that once you run the archetype with the "-DremoteRepository" the first time you won't have to use it again. That's not exactly true...because it's a SNAPSHOT version, you'll need to use the "-DremoteRepository" parameter once every day (or maybe change your Maven configuration's SNAPSHOT policy).

corrections summary

For me the remote repository parameter only worked after updating the archetype plugin as pointed out by delasoul in another post (see http://jira.codehaus.org/browse/TRAILS-2).

Furthermore, the correct parameter name seems to be '-DremoteRepositories=' instead of '-DremoteRepository='.

This basically means to first run


mvn -U archetype:create

and then

mvn archetype:create
-DarchetypeGroupId=org.springframework.osgi
-DarchetypeArtifactId=spring-osgi-bundle-archetype
-DremoteRepositories=http://static.springframework.org/maven2-snapshots
-DarchetypeVersion=1.0-SNAPSHOT
-DgroupId=<your-project-groupId>
-DartifactId=<your-project-artifactId>

it might also work to combine these two statements by simply adding the -U option to the full archetype:create command, though I did not verify this.

Need to specify plugin repository

This is wonderful (I'm a huge fan of Maven archetypes), but fails to mention that you need to specify the remote plugin repository the very first time you run it:

mvn archetype:create ... -DremoteRepository=http://static.springframework.org/maven2-snapshots

(Fill in the ellipses with all of the stuff in Adrian's original post.)

This tells Maven where to download the archetype plugin from. (You *should* be able to specify the remote repository in settings.xml, but that isn't working in the latest release of Maven. See http://jira.codehaus.org/browse/ARCHETYPE-1.)

Once you've used the archetype, the plugin will be placed in your local Maven repository and you won't need to specify the remote repository again.

remoteRepository doesn't work for MAVEN 2.0.4

I tried to follow your tips and wasn't been able to achieved the installation of the plugins with the -DremoteRepository (also tried repositories in settings.xml)
So I've install "by hand" (using install:install-file
). Once I've installed spring-osgi-bundle-archetype and import the project into my Eclipse workspace, I had to install (to avoid eclipse classpath errors) also:
- commons-logging.osgi
- spring-beans
- spring-context
- spring-core (in the repository it appears with a bomb ico!!! What does it mean?)
- spring-mock

Now the Eclipse project is OK

to get -DremoteRepositories

to get -DremoteRepositories working see
http://jira.codehaus.org/browse/TRAILS-2
You have to update your archetype create first before it uses this param,

hth,

Michael

Thanks for the feedback

Thanks everyone for the feedback and comments on these instructions. I've folded them into the main body of the text.

-- Adrian