创建测试工程:
F:\lein-test>lein new compojure lein-test-project
Retrieving compojure/lein-template/0.4.7/lein-template-0.4.7.pom from clojars
Retrieving compojure/lein-template/0.4.7/lein-template-0.4.7.jar from clojars
F:\lein-test>
此时,你需要privoxy + shadowsocks组合,并添加两个环境变量:
HTTP_PROXY 127.0.0.1:8118
HTTP_PROXYS 127.0.0.1:8118
这两个变量增加后,有可能导致部分软件联网失败(比如京东读书,增加两个变量,京东读书将无法联网,需要将这两个变量改名或删除后,就可以正常登陆了)。
privoxy的详细配置可以查看官方文档,这里简单介绍上述使用到的配置方法:
打开privoxy,在主页上依次选择Options-->Edit Main Configuration,将打开一个config.txt文件,在该文件的第780行(我使用的版本是的)下方加入配置:
listen-address 127.0.0.1:8118
最好重启下privoxy,不重启不知道生不生效。
shadowsocks是用来翻墙用的,至于怎么搭建,可以自行百度,因为leiningen中默认的仓库是Apache中央仓库,且有部分地址是国外的,如果不搭建该环境,国内可能搞不了。至于怎么修改leiningen中中央仓库的地址,目前还没找到方法。估计需要反编译安装lein时下载的JAR文件,修改地址后重新编译。
回来,创建测试工程成功后,进入工程目录下,建议在project.clj文件中增加一个配置:
:local-repo "F:\\ClojureRepos"
修改后的配置文件长这个样子:
(defproject lein-test-project "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:min-lein-version "2.0.0"
:dependencies [[org.clojure/clojure "1.10.0"]
[compojure "1.6.1"]
[ring/ring-defaults "0.3.2"]]
:plugins [[lein-ring "0.12.5"]]
:ring {:handler lein-test-project.handler/app}
:profiles
{:dev {:dependencies [[javax.servlet/servlet-api "2.5"]
[ring/ring-mock "0.3.2"]]}}
:local-repo "F:\\ClojureRepos"
)
:local-repo的作用和maven的local-repository是同样的,防止你的C盘占用变的很高。
在工程目录下,启用REPL(随机端口启动):
F:\lein-test\lein-test-project>lein repl
nREPL server started on port 52276 on host 127.0.0.1 - nrepl://127.0.0.1:52276
REPL-y 0.4.3, nREPL 0.6.0
Clojure 1.10.0
Java HotSpot(TM) 64-Bit Server VM 1.8.0_212-b10
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
user=>
在这行
nREPL server started on port 52276 on host 127.0.0.1 - nrepl://127.0.0.1:52276
将会卡一会,是在加载东西还是干嘛的不太清楚,如果卡的时间长,可按Enter键试试,有可能会直接显示下面的信息(我等了5分钟吧,还不出来,就按了两下Enter)。
可以使用doc查看某个函数的介绍,source查看源码(说实话,实际开发中用不到,因为用的是IDE,可以直接跳到源码):
user=> (doc assoc)
-------------------------
clojure.core/assoc
([map key val] [map key val & kvs])
assoc[iate]. When applied to a map, returns a new map of the
same (hashed/sorted) type, that contains the mapping of key(s) to
val(s). When applied to a vector, returns a new vector that
contains val at index. Note - index must be <= (count vector).
nil
user=> (source assoc)
(def
^{:arglists '([map key val] [map key val & kvs])
:doc "assoc[iate]. When applied to a map, returns a new map of the
same (hashed/sorted) type, that contains the mapping of key(s) to
val(s). When applied to a vector, returns a new vector that
contains val at index. Note - index must be <= (count vector)."
:added "1.0"
:static true}
assoc
(fn ^:static assoc
([map key val] (clojure.lang.RT/assoc map key val))
([map key val & kvs]
(let [ret (clojure.lang.RT/assoc map key val)]
(if kvs
(if (next kvs)
(recur ret (first kvs) (second kvs) (nnext kvs))
(throw (IllegalArgumentException.
"assoc expects even number of arguments after map/vector, found odd number")))
ret)))))
nil
user=>
退出REPL的命令是exit:
user=> exit
Bye for now!
F:\lein-test\lein-test-project>
下面,看下指定端口启动一个REPL:
F:\lein-test\lein-test-project>lein repl :start :host localhost :port 60000
nREPL server started on port 60000 on host localhost - nrepl://localhost:60000
REPL-y 0.4.3, nREPL 0.6.0
Clojure 1.10.0
Java HotSpot(TM) 64-Bit Server VM 1.8.0_212-b10
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
user=>
另一个终端连接到这个REPL:
F:\lein-test\lein-test-project>lein repl :connect localhost:60000
Connecting to nREPL at localhost:60000
REPL-y 0.4.3, nREPL 0.6.0
Clojure 1.10.0
Java HotSpot(TM) 64-Bit Server VM 1.8.0_212-b10
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
user=>
我们看到,上述启用REPL,最后都有一个命令交互行,即有一个客户端等待命令的输入,我们可以启用一个没有客户端的REPL:
F:\lein-test\lein-test-project>lein repl :headless :host localhost :port 60000
nREPL server started on port 60000 on host localhost - nrepl://localhost:60000
这样就不会启动带有客户端的REPL了。
连接这个REPL,和上面一样:
F:\lein-test\lein-test-project>lein repl :connect localhost:60000
Connecting to nREPL at localhost:60000
REPL-y 0.4.3, nREPL 0.6.0
Clojure 1.10.0
Java HotSpot(TM) 64-Bit Server VM 1.8.0_212-b10
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
user=>
运行应用的命令是:
F:\lein-test\lein-test-project>lein ring server
2019-06-28 15:43:49.755:INFO::main: Logging initialized @2145ms
2019-06-28 15:43:52.505:INFO:oejs.Server:main: jetty-9.2.21.v20170120
2019-06-28 15:43:52.534:INFO:oejs.ServerConnector:main: Started ServerConnector@10466c55{HTTP/1.1}{0.0.0.0:3000}
2019-06-28 15:43:52.535:INFO:oejs.Server:main: Started @4924ms
Started server on port 3000
这样将在3000端口启动应用,通过浏览器访问地址http://localhost:3000,将会打开工程主页Hello World。
lein ring server启动应用后会自动打开默认浏览器,lein ring server-headless则不会打开浏览器。
通过配置project.clj文件,让应用启动时同时启动nREPL(n是网络的意思):
(defproject lein-test-project "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:min-lein-version "2.0.0"
:dependencies [[org.clojure/clojure "1.10.0"]
[compojure "1.6.1"]
[ring/ring-defaults "0.3.2"]]
:plugins [[lein-ring "0.12.5"]]
:ring {:handler lein-test-project.handler/app
:nrepl {:start? true
:port 60000
}
}
:profiles
{:dev {:dependencies [[javax.servlet/servlet-api "2.5"]
[ring/ring-mock "0.3.2"]]}}
:local-repo "F:\\ClojureRepos"
)
启动应用:
F:\lein-test\lein-test-project>lein ring server
2019-06-28 15:51:22.191:INFO::main: Logging initialized @2104ms
Started nREPL server on port 60000
2019-06-28 15:51:25.585:INFO:oejs.Server:main: jetty-9.2.21.v20170120
2019-06-28 15:51:25.615:INFO:oejs.ServerConnector:main: Started ServerConnector@11d0bff5{HTTP/1.1}{0.0.0.0:3000}
2019-06-28 15:51:25.619:INFO:oejs.Server:main: Started @5528ms
Started server on port 3000
应用启动后,我们可以启用REPL连接到这个应用(报的错忽略):
F:\lein-test\lein-test-project>lein repl :connect localhost:60000
Connecting to nREPL at localhost:60000
Syntax error (ClassNotFoundException) compiling at (form-init3215558098689658402.clj:1:82).
nrepl.core
#object[clojure.lang.Namespace 0x380c4221 "user"]
Error loading namespace; falling back to user
nil
user=>