Showing posts with label tools. Show all posts
Showing posts with label tools. Show all posts

Monday, July 30, 2007

Maven2 SWT builds




Building a SWT application with maven can be tricky. The public maven repositories don't have current versions of SWT. You must download the recent version for each platform that you need to support and place them in your own repository. Since SWT needs the native libraries for your platform, you will need to package those in individual jar (or zip) files and deploy those to your local repository. I recommend giving them the same artifactId as your Java SWT libraries with a suffix like '-native'. So for three platforms you should have six artifacts like the following.

swt/swt-win32/3.2.2/swt-win32-3.2.2.jar
swt/swt-win32-native/3.2.2/swt-win32-native-3.2.2.jar
swt/swt-linux-gtk/3.2.2/swt-linux-gtk-3.2.2.jar
swt/swt-linux-gtk-native/3.2.2/swt-linux-gtk-native-3.2.2.jar
swt/swt-macosx/3.2.2/swt-macosx-3.2.2.jar
swt/swt-macosx-native/3.2.2/swt-macosx-native-3.2.2.jar

Now you can use profiles to determine the proper dependency and the maven-dependency-plugin to unpack the proper native libraries. First create a profile for each platform that you need to support in your pom.xml file and set properties defining the proper dependencies for each platform:

<profiles>
<profile>
<id>unix</id>
<activation>
<os>
<family>unix</family>
</os>
</activation>
<properties>
<swt.os-specific-dep>swt-linux-gtk</swt.os-specific-dep>
<swt.version>3.2.2</swt.version>
</properties>
</profile>
<profile>
<id>windows</id>
<activation>
<os>
<family>windows</family>
</os>
</activation>
<properties>
<swt.os-specific-dep>swt-win32</swt.os-specific-dep>
<swt.version>3.2.2</swt.version>
</properties>
</profile>
<profile>
<id>mac</id>
<activation>
<os>
<family>macosx</family>
</os>
</activation>
<properties>
<swt.os-specific-dep>swt-macosx</swt.os-specific-dep>
<swt.version>3.2.2</swt.version>
</properties>
</profile>
</profiles>

You can reference these properties in your pom for dependencies and for the unpack goal of the maven-dependency-plugin. The example below uses the appassembler-maven-plugin output directory to unpack the native libraries. You can use the appassembler plugin to create scripts for launching your application and add extra JVM arguments which you will need to do.

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>appassembler-maven-plugin</artifactId>
<executions>
<execution>
<id>assemble</id>
<phase>package</phase>
<goals>
<goal>assemble</goal>
</goals>
<configuration>
<programs>
<program>
<mainClass>esrl.gsd.fsl.enroute.client.Client</mainClass>
<name>enroute-client</name>
</program>
</programs>
<extraJvmArguments>-Djava.library.path=lib</extraJvmArguments>
</configuration>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack</id>
<phase>package</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>swt</groupId>
<artifactId>${swt.os-specific-dep}-native</artifactId>
<version>${swt.version}</version>
<type>jar</type>
<overWrite>true</overWrite>
<outputDirectory>
${project.build.directory}/appassembler/lib
</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

Notice the extra JVM argument given to the appassembler plugin and the output directory for the unpack configuration.

%> mvn package

This will unpack the proper native libraries in the lib directory of the assembled application, and create shell scripts for launching the application as well as copy all the other necessary dependencies to the assembled application. See the dependency and appassembler plugins for more configuration options.

Tuesday, December 5, 2006

Building scala from maven - maven-scala-plugin

I recently wanted to build scala code in maven. There was no plugin to do this but I did find a patch for maven. I didn't like this approach because maven is supposed to be plugin based and you should be able to extend it without changing it. So I wrote a quick and dirty little plugin http://millstone.iodp.tamu.edu/~blambi/maven-scala-plugin(docs). Update 7/11/07: The maven-scala-plugin is available on Google Code

At first I wanted to use the api provided by scala to compile code. I used the scala eclipse plugin as an example, and got it working. When I ran it as a maven plugin I got a strange error: "object scala not found". I assumed this meant that the package scala wasn't available, due to the fact that the scala jar files are not included on the classpath when the jvm is started by maven. So next I tried creating a new ClassLoader and force loading every class in the scala libraries, but I received the same error.

The simple way, I decided, was to run a new jvm with the proper classpath. This is what scalac does when it runs. So I simply setup the classpath and execute 'java scala.tools.nsc.Main'. It may be a naive implementation, but it works and I'm happy I can now compile my scala code using maven.

Friday, December 1, 2006

Maven 2 Quick Guide

Maven 2 is a project management tool that you can use for building, deploying, configuring, documenting, dependency management, etc. It's not just for Java, it will also build many other languages and you can build a plugin to support any language you like.

First create a new project:

mvn archetype:create -DgroupId=my.org -DartifactId=myapp

groupId is whatever you want, it's sort of like a package name or namespace. artifactId is probably your application name, a directory with this value will be created and your final build will be named this value.

Inside the directory just created you will find the src dir. If you are developing a java app, put all your code under src/main/java. There will be some generated code in src/main/java and src/test/java, you can delete it or ignore it.

The easy way to get all your external jar files into your project is to set up your own repository. It can be a local dir, or be hosted with apache. Just make a directory somewhere and start putting jar files in your repository with this command:

mvn deploy:deploy-file -DgroupId=mailapi -DartifactId=smtp \
-Dversion=1.4 -Durl=file:///path/to/repo \
-Dpackaging=jar -Dfile=smtp.jar

If you have ssh keys setup, you can use scp in the url also.
Now you have a repository you can reference in your project. Open pom.xml and add the new repository to your project:

<repositories>
<repository>
<name>myname</name>
<id>someid</id>
<url>file:///path/to/repo</url>
</repository>
</repositories>

Now you can reference your jar files as dependencies in pom.xml.

<dependency>
<groupId>mailapi</groupId>
<artifactId>smtp</artifactId>
<version>1.4</version>
</dependency>

If you omit version, it will use the newest version available.
Now to you can use simple commands to build your application:

mvn compile

Only compiles the source code.

mvn install

Compiles and packages your project then installs it in your local repository.

mvn war:war

Builds a war file of your application, including everything from src/main/webapp and your packaged project.

Creating a web site for your project is really easy, you can use an xml format (xdoc), that you can embed html into, or you can use a wiki like format (apt). You need a site.xml file, put it in src/site. Put your xml or apt files in src/site/xdoc and src/site/apt repectively. Here is an example site.xml and xdoc file:

<!-- site.xml -->
<project name="My Project">
<bannerLeft>
<name>banner</name>
<src>relative/path/to/image</src>
<href>http://my.home.page</href>
</bannerLeft>
<body>
<links>
<item name="orglink" href="http://my.org" target="_blank"/>
</links>
<menu name="About My Project">
<item name="Introduction" href="index.html"/>
<item name="Getting Started" href="getting-started.html"/>
</menu>
</body>
</project>

<!-- index.xml -->
<document>
<properties>
<title>My Project</title>
<sub-title>Welcome to My Project</sub-title>
<authors>
<person name="Brice Lambi" email="bricelambi@my.org"/>
</authors>
</properties>
<body>
<section name="Welcome to My Project">
<p>Maven is cool!</p>
</section>
</body>
</document>

You can build your site with the command:

mvn site:site

Check out the maven documentation for more info: http://maven.apache.org