To get anywhere reusing Clojure libraries, you’ll need to set up Leiningen. It is an abstraction on top of the Maven infrastructure used in Clojure. The big idea is that you declare the libraries and versions that you’ll need in a project.clj
file, and then it will go and get them for you.
In this chapter we assume the following:
You know how to add files to your path and grant permissions.
The benefits of Leiningen are similar to those of Maven. You will get a consistent project file structure and library dependency management. This means that other Leiningen projects will have a familiar layout and build process. It also means that you won’t have to manually download and add libraries to your project. Just declare the ones you want and those libraries (and their dependencies) will be downloaded and added to your Classpath.
So let’s get started. For the purpose of this example, we’ll assume you’re on Windows.
1. If you don’t have one already, create the following directory:
C:util
2. Next, add this directory to your path using the following steps:
a. Open up My Computer (Windows Key E).
b. Right-click Computer and select Properties.
c. Click Advanced System Settings.
d. Click Environment Variables.
e. Under System variables (not User variables) find the entry: Path. (If it doesn’t exist, create it.) Click Edit.
f. Onto the end of this row add the value C:util
(use a semicolon to delineate it from the previous value).
g. Click OK to close the editing of the Environment variable.
h. Click OK to close the Environment Variables window.
i. Click OK to close the System Properties window.
3. Download the following bat file and drop it into your c:util
directory.
https://raw.github.com/technomancy/leiningen/stable/bin/lein.bat
4. You will also need an application called wget.exe
. To get it, download the file leiningen-1.5.2-win.zip
(or a later version) from here:
https://github.com/technomancy/leiningen/downloads
5. Extract the wget.exe
from the zip file and drop it into your c:util
directory (which you have just put on the Path).
6. Open a new command prompt (cmd.exe
).
7. Confirm that you have Java running and on your path by running
java –version
You should expect output similar to
Java(TM) SE Runtime Environment (build 1.8.0_40-b27)
If you get an error, then you first need to install the Java JDK (Java Development Kit). You can get it from the Oracle website here:
http://www.oracle.com/technetwork/java/javase/downloads/index.html
8. Run the following command:
lein help
You should get the following output:
Leiningen is a tool for working with Clojure projects.
...
9. Now we’re going to create a new project in your projects directory. We’ll run the following commands:
cd projects
lein new app lein-test
10. Open the projects.clj
file that has just been created. It should look something like this:
(defproject lein-test "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.7.0-RC1"]]
:main ^:skip-aot lein-test.core
:target-path "target/%s"
:profiles {:uberjar {:aot :all}})
Note in particular the line (perhaps with a different version):
:dependencies [[org.clojure/clojure "1.7.0-RC1"]]
This is Leiningen loading libraries for you.
For this example we assume you are working on some kind of Mac.
1. Open a new command prompt by running Terminal.
2. Ensure that you have Java installed and on your path by running the following command:
java –version
You should expect a result similar to the following:
Java(TM) SE Runtime Environment (build 1.8.0_40-b27)
If you get an error, then you first need to install the Java JDK. You can get it from the Oracle website here:
http://www.oracle.com/technetwork/java/javase/downloads/index.html
3. Check that ~/bin
is on your path.
echo $PATH
You should expect the following in the results:
/Users/username/bin:
where username
is the name you log into your Mac with. For me this is Julian
.
4. Download the lein
command to your bin directory.
cd ~/bin
curl-L -O
https://raw.github.com/technomancy/leiningen/stable/bin/lein
chmod 755 lein
cd ~/
5. To test it is running, run the following command:
lein help
In the output, you should see the following:
Leiningen is a tool for working with Clojure projects.
...
6. Now we’re going to create a new project in your projects directory. We’ll run the following commands:
cd projects
lein new app lein-test
7. Open the projects.clj
file that has just been created. It should look something like this:
(defproject lein-test "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.7.0-RC1"]]
:main ^:skip-aot lein-test.core
:target-path "target/%s"
:profiles {:uberjar {:aot :all}})
Note in particular the line (perhaps with a different version):
:dependencies [[org.clojure/clojure "1.7.0-RC1"]]
This is Leiningen loading libraries for you.
8. Run the commands
cd lein-test
lein run
You should get the following result:
Hello, World!
We’ve set up Leiningen on your machine. From here you can work on projects at the REPL1 or build projects using Clojure’s build tool with the best bits of Maven.
1. REPL is an acronym for Read Eval Print Loop, which is a way to test Clojure code on the command line. For more information about the Clojure REPL, take a look at http://clojure.org/getting_started.
Following these steps will remove any roadblocks to getting Java compilation (and thus Clojure compilation) running on a Mac.
1. First determine whether the JRE is on your PATH. Open a new command prompt and enter the command:
java –version
You should expect a result similar to
java version "1.8.0_40"
Java(TM) SE Runtime Environment (build 1.8.0_40-b27)
(i.e., Java 8 or greater).
If you don’t get this result, you may need to install a JDK, or it may be installed and you simply have to add it to your Path
. To do so, take the following steps.
2. Determine if JAVA_HOME
is set and pointing to a JDK by running the following on a command prompt:
echo $JAVA_HOME
You should expect a result like:
/Library/Java/JavaVirtualMachines/jdk1.8.0_40.jdk/Contents/Home
If you got something similar to this (i.e., pointing to a JDK and version at least Java 8), you’re good to go. Otherwise, you’ll need to follow the remaining steps.
3. Confirm you’ve got a JDK installed by looking in the directory:
/Library/Java/JavaVirtualMachines/
In other words, open a new Finder window and select Go [Menu] -> Go to Folder.
4. Now ensure you have a folder with a name similar to jdk1.8.0_40.jdk
. (i.e., containing a JDK and with a Java version greater than 8). If not, you’ll need to download the JDK from here:
http://www.oracle.com/technetwork/java/javase/downloads/
5. Now you need to check that java
and javac
are on your path. (This is quite different from how Windows does it.) To do this, run the following on your command prompt:
which java
which should return the following result:
/usr/bin/java
6. Now run the command
ls -al /usr/bin/java
which should return a result similar to
lrwxr-xr-x 1 root wheel 74 21 Oct 2014 /usr/bin/java -> /System/Library/
Frameworks/JavaVM.framework/Versions/Current/Commands/java
What we see is an artifact of the way that the Mac versions Java. If this is not shown, then you’ll need to reinstall your JDK.
Repeat this process for javac
.
There are lots of traps you can fall into when setting up a JDK on a Windows machine. These steps are designed to alleviate that risk.
1. Check that you have Java installed by running
java -version
on the command prompt. This should give you a result similar to the following:
java version "1.8.0_45"
Java(TM) SE Runtime Environment (build 1.8.0_45-b15)
Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)
You should expect Java 1.8 or higher. If not, you’ll need to install an upgrade.
2. Ensure that you have a JDK installed by going to
C:Program FilesJava
and ensuring you have a directory similar to
jdk1.8.0_45
or higher. And if it is in C:Program Files (x86)
, that’s OK too, but you’ll need to ensure you have the 32-bit version of Tomcat installed as well. If you don’t have it, then you’ll need to download it from the Sun website at
http://www.oracle.com/technetwork/java/javase/downloads/.
3. Check that you have your JAVA_HOME
set to your JDK by running on the command prompt:
SET JAVA_HOME
You should expect a result similar to
JAVA_HOME=C:Program FilesJavajdk1.8.0_45
Ensure that this contains the JDK and not the JRE; that is, it should not be similar to this:
JAVA_HOME=C:Program Files (x86)Javajre1.8.0_31
4. If this is not set to the JDK, then you’ll need to do the following:
a. Select My Computer/This PC by using the keyboard shortcut Windows-E, Right-click My Computer/This PC and select Properties, as shown in Figure 1.1.
b. Select Advanced system settings (see Figure 1.2).
c. Click the button Environment Variables (see Figure 1.3).
d. Under System variables navigate to the entry JAVA_HOME
and click Edit (if it doesn’t exist, click New).
e. Under the Variable Value (Figure 1.4), change the path to the path of your JDK (if it is incorrect or doesn’t exist), for example,
C:Program FilesJavajdk1.8.0_45
(don’t forget the backslash on the end) and click OK.
f. Also under System variables, under the PATH
entry, click Edit.
g. Under Variable value (Figure 1.5), see if there is an entry for
;%JAVA_HOME%in;
If this doesn’t exist, add it and click OK.
h. Click OK to close Environment Variables.
i. Click OK to close System Properties.
j. Close the System window.
k. In a new command prompt (it won’t work in the old one), run the command
set JAVA_HOME
Now you should see a result pointing to your JDK directory similar to
13.59.209.131