So… I’ve gotten interested in Clojure and, since I’m a native Eclipse/Maven user, my first instinct was to try out Clojure from within my old familiar environment…. Thinks I to myself, “That shouldn’t be too hard since Clojure runs on the JVM.” Luckily for me, it turns out to be very easy to get clojure-dev (the Eclipse Clojure plugin) working with m2eclipse (the Eclipse Maven plugin I use).

First, I installed the Eclipse Clojure plugin. Installation was easy thanks to the Eclipse update site that they provide. I should note that I already had the Eclipse Maven plugin installed, but that can be installed in the same way. Next, I had to install the clojure jars that come with the clojure-dev plugin into my local maven repository.

Maven provides a standard way of installing third party jars. First, I cd’ed into my Eclipse plugins directory (mine is at /opt/eclipse/plugins), then I typed:

mvn install:install-file -Dfile=clojure_1.0.0/clojure.jar -DgroupId=org.clojure -DartifactId=clojure-lang -Dversion=1.0 -Dpackaging=jar

The clojure jar is the one that comes with clojure-dev, version 0.0.36; later versions of the plugin might include different versions of clojure. Next, I wanted to install clojure’s contributed libraries, so I typed:

mvn install:install-file -Dfile=clojurecontrib_0.0.0.20090504_r756/clojure-contrib.jar -DgroupId=org.clojure -DartifactId=clojure-contrib -Dversion=0.0.0.20090504_r756 -Dpackaging=jar

Like the main jar, the name of the contributed jar (and its parent directory) will most definitely change over time. So, with this done, I should be ready to go.

All I needed to do then was to modify my pom.xml to use the clojure jars (and any other jars I might want to use).  I added a dependencies section to my pom.xml:

<dependencies>
<dependency>
<groupId>org.clojure</groupId>
<artifactId>clojure-lang</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.clojure</groupId>
<artifactId>clojure-contrib</artifactId>
<version>0.0.0.20090504_r756</version>
</dependency>
<dependency>
<groupId>org.ccil.cowan.tagsoup</groupId>
<artifactId>tagsoup</artifactId>
<version>0.9.7</version>
</dependency>
</dependencies>

Then I created .clj file with a simple test:

(ns info.freelibrary.test)
(. (new org.ccil.cowan.tagsoup.Parser) (toString))

It worked! So, the next step was to see if adding a jar with just .clj files would work (I had no reason to think it wouldn’t). I downloaded enlive and then jar’ed up the contents of the src directory.

The next step was to add that new jar (enlive.jar) to the local maven repository which I did with:

mvn install:install-file -Dfile=enlive.jar -DgroupId=net.cgrand.enlive-html -DartifactId=enlive-html -Dversion=1.0-SNAPSHOT -Dpackaging=jar

Then I added a new dependency to my pom.xml by including:

<dependency>
<groupId>net.cgrand.enlive-html</groupId>
<artifactId>enlive-html</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>

Next, to test that everything was working, I modified my .clj file to:

(ns info.freelibrary.test
(:refer-clojure :exclude [empty complement])
(:use net.cgrand.enlive-html)
)
(select
(html-resource
(java.net.URL. "http://library.appstate.edu/")
) [:#main [:a (attr? :href)]]
)

And I ran it from within Eclipse using Ctrl-L. It ran and gave me valid output. Hurray! So, that’s it… I can now continue experimenting with Clojure using Eclipse and the Maven plugin.

Caveat: Though the above worked, the tagsoup dependency for enlive is really 1.2 (but there is no tagsoup-1.2 in the maven repository). I used an older version for testing but you can, of course, add the 1.2 version to your local maven repository in the same way you added the clojure jars (above).