LoginSignup
26
11

More than 5 years have passed since last update.

ClojureのビルドツールLeiningenに入門してみた

Posted at

Clojure使ってるわけじゃないんですが、なんとなくLeiningenについて調べて入門してきましたメモです。

ClojureとLeiningen

Clojureは、LISP系JVM言語の中で最もメジャーな言語です。
LeiningenはそのClojureのビルドツールです。

的確な説明ができるほど詳しくないので、説明はリンクに任せます。

参考リンク

Leiningen
http://leiningen.org/

Leiningen (software) - Wikipedia, the free encyclopedia
https://en.wikipedia.org/wiki/Leiningen_(software)

Clojure をはじめる (1): Clojure と Leiningen のインストール - tnoda-clojure
http://tnoda-clojure.tumblr.com/post/111489802935/getting-started-with-clojure-1-installing-clojure-and-le

Leiningen入門(再) - CLOVER
http://d.hatena.ne.jp/Kazuhira/20130820/1377000175

Leiningen で Clojure と Java の混在プロジェクト - tnoda-clojure
http://tnoda-clojure.tumblr.com/post/42289912282/leiningen-%E3%81%A7-clojure-%E3%81%A8-java-%E3%81%AE%E6%B7%B7%E5%9C%A8%E3%83%97%E3%83%AD%E3%82%B8%E3%82%A7%E3%82%AF%E3%83%88

実行環境

ここでは以下の環境を使っています。

  • Windows 7 (professional 64bit)
  • Leiningen 2.6.1

インストール

MacならHomebrewでbrew install leiningen
パッケージマネージャーを使わない場合は、Unixライク環境であればleinスクリプトファイルを、Windowsなら、lein.batを、それぞれダウンロードします。
スクリプトファイルは、ダウンロードしたらパスの通っているディレクトリーに置きます。

以降、Windowsの場合。

何もインストールされていない状態なら、self-installを試せと言われるので、それを実行。
インストール後、バージョンを確認。

C:\>lein self-install
Downloading Leiningen now...

C:\>lein --version
Leiningen 2.6.1 on Java 1.8.0_73 Java HotSpot(TM) 64-Bit Server VM
C:\>

続けて新規プロジェクト作成。

C:\>lein new clojure1
Generating a project called clojure1 based on the 'default' template.
The default template is intended for library projects, not applications.
To see other templates (app, plugin, etc), try `lein help new`.
C:\>

デフォルトテンプレートに従ってプロジェクトが作成されました。

ディレクトリー構成がMaven方式ではないんですね。
$project_root/src/clojure1/*.cljのようになっています。

プロジェクトルートに作成されるproject.cljがプロジェクトファイルのようです。
また、clojure1/src/clojure1/ディレクトリーに、core.cljという"Hello, world"コードのファイルが作られています。

  • core.cljファイル
(ns clojure1.core)

(defn foo
  "I don't do a whole lot."
  [x]
  (println x "Hello, World!"))

プログラムを実行 (Hello, World)

さっそくcore.cljを実行してみます。

C:\>cd clojure1
C:\clojure1>lein run
No :main namespace specified in project.clj.
C:\clojure1>

あれ、実行できませんでした。
project.cljmain名前空間を設定する必要があるようです。

設定せずに実行する場合は、-mオプションを使えば良さそう。
もうひとつ、core.clj内のfoo関数を-mainに変える必要があるみたいです。
それと、実行時に引数が無いとランタイムエラーになります。

C:\clojure1>lein run -m clojure1.core argius
 (省略 ... dependencies関連のメッセージ )
argius Hello, World!
C:\clojure1>

実行できました。

ライブラリーを使う

デフォルトでは、$HOME/.m2がローカルリポジトリーとして使われるみたいです。

依存関係を追加するには、project.clj:dependenciesに設定します。

今回は、Joda-Time 2.9.2で試しました。

Maven Repository: joda-time » joda-time » 2.9.2
http://mvnrepository.com/artifact/joda-time/joda-time/2.9.2

既に:dependencies [[org.clojure/clojure "1.8.0"]]と設定されていますので、そこに追加します。

  • project.cljのdependenciesにJoda-Time 2.9.2 を追加
(defproject clojure1 "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.8.0"] [joda-time/joda-time "2.9.2"]])

呼び出してみましょう。

  • core.cljファイル
(ns clojure1.core
    (:import org.joda.time.DateTime))

(defn -main
  []
  (println (DateTime/now)))
  • 実行
C:\clojure1>lein run -m clojure1.core
Retrieving joda-time/joda-time/2.9.2/joda-time-2.9.2.pom from central
Retrieving joda-time/joda-time/2.9.2/joda-time-2.9.2.jar from central
#object[org.joda.time.DateTime 0x5b247367 2016-02-28T16:04:06.709+09:00]
C:\clojure1>

上手く行きました。

Javaソースコードを混ぜる

Javaのソースコードファイルを混ぜてみることはできるでしょうか。
リンク5の記事によれば、project.clj:java-source-pathsを設定すればコンパイルしてくれるようです。

Foo.javaファイルを、core.cljファイルと同じディレクトリーに置きます。

  • Foo.java
package clojure1;

public final class Foo {
    public static String bar() { return "baz"; }
}
  • core.cljファイル
(ns clojure1.core
    (:import clojure1.Foo))

(defn -main
  []
  (println (Foo/bar)))

実行してみましょう。

C:\clojure1>lein run -m clojure1.core
Compiling 1 source files to C:\clojure1\target\classes
baz
C:\clojure1>

上手く行ったようです。

感想

Windowsでもコマンドがバッチリ使えますし、とても良い感じです。
あとはエディターを揃えれば、バリバリClojureが使えそうです。

26
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
26
11