Thursday, December 1, 2011

Making Eclipse, CounterClockWise and Leiningen Sing on Windows

Download the Leiningen batch file for Windows to get started. You'll likely need to edit the file to set the path for where you are at.

set LEIN_HOME=c:\dev\leiningen

Next you need to run the Leiningen installer:

lein.bat self-install

If you are behind a firewall you can download the standalone jar file, place it in the 'self-installs' directory. That way you won't need to mess with curl or wget.

The rest of Leiningen uses maven, so this is how you get around an http proxy with maven. Create a setting.xml file in your user's home directory within a folder called .m2.

C:\Users\joe_user\.m2\settings.xml:


<settings>
  <proxies>
    <proxy>
      <id>default</id>
      <active>true</active>
      <protocol>http</protocol>
      <host>PROXY_HOST</host>
      <port>PROXY_PORT</port>
 <username>YOUR_PROXY_USERNAME</username>
      <password>YOUR_PROXY_PASSWORD</password>
      <nonProxyHosts>localhost</nonProxyHosts>
    </proxy>
  </proxies>
</settings>

At this point I'd recommend testing things out from the command line.

Test line.bat to ensure you get the help menu.

Create a new project using lein.bat new.

Pull in dependency's for the project by going into the project directory and running lein.bat deps.
This will test the maven proxy configuration.

Once you have Leiningen setup and installed it's time to setup eclipse. I'm going to refer to the labrepl wiki instructions for setting up Eclipse with counterclockwise. You don't have to go all the way through the instructions since we aren't setting up labrepl, but it wouldn't hurt to install the Git and Maven plugins as well as CounterClockWise, which are all discussed.

Great, now I'm going to refer you to the sexp.posterous blog, which has simple instructions to create the external calls to the Leiningen script from Eclipse. These can then be used by all of your Clojure projects and should only take you about 5 minutes to setup.

Now you have your basic Eclipse-Clojure-Leiningen environment setup and I'll cover general use and some gotchas that I ran into.

The first thing you want to do is create a new Eclipse project. Select the 'Clojure' type for your project. If you don't see this option, then perhaps you didn't get the CounterClockWise plug-in properly installed. It should look like this:

New Project Wizard

We will now run some of the Leiningen external tools you setup earlier. If you have difficulty getting the commands to run, I've found I sometimes have to click on the project name before I run the command, as I'm showing in this pic:

External Tools Drop-Down

Run the Leiningen 'new' command first. This will add some files and folders to the existing Eclipse project. (You may need to refresh the project before they appear.) The files of interest are the project.clj file in the project root and the core.clj files under /src and /test in your project's namespace, which in my case is helloWorld - the same name as the project. It get's this name, based on how we setup the Leiningen 'new' command. You can think of the Clojure namespace being similar to a Java package.

Your setup should now look something like this view from the Eclipse package manager:

Package Manager View

Next you will run the Leiningen 'deps' command from the external tools drop-down. This will look at the project.clj file and download any necessary dependencies and put them into the /lib folder. Leiningen uses  Maven to do this, which is why I had to add the settings.xml file to Maven to get through my http proxy. You will not need to do this if you don't have to deal with a proxy.

Now let's add the test folder that was created by the Leiningen 'new' command to the project's source build path in Eclipse:

Java Build Path Window

You'll notice that after running the 'dep' command you have a build error on your project. This is because 'dep' wipes out the /classes folder. 

Run the Leiningen 'compile' command and it will be restored. You may need to refresh your project after you run compile. 

FYI: Just so you know when you are in the package manage view, if there ever is a folder that you know exists, which you cannot see, it is likely because the folder is configured as a build path library object for your project.

Now let's modify the default code and create a simple hello world program which we want to be able to package and run outside of Eclipse. To do this you will modify the src/core.clj and project.clj files.

Aside from the hello world function, you will also need to add the :gen-class specification to your core.clj file in order to create runnable java class files. Note that we have given the function the name of -main, which is the same as the main method for a Java program.

(ns helloWorld.core
(:gen-class))

(defn -main [msg]
(println "Hello" msg))

Now on to the project.clj file, which lists dependencies and also needs to point to the main method.

Note the :manifest specification in the project.clj file shown below. The manifest does not need to be specified if you are going to only use the standalone jar using Leiningen's 'uberjar' tool, since clojure is included with standalone jar. If however, you wanted to have several projects deployed somewhere which reference only one instance of the clojure library to save space, you can specify where your libraries live using :manifest.

(defproject helloWorld "1.0.0-SNAPSHOT"
:description "FIXME: write description"
:dependencies [[org.clojure/clojure "1.3.0"]]
:main helloWorld.core
:manifest {"Class-Path" "lib/clojure-1.3.0.jar"})

Now run the 'uberjar' command, which will create 2 jar files of your project. One which includes the clojure library, it will have STANDALONE in its name, and another jar which merely references your clojure library, which was specified using :manifest.

You should be able to run your project like so:


c:\UL\workspace-helios32\helloWorld>java -jar helloWorld-1.0.0-SNAPSHOT-standalone.jar Joshua
Hello Joshua


If you don't give this program any parameters it will throw the following "arity" exception. Maybe more about that another time... Enjoy your new environment!


c:\UL\workspace-helios32\helloWorld>java -jar helloWorld-1.0.0-SNAPSHOT-standalone.jar
Exception in thread "main" clojure.lang.ArityException: Wrong number of args (0)
 passed to: core$-main
        at clojure.lang.AFn.throwArity(AFn.java:437)
        at clojure.lang.AFn.invoke(AFn.java:35)
        at clojure.lang.AFn.applyToHelper(AFn.java:159)
        at clojure.lang.AFn.applyTo(AFn.java:151)
        at helloWorld.core.main(Unknown Source)

3 comments:

  1. Very helpful! Thanks for writing it up!

    ReplyDelete
  2. Thank you for taking the time to provide us with your valuable information. We strive to provide our candidates with excellent care and we take your comments to heart.As always, we appreciate your confidence and trust in us.

    SAP training in Chennai

    ReplyDelete
  3. I have read your blog its very attractive and impressive. I like it your blog.

    Java Training in Chennai Core Java Training in Chennai Core Java Training in Chennai

    Java Online Training Java Training in Chennai Core Java 8 Training in Chennai Core Java 8 Training in Chennai JavaEE Training in Chennai Java EE Training in Chennai

    ReplyDelete