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

About these ads

4 Responses to “Writing Eclipse SWT apps using Maven”

  1. Ivan Says:

    Thanks. Helped me a lot.

  2. Trevor Says:

    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!

    • Alistair Says:

      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.


  3. [...] you need to build an SWT application that runs on multiple platforms, and you use Maven to build, this link is really [...]


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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: