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.
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
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:
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.
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.)