14

In order to do some multi-platform GUI development, I have just switched from GTK + Clojure (because it looks like the Java bindings for GTK never got ported to Windows) to SWT + Clojure. So far, so good in that I have gotten an uberjar built for Linux.

The catch, though, is that I want to build an uberjar for Windows and I am trying to figure out a clean way to manage the project.clj file.

At first, I thought I would set the classpath to point to the SWT libraries and then build the uberjar. This would require that I set a classpath to the SWT libraries before running the jar, but I would likely need a launcher script, anyway. However, leiningen seems to ignore the classpath in this instance because it always reports that

Currently, project.clj looks like this for me:

(defproject alyra.mana-punk/character "1.0.0-SNAPSHOT"
  :description "FIXME: write"
  :dependencies [[org.clojure/clojure "1.2.0"]
                 [org.clojure/clojure-contrib "1.2.0"]
                 [org.eclipse/swt-gtk-linux-x86 "3.5.2"]]
  :main alyra.mana-punk.character.core)

The relevant line is the org.eclipse/swt-gtk-linux-x86 line. If I want to make an uberjar for Windows, I have to depend on org.eclipse/swt-win32-win32-x86, and another one for x86-64, and so on and so forth.

My current solution is to simply create a separate branch for each build environment with a different project.clj. This seems kinda like using a semi to deliver a single gallon of milk, but I am using bazaar for version control, so branching and repeated integrations are easy. Maybe the better way is to have a project.linux.clj, project.win32.clj, etc, but I do not see any way to tell leiningen which project descriptor to use.

What are other (preferably more elegant) ways to set up such an environment?

2
  • As pointed out by technomancy, my solution isn't really solving your problem in the end. I did not read your question correctly and I think you should unaccept my answer. Jan 19, 2011 at 20:57
  • Answering random questions is definitely not my cup of tea, I should stick to coding only! ;-) Jan 19, 2011 at 20:58

1 Answer 1

18

Here's a quite elegant solution using Java system properties:

(let [properties (select-keys (into {} (System/getProperties))
                              ["os.arch" "os.name"])
      platform (apply format "%s (%s)" (vals properties))
      swt (case platform
            "Windows XP (x86)" '[org.eclipse/swt-win32-win32-x86 "3.5.2"]
            "Linux (x86)"      '[org.eclipse/swt-gtk-linux-x86 "3.5.2"])]
  (defproject alyra.mana-punk/character "1.0.0-SNAPSHOT"
    :description "FIXME: write"
    :dependencies [[org.clojure/clojure "1.2.0"]
                   [org.clojure/clojure-contrib "1.2.0"]
                   ~swt]
    :main alyra.mana-punk.character.core))
7
  • Oh, that will definitely do the trick! Could you explain, though, the ~ in the line "~swt]"? I see that it unquotes in the macro, but I thought you only needed to quote and unquote in that style when writing a macro. Jan 14, 2011 at 16:23
  • That's because defproject is a macro and internally it properly evaluate unquoted expressions using the private unquote-project function: bit.ly/gOZriZ Jan 14, 2011 at 18:11
  • It looks like there's no reason the platform used to build the uberjar needs to be the same as the target platform. I'd recommend using an environment variable to specify the target platform instead.
    – user61051
    Jan 19, 2011 at 18:22
  • @technomancy That's a very valid point I didn't thought about, in fact it makes my solution only useful for development purpose, which is what I was thinking about! This make me wonder if you have plans to add a way of creating multiple jars using different dependencies? Jan 19, 2011 at 20:49
  • I'm not sure of that. SWT depends on platform-specific libraries and the uberjar process seems to demand being able to open (and fully compile?) those libraries. So, if I try to uberjar with swt-gtk-linux-x86 on a Windows system, I get this: "Exception in thread "main" java.lang.UnsatisfiedLinkError: no swt-gtk-3557 or swt-gtk in swt.library.path, java.library.path or the jar file". Jan 21, 2011 at 17:12

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.