Writing Eclipse SWT apps using Maven

I’ve been trying to get my feet wet writing code for the Eclipse platform. I figured a good first step is to become familiar with Eclipse SWT—the Standard Widget Toolkit, and work my way up from there.

Since I use Maven, I had to struggle a bit finding out how to configure my Maven project properly so that it’ll build an SWT app that’ll run both under Eclipse and from the terminal.

A little Googling brought me to Brice Lambi’s post on Maven SWT builds, but that was a little outdated.

In particular (and only after careful reading), the guide to Deploying SWT apps on Mac OS X says that the -Djava.library.path=.. option is needed for Eclipse 3.2.2 and earlier. Apparently, starting with 3.4 all you need is your OS-specific JAR (see here).

Anyway, after much fiddling about, I was able to get it all working like so:

Download and Install SWT to your local repository

First, you’ll want to download and extract the SWT release specific to your OS from eclipse.org.

(I’m using swt-3.4.2-carbon-macosx.zip for the rest of this article.)

Next, you’ll want to install the extracted swt.jar into your local Maven repository with a suitable groupId and artifactId. Following the conventions on the Ibiblio Maven repository, I used org.eclipse.swt.carbon and macosx, respectively. Here’s the Maven command to do this:

mvn install:install-file -DgroupId=org.eclipse.swt.carbon -DartifactId=macosx -Dversion=3.4.2 -Dpackaging=jar -Dfile=swt.jar

You might also want to point Maven to the SWT sources, so that if you use m2eclipse it can ‘attach’ those sources to the library JAR appropriately:

mvn install:install-file -DgroupId=org.eclipse.swt.carbon -DartifactId=macosx -Dversion=3.4.2 -Dpackaging=jar -Dfile=src.zip -Dclassifier=sources

Configure your POM

A thousand XML elements is worth a thousand words:

<project xmlns="http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance&quot;
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd&quot;>



          <name>mac os x</name>


If you’re only ever going to target one OS, then you can do without the <profiles... > section and ‘hard-code’ your OS-specific dependencies directly. I’ve left it there as a template to make your Maven project more portable.

Run it!

To run this from the command line, I made a simple bash script that basically goes:

java -XstartOnFirstThread -cp ~/.m2/repository/org/eclipse/swt/carbon/macosx/3.4.2/macosx-3.4.2.jar:

The real trick there is the -XstartOnFirstThread option to the Java runtime.

This is the same trick you’ll need to run the app under Eclipse. All you need to do is in Run -> Run Configurations..., in the “Arguments” tab specify -XstartOnFirstThread in the “VM arguments” field and you’re all set!

(Edited 7/16/09 to align the SWT group and artifact id’s with those on the Ibiblio Maven repository, and added the Windows profile.)

4 thoughts on “Writing Eclipse SWT apps using Maven

  1. I’m having trouble getting this to work. I have the profiles set up just like you show, and then include variables in the dependencies section just like your example. However, the variables are not being resolved into the appropriate group and artifact IDs. IS there a plugin or something else I need to activate to get this to work?

    Thanks for the post!

    1. Hi, Trevor. Are you saying that Maven isn’t able to trigger the appropriate profile (which would give the build variables the appropriate values for groupId and artifactId)? What OS are you running on? Have you checked the docs of Maven Enforcer Plugin and lopica.sourceforge.net to see what the correct values might be for your system? Note that I haven’t tried this on Linux so you’ll probably have to do a little experimenting to get all the ids right.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s