Building with Tycho – part 1 (OSGi bundles)

来源:互联网 发布:c语言中-i 是什么意思 编辑:程序博客网 时间:2024/06/03 05:53

http://mattiasholmqvist.se/2010/02/building-with-tycho-part-1-osgi-bundles/

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 asPDE 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 ofmvn 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:

view source
print?
1mvn3 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 anotherpom.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:

view source
print?
1mvn3 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:

view source
print?
1-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:

view source
print?
1mvn3 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 sectionpom.xml for our parent project:

view source
print?
1<plugin>
2  <groupid>org.sonatype.tycho</groupid>
3  <artifactid>target-platform-configuration</artifactid>
4  <version>${tycho-version}</version>
5  <configuration>
6    <resolver>p2</resolver>
7  </configuration>
8</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 thegenerate-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:

view source
print?
01<settings>
02  <profiles>
03    <profile>
04      <id>tycho</id>
05      <repositories>
06        <repository>
07          <id>galileo</id>
08          <layout>p2</layout>
09          <url>http://download.eclipse.org/releases/galileo/</url>
10        </repository>
11      </repositories>
12    </profile>
13  </profiles>
14</settings>

It is now possible to build the simple bundle project by using:

view source
print?
1mvn3 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.

原创粉丝点击