Ruby script to install JFace JARs into your local Maven repository

July 16, 2009

After figuring out how to use Maven to write SWT applications, I figured it was time to tackle JFace.

If you read through the JFace wiki page above, though, it says that to use JFace + SWT outside of Eclipse we basically have to locate several dependencies, namely:

  • org.eclipse.core.commands_<version info>.jar
  • org.eclipse.equinox.common_<version info>.jar
  • org.eclipse.jface_<version info>.jar
  • org.eclipse.osgi_<version info>.jar
  • org.eclipse.ui.workbench_<version info>.jar

All of these are available in the standard installation of Eclipse IDE in the ${ECLIPSE_HOME}/plugins directory.

Now, it would be a 10 minute job to manually search for, copy and paste the actual JAR filenames and execute the Maven commands to install those into the local repository with proper group and artifact ids.

Since I’m ‘lazy’, though, instead of 10 minutes I decided to spend a couple of hours writing a Ruby script to do this all for me (I originally started to write it in bash but I realized it’d be a lot easier in Ruby).

Here it is. Save this as mvn_install_jface.rb somewhere, and make it executable. To use the script, simply go:

mvn_install_jface.rb ${ECLIPSE_HOME}

Obviously, replace ${ECLIPSE_HOME} with the directory where you installed Eclipse into.

If you don’t supply the directory, the script defaults to /Applications/eclipse which is where Eclipse would normally be installed in OS X.

#!/usr/bin/env ruby

require ‘find’

# Some utility code. Skip to # MAIN for main logic

CHECK_MESSAGES={
  :exists => ‘not found’,
  :file => ‘is not a file’,
  :directory => ‘is not a directory’,
  :readable => ‘is not readable’,
  :executable => ‘is not executable’
}

def assert_path(path, *checks)
  checks.each { |check|
    unless eval("File.#{check}?(path)")
      puts "\"#{path}\" #{CHECK_MESSAGES[check]}!"
      puts "\a"
      exit 1
    end
  }
end

# MAIN

# First we do our environmental checks

# Determine Eclipse installation directory parameter
ECLIPSE_HOME=ARGV[0] || File.join(‘/’, ‘Applications’, ‘eclipse’)
assert_path ECLIPSE_HOME, :exists, :directory, :readable
puts "Using Eclipse home: \"#{ECLIPSE_HOME}\""

# Now for the plugins directory under ECLIPSE_HOME
PLUGINS_DIR=File.join(ECLIPSE_HOME, ‘plugins’)
assert_path PLUGINS_DIR, :exists, :directory, :readable

# Next, we look for the Maven executable
MVN=`which mvn`.chomp # chomp to get rid of trailing ‘\n’
assert_path MVN, :exists, :file, :executable
puts "Using Maven: \"#{MVN}\""

puts

# The libraries we’re interested in
LIBS="org.eclipse.core.commands \
org.eclipse.equinox.common \
org.eclipse.jface \
org.eclipse.osgi \
org.eclipse.ui.workbench \
org.eclipse.jface.databinding \
org.eclipse.jface.text \
org.eclipse.jface"
.split

# As filename glob patterns
globs=LIBS.inject({}) {|map, lib| map[lib] = File.join(PLUGINS_DIR, "#{lib}_*.jar"); map}

found_jars={}

Find.find(PLUGINS_DIR) { |path|
  globs.delete_if { |lib, glob|
    if File.fnmatch?(glob, path)
      assert_path path, :file, :readable
      found_jars[lib] = path
    end
  }
}

unless globs.empty?
  puts "Couldn’t find any JARS for the following libraries:"
  globs.each_pair {|lib, glob| puts "#{lib} => #{glob}"}
  puts "\a"
  exit 2
end

# Utility struct just to make things a little tidier
Dependency = Struct.new :groupId, :artifactId, :version

DEPS=LIBS.map { |lib|
  parts=lib.split(‘.’)
  artifactId=parts.pop
  groupId=parts.join(‘.’)
  file = found_jars[lib]
  version=File.basename(file) [/^.+_(.+)\.jar$/, 1]
  command="#{MVN} install:install-file \
    -DgroupId=#{groupId} \
    -DartifactId=#{artifactId} \
    -Dversion=#{version} \
    -Dpackaging=jar \
    -Dfile=#{file}"
.split.map {|s| s.strip}
  system *command
  Dependency.new(groupId, artifactId, version)
}

puts
puts ‘Include the following dependencies in your pom.xml:’

DEPS.each { |dep|
  puts "    <dependency>"
  puts "      <groupId>#{dep.groupId}</groupId>"
  puts "      <artifactId>#{dep.artifactId}</artifactId>"
  puts "      <version>#{dep.version}</version>"
  puts "    </dependency>"
}

# kthxbye
 

First (after the environmental/sanity checks), the script tries to search for all the JFace JARs and will exit with an error if it can’t find any of the JARs we need.

The script will run all the necessary mvn install:install-file ... commands and figure out/supply the appropriate groupId, artifactId and version for all the JARs.

Finally, it’ll helpfully print out a bunch of <dependency>...</dependency> snippets you can copy & paste directly into your Maven pom.xml to use the found JFace JARs immediately. You’re welcome. :)

I’ve only tried this on OS X, and would love to hear from people who’ve tried it and gotten it work on other operating systems, especially Windows. I figure under Windows, if you already have Ruby and Maven on the path the command should be something like:

ruby mvn_install_jface.rb C:\eclipse

But I don’t have a Windows machine with Ruby, Java, Eclipse and Maven handy right now to test it myself.

Added 2009.07.24: I just realized the `which mvn` part won’t work at all under Windows, so Windows users will have to edit that part out—just make sure mvn is on the path.

(Oh and legalese blah blah MIT License.)

About these ads

One Response to “Ruby script to install JFace JARs into your local Maven repository”

  1. tomaszn Says:

    It works in Debian 6 :)


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: