今回は Clojure 界での Maven 的存在、Leiningen を使って Clojure プログラミングの開発環境を整備する手始めをしていきます。 Leiningen は『ライニンゲン』と読むそうです(雷人間?)。 ロゴが雷親父っぽいのは偶然かな?

Linux 系(Mac 含む)の OS 上でのインストール方法はこちらにあるので、本記事では Windows 環境に限定。 Linux 系ではスクリプトを実行するだけで済むようですが、Windows 上ではもう一手間必要な感じです。 原因は、Leiningen は Maven などのように必要なライブラリを自動ダウンロードしてくれるツールですが、Leiningen 自体をダウンロードしてインストールしてくれるツールが Windows にはデフォルトで入っていないことのようです。 Windows 上に Leiningen をインストールするバッチファイルは配布されてますが、そのバッチファイルは Wget (もしくは curl.exe)によって Leiningen をダウンロード&インストールするので、Wget 自体のインストールは自分で行う必要があります。
一応、Windows のための zip アーカイブも用意されているようですが、バージョン 1.5.2 までしかなくて、 Leiningen の最新バージョン(現在 2.0.0-preview7)には対応していないようです(今後、そのような zip ファイルが用意されていくのかも不明)。 なので、ここでは Wget を自分でインストールする方法を見ていきます。 まぁ、大して難しくありませんヨ。
Leininngen インストール準備
まずは Leiningen をインストールする準備:
- Leiningen をインストールするフォルダ*1を作成する。 以下、このフォルダを LEIN_HOME とします。
- lein.bat を LEIN_HOME へダウンロードする。
- wget.exe を LEIN_HOME へダウンロードする。
- LEIN_HOME へのパスを環境変数 PATH に追加する。
lein.bat はLeiningen を起動する際に常に使うので、配置しているフォルダへのパスを PATH に追加しておいてください。 これで準備完了です。 Linux 系に比べて wget.exe をダウンロードする手間がありますが、軽いファイルなのですぐダウンロードできます。
Leiningen のインストール
これで Leiningen をインストールする準備が完了したので、インストールを実行しましょう。 これ以降はこちらに書いている Linux 系へのインストールと同じです。
好きなフォルダ上でコマンドライン(or Cygwin)を開いて以下のコマンドを実行してください:
lein self-install
これで Leiningen の Jar ファイルなどがダウンロードされて、Leiningen が使えるようになります。 Leiningen のインストールはこれだけです。 別途 Clojure のライブラリをダウンロードしたりする必要もありません(Leiningen の中に組み込まれてます)。 Oh! 簡単!
Leiningen を使う
Leiningen のインストールは以上で終了ですが、幾つか簡単な使用法を見ておきましょう。
- ヘルプ、使用可能なコマンド一覧
- REPL を起動する
- Clojure プロジェクトを作成する
ヘルプ、使用可能なコマンド一覧
ヘルプを表示させるには
lein help
を実行します。 そのままやん! 引数なしで実行しても表示されますがね。 インストールが成功していれば以下のような出力が表示されるはずです:
$ lein help Leiningen is a tool for working with Clojure projects. Several tasks are available: check Check syntax and warn on reflection. classpath Write the classpath of the current project to output-file. clean Remove all files from project's target-path. compile Compile Clojure source into .class files. deploy Build jar and deploy to remote repository. deps Show details about dependencies. do Higher-order task to perform other tasks in succession. help Display a list of tasks or help for a given task. install Install current project to the local repository. jar Package up all the project's files into a jar file. javac Compile Java source files. new Generate project scaffolding based on a template. plugin DEPRECATED. Please use the :user profile instead. pom Write a pom.xml file to disk for Maven interoperability. repl Start a repl session either with the current project or standalone. retest Run only the test namespaces which failed last time around. run Run the project's -main function. search Search remote maven repositories for matching jars. show-profilesList all available profiles or display one if given an argument. test Run the project's tests. trampoline Run a task without nesting the project's JVM inside Leiningen's. uberjar Package up the project files and all dependencies into a jar file. upgrade Upgrade Leiningen to specified version or latest stable. version Print version for Leiningen and the current JVM. with-profile Apply the given task with the profile(s) specified. Run lein help $TASK for details. See also: readme, faq, tutorial, news, sample, profiles, deploying and copying.
「lein help」の後にタスク名を続けて実行すると、そのタスクの詳細が得られるようですね。
REPL を起動する
Leiningen なら REPL の起動も簡単!
lein repl
と実行します。 そのままやん! しかも、この REPL は「exit」もしくは「quit」で終了できます!(こちらを参照)
Clojure プロジェクトを作成する
Leiningen で Clojure プロジェクトを作成するには
lein new 《プロジェクト名》
と実行します。 これも簡単ですね。 まぁ、生成されるプロジェクトの詳細については機会があれば。
おまけ : Jar ファイルから動かしてみる
ちなみに、Leiningen の Jar ファイルはこちらからダウンロードできるので、それを使って Leiningen を実行することもできます。 ただし、この Jar ファイルは実行可能ではないので、実行するのに少々長いコマンドが必要です。 java コマンドは使用できるとし、また環境変数 LEIN_HOME に Leiningen の Jar ファイルが配置されているフォルダへのパスが設定されているとして、新規に Clojure プロジェクトを作成するには
java -cp %LEIN_HOME%\leiningen-2.0.0-preview7-standalone.jar clojure.main -m leiningen.core.main new 《プロジェクト名》
というコマンドを実行します。 同様に、他のコマンドを使いたい場合は「leiningen.core.main」の後に引数として指定します。
参考
- Leiningen
- leiningen downloads page
- Wget for Windows
- Nyampass tech blog 『Clojure入門「とりあえず使ってみる」』
- Nyampass tech blog 『Clojure入門「プロジェクトを作る」』

- 作者: Stuart Halloway and Aaron Bedra,川合史朗
- 出版社/メーカー: オーム社
- 発売日: 2013/04/26
- メディア: 単行本(ソフトカバー)
- この商品を含むブログ (5件) を見る