Clojure in Eclipse, Part 2: Counterclockwise + Maven
March 18, 2012 at 19:24
Nico in Clojure, Clojure, Counterclockwise, Eclipse, Maven

This article explains how Eclipse, Counterclockwise and Maven come together to form a powerful Clojure development platform.

In Part 1: Maven, I explained how to develop Clojure applications with Eclipse and its build automation tool Maven. In particular:

As far as structured and monolithic compiled development goes, Maven is indeed a powerful one stop shop. But it needs something extra to satisfy our needs as Clojure developers.

This is precisely what Counterclockwise, the Clojure plugin for Eclipse, adds to the picture.

Let us install Counterclockwise first, and then go back to where we left at the end of our Maven tutorial. We will then observe the new features that Counterclockwise provides, delve into its productivity features, and look at how it eventually combines with Maven into a single powerful workflow.

Another build tool for Clojure is the famous Leiningen. It is written in Clojure and for Clojure, and many prefer it to Maven. At the time of writing, it is not as tightly integrated in Eclipse as Maven, although this should soon change. Which of Leiningen and Maven is best to use very much depends on your own individual circumstances, background and taste.

But in a nutshell, you should like Maven if you like a tool that is:

Installing Counterclockwise

Install Counterclockwise like any other Eclipse plugin, via Help > Install New Software... . Specify one of the following update sites:

At the time of writing, I am installing the 0.6 version (from the beta site).

Creating a new project

You can either create a new Maven project or create a new Clojure project and work from there. The two options are really equivalent, with an equal (small) amount of tidying up required. I prefer giving the bulkiest animal precedence, and to start with a Maven project.

So let me quickly review how to start with a Clojure project first. I will then restart where we left at the end of our Clojure and Maven tutorial, which began with a Maven project.

Mavenizing a Clojure project

to start with a Clojure project:

You will have to create extra directories for Java codes if you need them.

Clojurizing a Maven project

To start with a Maven project:

Counterclockwise's features

Counterclockwise's features are explained in its documentation. The first ones you get out of the box are:

But most of Counterclockwise's features come to life once you start a REPL. For Counterclockwise is really built around the REPL; without one running, things such as code completion will not work. So it helps to see the REPL as the hub from which all Counterclockwise functions emanate from. A corollary of this is that you must ensure that your REPL environment is loaded with:

Let us look at this properly.

I am restarting where we left at the end of Part1: Maven. You can catch up by downloading part one's tutorial project straight from GitHub.

In what follows, Use the following lines of Clojure to observe what is going on:

Starting a REPL from a Clojure file

Open maven.clj (In src/main/clojure, package chaomancy), which contains the main code of our application. From here:

REPL activated features

Now we can use Counterclockwise's REPL activated features:

Loading a file in the REPL

You can load a file in an existing REPL via the Clojure menu, instead of starting a new REPL. Let us move across namespaces in the REPL to illustrate how this works:

Do experiment with other commands in the Clojure menu at your own pace.

Shutting the REPL down

To close and stop the REPL:

Note that you can start several REPL side by side; they will each have their own Console process.

Counterclockwise productivity with Run Configurations

When you first start a REPL from a Clojure source file, Counterclockwise creates a permanent Run Configuration behind the scenes. You can customise Run Configurations to your own need. Let us look at this by example.

Scripts

Launching a script is done in exactly the same way as loading a namespace.

Now, a script usually performs a defined task, and you don't necessarily want a REPL to be started when you run one. You could load one in an existing REPL, but this may load namespaces and create symbols that will clutter your REPL. You might also want to make a script easily accesible via the GUI in order not to reopen the script's file everytime.

Let's show how to do this:

This will also work if you already have a REPL running, so it a great to run stuff without cluttering your development environment.

What we did here is a direct alternative to using the clojure:run goal in Maven.

Project REPL

Counterclockwise allows you to start a generic project REPL via the Package Explorer, and to initialise it as you want; this is precisely the purpose of the maven-repl.clj script.

You can now start this generic project REPL via the Package Explorer (you could add it to favorites too).

This is a direct alternative to using the clojure:repl goal in Maven. Counterclockwise is a much nicer REPL environment, although it won't launch lifecycle phases before starting the REPL as Maven did.

Configuration Wrap-up

The entire configuration path I recommend is the following:

Then:

From there, you can run/load .clj files from 5 IDE locations:

Maven + Counterclockwise

We can finally conclude on what to do with Counterclokwise an what to do with Maven:

In a more visual fashion:

Counterclockwise Maven
Phases Interactive Clojure coding:
  • REPLs
  • Scripts
Bilingual life-cycle phases:
  • Test *
  • Package **
  • Install *
Troubleshooting Interactive:
  • Main script
  • General REPL
Life-cycle bound:
  • Main script **
  • General REPL **
*,** click on the project in the Package Explorer and then:
    * built-in: under Run > Run As
    ** custom: under Run > Run As > Maven build

What Next?

There is little you cannot do with Eclipse, Counterclockwise and Maven. So what else?

Counterclockwise itself is in active development, and the stable release of version 0.6 should come out any day now. The best places to follow the action are:

Now, a very expected feature is the integration of Leiningen in Eclipse/Counterclockwise, which you can read about in this informal Leiningen spec by Laurent Petit. In the meanwhile, this Poor man's integrating of Leiningen into Counterclockwise explains how to call Leiningen functions from Eclipse.

Reasons to use Leiningen instead of Maven can range from personal preferences to constraints. In a recent post, I explained how I decided to give a shot at Heroku to host Clojure applications, and Heroku requires projects to be compatible with... Leiningen. So something tells me I will write about Leiningen soon.

Article originally appeared on Chaomancy (http://www.chaomancy.com/).
See website for complete article licensing information.