Skip to content
ato edited this page Sep 13, 2010 · 36 revisions

Hi! Today I’ll walk you through creating and building a simple library with Leiningen and pushing it to Clojars.org. First up, install Leiningen:

cd ~/bin
wget http://github.com/technomancy/leiningen/raw/0.5.0/bin/lein
chmod +x lein
./lein self-install

Our example library is going to be called too-hot so lets create a directory for it.

mkdir -p too-hot/src

Next create a Leiningen project file called too-hot/project.clj using your text editor. This is where we specify the name and version of our project and list its dependencies.

This says that we want to use the latest version of Clojure and the lein-clojars plugin. If you need more dependencies for your project, try searching “Maven central”: http://mvnrepository.com/ for Java libs and Clojars for Clojure libraries, then just add them to your :dependencies list. The format is [groupId/artifactId "version"] and the groupId is optional if it’s the same as the artifactId.

Now ask Leiningen to fetch the dependencies for you, and put them in the project’s lib directory:

 lein deps

Now you can fire up your editor of choice and start working on the library. I use Emacs so hit M-x swank-clojure-project and select the too-hot project directory so that SLIME adds the jars in lib directory to the classpath.

As an Australian if I complain to an American friend about how it’s 40 degrees outside and way they usually just give me a puzzled look. So our example library is going to be a Celsius to Fahrenheit converter. Create a file too-hot/src/too_hot.clj:

Compile and load the file with C-c C-k in Emacs and check that it works in the REPL:

user> (fahrenheit 40)
104
user> (celsius 104)
40

Beaut, we’re finished. So lets now push the library to the Clojars repository so that other people can use it.

If you haven’t already, create an SSH public key and paste it into your Clojars profile. You can create a key using ssh-keygen -t rsa (or lein keygen from your project directory). Now you should be able to push your project:

lein push

Whoops, what happened?

Error: transaction rolled back: You don't have access to the too-hot group.

Here you see Clojars’ groups in action. When you push a jar a group is automatically created with you as it’s sole member. You can add members by goind to the group under “Your groups” on your dashboard and filling in a username to add. In this case I own the official too-hot group so you are unable to push to it.

If you want to push your own version of somebody else’s jar you’ll have to put it under your group, to show it’s your version and not the official one. To do this, just qualify the project name by putting org.clojars.username/ in front of it in your project.clj. So for example, if your Clojars username is ato change your project.clj to:

Now try pushing again:

lein push

Note that if you’re having trouble with the lein-clojars plugin you can also push manually just with scp:

lein pom
scp pom.xml too-hot.jar [email protected]:
Clone this wiki locally