Posts Tagged ‘Tycho’

Building Eclipse plugins with Maven

Tuesday, March 8th, 2011

In this short tutorial I will explain how to build Eclipse plugin with Maven and Tycho. I decided to write this tutorial as I struggled to manage to build ModelGoon in an “industrial” manner with Maven. This tutorial supposes that the reader has a good knowledge of Maven builds and Eclipse Plugins structure.

Introduction :

Building an Eclipse plugin with maven is quite different from building other Java applications. OSGi bundles are stored in  p2 repositories (as Maven uses m2 repositories) and defines their versions and dependencies in the Manifest ; Maven uses the POM. In order to perform the build, we will use a maven plugin called Tycho and the M2Eclipse plugin with Tycho support.

What I did for ModelGoon is to create a Maven multi-module project containing :

  • the plugin’s implementation
  • the test plugin : contains all unit tests
  • the feature
  • the update site
  • the target platform description

In the next sections I will only focus on the Tycho specific part.

Parent Project Configuration :

The parent Project’s POM adds the declaration of “tycho-maven-maven” and describes the target platform’s configuration.

<
packaging>pom</packaging>

…..

<plugin>
<groupId>org.sonatype.tycho</groupId>
<artifactId>tycho-maven-plugin</artifactId>
<version>${tycho-version}</version>
<extensions>true</extensions>
</plugin>
<plugin>
<groupId>org.sonatype.tycho</groupId>
<artifactId>target-platform-configuration</artifactId>
<version>${tycho-version}</version>
<configuration>
<resolver>p2</resolver>
<pomDependencies>consider</pomDependencies>
<target>
<artifact>
<groupId>org.modelgoon</groupId>
<artifactId>TargetPlatform</artifactId>
<version>4.2.0-SNAPSHOT</version>
<classifier>platform</classifier>
</artifact>
</target>
</configuration>
</plugin>

Target Platform Configuration :

First of all we need to create a target Definition :  “New->Plugin Development->Target Definition”
Then we add referenced p2 repositories in it. This is where the tycho plugin will search for plugins bundles. For instance :
-  http://download.eclipse.org/eclipse/updates/3.6 Official Eclipse p2 repository
-  http://download.eclipse.org/tools/gef/updates/releases GEF’s p2 repository
Note that it is also possible to work will a local repository if there is no internet connexion available. In this case, just create an update site project, add all of the proposed bundles, and host it on a local web server.
The project packaging must be “pom” :
<packaging>pom</packaging>

Then just add the following line into the project’s pom.xml :
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.3</version>
<executions>
<execution>
<id>attach-artifacts</id>
<phase>package</phase>
<goals>
<goal>attach-artifact</goal>
</goals>
<configuration>
<artifacts>
<artifact>
<file>platform.target</file>
<type>target</type>
<classifier>platform</classifier>
</artifact>
</artifacts>
</configuration>
</execution>
</executions>
</plugin>

Plugin Project Configuration :

The packaging has to be :
<packaging>eclipse-plugin</packaging>
Here comes Tycho’s configuration part :
<plugin>
<groupId>org.sonatype.tycho</groupId>
<artifactId>target-platform-configuration</artifactId>
<version>${tycho-version}</version>
<configuration>
<resolver>p2</resolver>
<target>
<artifact>
<groupId>org.modelgoon</groupId>
<artifactId>TargetPlatform</artifactId>
<version>4.2.0-SNAPSHOT</version>
<classifier>platform</classifier>
</artifact>
</target>
</configuration>
</plugin>
Note that it is still possible to include standard Maven dependencies inside the POM.

Test Project Configuration :

Only the packaging differs from the Plugin project configuration. It has to be :
<packaging>eclipse-test-plugin</packaging>

Feature configuration :

In this part, there is no need for tycho configuration just define the packaging as :
<packaging>eclipse-feature</packaging>

Update Site configuration :

Define the packaging :
<packaging>eclipse-update-site</packaging>
I also added this in the plugins section :
<plugin>
<groupId>org.sonatype.tycho</groupId>
<artifactId>target-platform-configuration</artifactId>
<configuration>
<allowConflictingDependencies>true</allowConflictingDependencies>
</configuration>
</plugin>

Thanks for reading, hope it helps!