Leiningen
Leiningen是一个非常便利的clojure工程构建工具。它主要提供的功能有:
创建工程
为工程获取依赖库
run tests
编译java文件
运行整个工程
生成pom文件
运行配置过的REPL
为部署编译和打包
发布libraries到仓库
运行定制任务
获得帮助
运行lein help
会列出所有lein的子命令,lein help $TASK
会显示子命令的详细说明。
创建一个工程
创建一个工程师非常简单的:
$ lein new app my-app
Generating a project called my-app based on the 'app' template.
$ cd my-app/
$ tree
.
├── CHANGELOG.md
├── LICENSE
├── README.md
├── doc
│ └── intro.md
├── project.clj #工程描述文件
├── resources
├── src #代码文件夹
│ └── my_app
│ └── core.clj
└── test #测试代码文件夹
└── my_app
└── core_test.clj
这个例子用的是app模板,如果不写 app
会用缺省的libraries模板创建工程。
添加依赖库
给工程添加依赖库非常简单,假设我们想给工程添加version为3.9.1的 clj-http依赖库。
打开工程描述文件project.clj:
1 (defproject my-app "0.1.0-SNAPSHOT"
2 :description "FIXME: write description"
3 :url "http://example.com/FIXME"
4 :license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0"
5 :url "https://www.eclipse.org/legal/epl-2.0/"}
6 :dependencies [[org.clojure/clojure "1.10.0"]]
7 :main ^:skip-aot my-app.core
8 :target-path "target/%s"
9 :profiles {:uberjar {:aot :all}})
更改第6行:
:dependencies [[org.clojure/clojure "1.10.0"]]
为
:dependencies [[org.clojure/clojure "1.8.0"]
[clj-http "3.9.1"]]
然后运行:
这样最新的库就会下载到classpath中。
有的库有"artifact id
和group ids
,可以写成下面的形式
``` clojure
[group ids/artifact id, "version"]
比如
[fr.jetoile.hadoop/redis "3.2"]
运行代码
在上面创建的my-app文件夹下执行:
$ lein repl
nREPL server started on port 54203 on host 127.0.0.1 - nrepl://127.0.0.1:54203
REPL-y 0.4.3, nREPL 0.6.0
Clojure 1.10.0
Java HotSpot™ 64-Bit Server VM 1.8.0_181-b13
Docs: (doc function-name-here)
(find-doc "part-of-name-here")
Source: (source function-name-here)
Javadoc: (javadoc java-object-or-class-here)
Exit: Control+D or (exit) or (quit)
Results: Stored in vars *1, *2, *3, an exception in *e
my-app.core=>
执行 -main函数
my-app.core=> (my-app.core/-main)
Hello, World!
nil
因为我们运行的是工程的入口函数-main,我们也可以直接运行 run
命令来调用-main函数:
$ lein run
Hello, World!
打包
运行打包命令:
$ lein uberjar
Compiling my-app.core
Created ~/my-app/target/uberjar/my-app-0.1.0-SNAPSHOT.jar
Created ~/my-app/target/uberjar/my-app-0.1.0-SNAPSHOT-standalone.jar
运行jar:
$ java -jar target/uberjar/my-app-0.1.0-SNAPSHOT-standalone.jar
Hello, World!