Maven is as many of us know a good tool for managing dependencies between components. However, when developing OSGi bundles or Eclipse plugins, we typically want to specify our dependencies as imported/exported packages in our Manifest. This has caused trouble for headless building of OSGi bundles for some time although there exist some solutions such as PDE build and BND tool.
The Tycho project provides plugins to Maven which will make it possible (and rather simple) to build OSGi bundles with Maven . There is currently a project overview which provides a good starting point (although some of the documentation is quite outdated). The best source for help is the Tycho users mailing list.
This post will be the first in a series of posts covering building OSGi bundles/Eclipse RCP applications with Tycho.
It is important to remember that Tycho relies on new features of Maven which is only currently available in the 3.0 version of Maven which has not yet been released, but it is possible to try this out by downloading an alpha version of Maven 3.0.
Environment setup
I downloaded the alpha-6 from the Maven download site. I also set up my environment to use mvn3 instead of mvn for Maven 3 since I didn’t want to break my Maven 2 installation.
Since Tycho has been pushed to Maven central since version its last couple of revisions (0.5.0 i think), we don’t need to download anything else or setup any additional Maven repositories.
Building OSGi bundles
Fire up Eclipse (I use 3.5.1) and create a new project. Choose plug-in project and choose the target to be Equinox runtime. This creates a simple bundle without any plugin.xml or other Eclipse-specific dependencies.
I also chose to create an Activator to have some code to compile.
I recommend that you keep your source code in a different directory from the workspace. That’s my personal preference but it keeps Eclipse IDE from cluttering the source directory too much.
Open up your command line and go to the directory that contains your bundle project. In my case it looks like this:
mvn3 org.sonatype.tycho:maven-tycho-plugin:generate-poms -DgroupId=se.mattiasholmqvist -Dtycho.targetPlatform=/Users/mattias/dev/eclipse-3.5.1-delta/
Tycho is downloaded and it generates a pom.xml for the current directory (parent project) and another pom.xml for the bundle project. It should look something like:
So, Tycho has generated two poms for us. Let’s get ready to build our OSGi bundle! Try:
mvn3 clean install
and watch the magic!?
What’s the problem here? Maven tells us that it cannot resolve the project due to a missing OSGi dependency to org.osgi.framework. It considers missing OSGi dependencies as build errors. This means that we must set up a proper target platform for Tycho, and tell it to use that target platform when building. This is required for any proper build environment that builds OSGi bundles.
Currently (version 0.7.0) Tycho supports two different ways of specifying a target platform.
Building with an explicit target platform
We can expliticly tell Tycho to use an Eclipse installation using:
-Dtycho.targetPlatform=/path/to/target
I downloaded the Eclipse SDK (3.5.1) and untarred it into /Users/mattias/dev/eclipse-3.5.1/. I can now build my simple bundle from the command line with:
mvn3 clean install -Dtycho.targetPlatform=/Users/mattias/dev/eclipse-3.5.1
Using an implicit target platform
If you don’t want to keep track of the target platform locally, it is possible to build the OSGi bundle with an implicit target platform. To do this, we need to help Tycho along by specifying that it should try to resolve the dependencies using a resolver. We also need to help Tycho by specifying locations (repositories) where it might find artifacts that matches our needs.
We’ll start by telling Tycho that it should use a p2 resolver (we’ll use Equinox from Eclipse.org as our target platform). We’ll add the following to the build section pom.xml for our parent project:
<plugin>
<groupid>org.sonatype.tycho</groupid>
<artifactid>target-platform-configuration</artifactid>
<version>${tycho-version}</version>
<configuration>
<resolver>p2</resolver>
</configuration>
</plugin>
This tells Tycho to use an implicit resolver that supports p2 repositories. We are now one step from being able to build with an implicit target platform.
The ${tycho.version} is a Maven property that I use to switch between versions of Tycho. This is set to 0.7.0 in my case. If you want to use just this version, replace the property with the version literals. The version of this plugin should be the same as for the tycho-maven-plugin entry that was auto-generated to the pom.xml from the generate-poms target used earlier.
We need to setup the repositories that Tycho should use at build time. This is accomplished by normal repository configuration Maven-style. Tycho enhances these configuration options by supporting p2 repositories as well. Open your settings configuration (in my case ~/.m2/settings.xml) and add the following:
<settings>
<profiles>
<profile>
<id>tycho</id>
<repositories>
<repository>
<id>galileo</id>
<layout>p2</layout>
<url>http://download.eclipse.org/releases/galileo/</url>
</repository>
</repositories>
</profile>
</profiles>
</settings>
It is now possible to build the simple bundle project by using:
mvn3 clean install
That’s it for now. Hope this helps with your headless OSGi builds! Keep your eyes open for the next part in this series of posts.
Some interesting links:







You need to escape the XML brackets in your post as otherwise they are invisible in the HTML view.
Alex
Thanks Alex.
[...] Building with Tycho – part 1 (OSGi bundles) « Mattias Holmqvist [...]
Maven archetype for setting the project(s) including bundle, test fragment and update site would be great thing to have. It’s not trivial to set up tycho based project(s).
Dear,
I’m fairly new to Tycho and Maven 3.
I’m trying to make an automatic build process for making Eclipse plugins with dependencies in the manifist to a third-party software.
I installed maven 3, made a test plugin project without activator in Eclipse.
I tried to follow your guide step-by-step, but i didn’t succeed.
Is there a way i could mail you to ask some questions?
Kr,
Danny Van Dijck
Sure you could ask me by mail. I would like to hear which part of the guide that failed. You can also try the Tycho users mailing list, there are lots of helpful people there.
I have followed the above guide but when I get to the point where I need to run:
mvn clean install -Dtycho.targetPlatform=c:\Programs\eclipse
I get the error:
Unknown packaging: eclipse-plugin
If I change the packing type to jar in the produced pom.xml file it works. But is the whole point not that it should be possible to build eclipse plugins with maven3/tycho?
Hi js. You need to have access to the central maven repository to access the Tycho Maven plugins (or mirror them in an accessible repository). What does your settings.xml look like? Did you modify the repositories section for some reason?
Hi Mattias,
I am pretty new to maven, tycho and OSGi. And your post helped me a lot.
Can you also write about how to write junit testcases in an OSGi bundles?
do we need to create another test osgi project and have the main project as a dependency? or
(the one i prefer)
can we write the test cases in the same osgi bundle? can maven recognize the test cases from the actual code? will it run with a ‘mvn test’ command?
Thanks.
-Prasanth
Hi Prasanth,
If you take a look at my latest post (part 3) there’s an example on how to execute Junit tests from Tycho. However, these tests are in a separate bundle. I’m not sure what you would need to have them in the same bundle. In my projects, I’ve always separated the tests from the production code but I guess you could post a question on the Tycho users mailing list if you really need this feature.
Regards, Mattias
Hi Mattias,
I have the same problem as js.
First I build tycho 0.10.0 Snapshot and then created an plugin project described above. After the success of generating the pom.xml I tried “mvn clean install” but recieving an error “[ERROR] Unknown packaging: eclipse-plugin”
Do you know why?
Hi,
I found the problem. It was my incomplete pom.xml.
Thanks anyways.
-Tom
I am new in the maven/tycho/eclipse-pde business but managed to build the bundle using the above steps using eclipse helios 3.6 and the eclipse-delta-pack 3.6.
But then what? I would like to add some dependencies like google.guice to the generated pom.xml file and start writing my application. But they are not visible in eg. the Activator.java file.
I have tried to enable the maven dependency management on the project and adding eg. guice. As a result Injector is visible in the Activator.java file but I cannot build the project from the commandline with maven3 any more.
Should I use the MANIFEST file for the dependencies instead of the pom.xml file (thought the whole point was to use the pom-first approach)?
Hi Tom,
I got the same problem “Unknown packaging: eclipse-plugin”.What was incomplete and in which pom?
Thanks.
room
I’m also hitting the Unknown packaging: eclipse-plugin bit. Tom, can you post your fix?
Also, in the pom snippets above groupid should be groupId (cap the “i”) – same for artifactid (artifactId).
Thanks,
Hi room,
Sorry for the late anser due to my vacation.
Which pom: my parent pom
What: there were missing some plugins e.g. the tycho plugin
You should take a look at Mattias Github acc
http://github.com/mattiasholmqvist/tychoexamples
concrete in his parent pom