Writing Eclipse SWT apps using Maven
July 15, 2009
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 your 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:
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>swt-test</artifactId>
<packaging>jar</packaging>
<version>0.1</version>
<dependencies>
<dependency>
<groupId>${swt.groupId}</groupId>
<artifactId>${swt.artifactId}</artifactId>
<version>3.4.2</version>
</dependency>
</dependencies>
<profiles>
<profile>
<id>mac</id>
<activation>
<os>
<name>mac os x</name>
</os>
</activation>
<properties>
<swt.groupId>org.eclipse.swt.carbon</swt.groupId>
<swt.artifactId>macosx</swt.artifactId>
</properties>
</profile>
<profile>
<id>windows</id>
<activation>
<os>
<family>windows</family>
</os>
</activation>
<properties>
<swt.groupId>org.eclipse.swt.win32.win32</swt.groupId>
<swt.artifactId>x86</swt.artifactId>
</properties>
</profile>
</profiles>
</project>
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:
target/swt-test-0.1.jar
com.example.swt.test.Main
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.)