前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >[1084]windows搭建clojure开发环境

[1084]windows搭建clojure开发环境

作者头像
周小董
发布2021-11-30 21:14:04
1.9K0
发布2021-11-30 21:14:04
举报
文章被收录于专栏:python前行者python前行者

文章目录

1、单独运行的clojure

关于clojure是什么东西就自行百度了,简单说就是用Lisp语言写的程序,编译成.class放在jvm上面跑,所以是需要jre的。独立的clojure可以从官方网站找到,目前稳定版本是1.8,官网https://clojure.org/index。下载后是一个zip,里面有个jar是clojure-1.8.0.jar,可以像常用的jar那样运行,这样会得到一个REPL交互环境,当然需要预先配置好jre(java8)。

https://repo1.maven.org/maven2/org/clojure/clojure/1.8.0/clojure-1.8.0.zip

image
image

2、安装leiningen

这个是clojure上类似maven的东西。貌似它本质上就是整合了maven,会用到maven在当前用户下的配置文件,但应该不需要maven本身。安装和运行Leiningen也不需要预先安装clojure本身。

安装方法1:

官网 https://leiningen.org/,上面有安装方法,就是通过一个script(lein for linux,或者lein.bat for win)来安装和运行。

  • lein.bat self-install,安装出错,原因是无法下载lein它本身的jar。我在公司用代理,所以需要设置代理服务器。可以在lein script的最前面加上例如export HTTPS_PROXY=http://Username/Password@proxyserver:port的格式。
  • 如果仍然无法下载,可以尝试直接从leiningen的github那里下载,https://github.com/technomancy/leiningen/releases,目前有2.7.1版本,下载到的是一个zip,可以改后缀名为jar,然后按照message的提示,放到对应的位置,例如 c:/Users/abc/.lein/self-installs/leiningen-2.7.1-standalone.jar。也可以修改lein script内部查到jar的位置的变量,来指向这个jar。script内原文是这样的:
image
image
image
image

通过修改LEIN_HOME,来指向特定目录,例如 export LEIN_HOME="${LEIN_HOME:-"/home/test01/lein"}"。如有需要,再修改LEIN_JAR的指向,或者按照它的目录名来放置lein的jar。这样lein script就不会再尝试联网下载了。

安装方法2:

点我进入下载页,或者直接点我下载下载一个.exe的windows可执行文件,安装时使用默认配置,不然可能缺少self-installs文件夹。

image
image

关于leiningen,可以学习其中文教程

运行安装好的clojure REPL,不出意外的话会闪退,闪退的话去cmd运行 lein -version 会提示你要先安装,安装命令看提示,但该命令因为有墙基本上都执行不了,按照下一步安装。

手动配置leiningen

去https://github.com/technomancy/leiningen/releases下载leiningen-2.8.1-standalone.zip,找到上一步安装lein安装目录,放在self-install文件夹下(C:\Users\marvin.lein\self-installs),把文件后缀名改成.jar。

image
image

在cmd中执行lein repl即可进入clojure命令行,执行lein repl

image
image

或者可以直接在windows开始栏搜索安装的程序,点击打开。

image
image

更进一步,输出个Hello World吧

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-WKCrAuWT-1638189674822)(//upload-images.jianshu.io/upload_images/4109786-cde910d31fbbe374.png?imageMogr2/auto-orient/strip|imageView2/2/w/815/format/webp)]

3、使用Intellij Idea作为开发IED

需要安装Cursive插件

注意:如果在线安装安装不上的话,可以下载你的IDE对应版本插件的zip包,进行本地导入安装。

如图是本地新建的一个demo工程,可以在.clj文件上直接右击运行。

image
image

代码:

代码语言:javascript
复制
(ns firstclojure.core)

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

(println (foo "Hello Clojure \n"))

通过lein run的方式也可以很方便的执行代码,但是如果要将我们的代码share出去,就需要生成一个jar文件了,我们使用lein uberjar来生成jar,生成的jar文件为target/uberjar/clojure-noob-0.1.0-SNAPSHOT-standalone.jar,我们可以在java里面直接运行了。

代码语言:javascript
复制
java -jar target/uberjar/clojure-noob-0.1.0-SNAPSHOT-standalone.jar
Hello, Clojure!

更多资源可以通过我们公司的Clojure站点学习,Clojure学习

4、运行和打包lein项目

由于实际项目中,都要用到第三方包,所以依赖关系的处理就无法避免了。用lein来运行和打包项目,就跟maven的道理一样。测试中遇到问题,就是jre链接不上lein的官方仓库clojars,提示SSL认证问题。这时可以用浏览器打开它的主页,保存网站的证书为cer文件,然后添加到jre的lib/security/cacerts文件中,这个是jre的默认证书仓库。具体步骤参考java的keytool的用法。

如果是在project.clj文件中指定了私有仓库,并且需要登录的,可以在project.clj对应的那个repository后面加上username和password,例如下面的配置。可以参照官方的例子 https://github.com/technomancy/leiningen/blob/stable/sample.project.clj,里面有注释提及。

代码语言:javascript
复制
["repo-releases" {:url "http://blueant.com/archiva/internal"
                          :username "abcd"
                          :password "1234"}]

而lein的几个简单用法如下:

lein repl:打开REPL环境。 lein run:运行项目。 lein uberjar:打包项目(cd 到project.clj所在路径),包含依赖项。得到jar后就跟平常的jar没有区别了。

5、leiningen构建工具的project.clj配置

代码语言:javascript
复制
;;================================================================================  
;;这是一个包含注释的project.clj文件。  
;;包含了所有选项。可以视为一个配置样本。  
;;包含了比”lein帮助教程”更详细的注释  
;;================================================================================  
  
;; 这是一个项目名叫 "sameple"   
;; 组名(或者公司网站名之类的,group-id)叫 "org.example"  
;; 版本(version)为"1.0.0-SNAPSHOT"的项目(project)  
(defproject org.example/sample "1.0.0-SNAPSHOT"   
  ;;除了这一点,你可能前面加上反引号(unquote),或~,对它求值(eval)。(这个注释不是我也不是很明白)  
  
  ;; 这个描述文本有助于仓库搜索(比如clojars仓库)  
  :description "A sample project"  
    
  :url "http://example.org/sample-clojure-project"  
    
  ;;邮件列表。没啥好说的  
  :mailing-list {:name "sample mailing list"  
                 :archive "http://example.org/sample-mailing-list-archives"  
                 :other-archives ["http://example.org/sample-list-archive2"  
                                  "http://example.org/sample-list-archive3"]  
                 :post "list@example.org"  
                 :subscribe "list-subscribe@example.org"  
                 :unsubscribe "list-unsubscribe@example.org"}  
    
  ;;license这个也没啥好说的  
  :license {:name "Eclipse Public License - v 1.0"  
            :url "http://www.eclipse.org/legal/epl-v10.html"  
            :distribution :repo  
            :comments "same as Clojure"}  
    
  ;;依赖的格式类似:[group-id/project-name version]  
  ;; classifier :它表示在相同版本下针对不同的环境或者jdk使用的jar,如果配置了这个元素,则会将这个元素名在加在最后来查找相应的jar  
  ;; exclusions : 用来排除相应的重复依赖。比如log4j下包含了a.jar。spring.jar也包含了a.jar,但是两个版本不同,则需要排除掉一个,避免冲突。  
  :dependencies [[org.clojure/clojure "1.1.0"]  
                 [org.clojure/clojure-contrib "1.1.0"]  
                 [org.jclouds/jclouds "1.0-RC6" :classifier "jdk15"]  
                 [log4j "1.2.15" :exclusions [javax.mail/mail  
                                              javax.jms/jms  
                                              com.sun.jdmk/jmxtools  
                                              com.sun.jmx/jmxri]]]  
    
  ;; 只用于开发阶段的依赖。打包部署将不包含这些依赖。  
  :dev-dependencies [[org.clojure/swank-clojure "1.2.1"]]  
    
  ;; 全局的一个依赖排除。  
  :exclusions [org.apache.poi/poi  
               org.apache.poi/poi-ooxml]  
                 
  ;;在project.clj改变或者库文件目录(:library-path directory)为空时,重新获取依赖。  
  :checksum-deps true  
    
  ;;如果版本低于这个就警告  
  :min-lein-version "1.3.0"  
    
  ;; 如果这个选项为false,那么获取依赖的时候,lib目录将被清空。  
  ;; 如果要lib目录不被清空,请将它设为true  
  :disable-deps-clean false  
  
  ;; 禁用隐式的clean  
  :disable-implicit-clean true  
    
  ;; Delete .class files that do not have a corresponding package in  
  ;; the src/ directory. Workaround for Clojure bug CLJ-322. Causes problems  
  ;; with protocols in upstream libraries; false by default. Set to  
  ;; true to delete all non-project classes or set to a seq of regexes  
  ;; to only delete class files that match one of the regexes.  
  (这个翻译我觉的可能有问题,故保留)  
  ;; 不存在src/目录,删除所有.class文件  
  ;; 为了解决Clojure bug CLJ-322, 可以将其设置为true,或者定义一个正则序列,仅删除不匹配的class文件。  
  :clean-non-project-classes true  
    
  ;; 如果 :clean-non-project-classes 设置为true  
  ;; 你可以设置这个正则,用来保留匹配的class文件。  
  :class-file-whitelist #"^(org/example|clojure)"  
      
  ;; 在clean阶段其他文件将被删除(除了:compile-path 和 jars/uberjars)。  
  ;; %s这个符号将被替换成当前项目的版本号  
  :extra-files-to-clean ["tmp" "sample-%s.tar"]  
      
  ;; 如果你不能精确匹配到你要删除的文件名  
  ;; 你可以使用正则表达(从项目根目录对文件名进行匹配)  
  ;; 默认为 #"^$NAME-.*\.jar$".  
  :regex-to-clean #"hs_err_pid.*"  
    
  ;; 项目的checkout路径  
  :checkout-deps-shares [:source-path :test-path  
                         ~(fn [p] (str (:root p) "/lib/dev/*"))]  
                           
  ;; 在启动时加载hooks中的namespaces  
  ;; Hooks一般来自插件,但也可能包含在你的项目source中  
  :hooks [leiningen.hooks.difftest]  
    
  ;; Predicates to determine whether to run a test or not. See tutorial.  
  ;; 决定是否运行测试。请参考tutorial  
  :test-selectors {:default (fn [t] (not (or (:integration v) (:regression v))))  
                   :integration :integration  
                   :regression :regression}  
                     
  ;; 如果设置这个为true。将加载所有和leiningen.hooks.*匹配的namespaces。  
  ;; 警告!!!:很明显的将导致加载依赖过多,启动起来比蜗牛还慢    
  :implicit-hooks false  
    
    
  ;; (提示:ahead-of-time (AOT) compiler 是一个实现时间提前编译的编译器)    
  ;; gen-class和java互操作所需的将被提前编译。:namespaces 在这里是个别名  
  ;; 设置一个正则将编译所以匹配的  
  :aot [org.example.sample]  
    
    
  ;; 打包成jar文件的入口函数  
  ;; 设置 :skip-aot 元数据用来做其他事情。例如运行shell或者task。  
  :main org.example.sample  
    
  ;; 在repl启动的时候自动加载这个namespace  
  :repl-init sample.repl-helper  
    
  ;;和:repl-init一样,不过已经是过时的东西了,所以请使用:repl-init。  
  :repl-init-script "src/main/clojure/init.clj"  
    
  ;; 这些将传递给 clojure.main/repl; 查看他们的细节  
  :repl-options [:prompt (fn [] (print "your command, master? ") (flush))]  
    
  ;; 自定义repl的监听端口  
  :repl-port 4001  
  :repl-host "0.0.0.0"  
    
  ;; A form to prepend to every form that is evaluated inside your project.  
  ;; Allows working around the Gilardi Scenario: http://technomancy.us/143  
  :project-init (require 'clojure.pprint)  
    
  ;; 超时重连。默认为100  
  :repl-retry-limit 1000  
    
  ;; 对所有的反射调用进行警告  
  :warn-on-reflection true  
    
  ;; 仓库配置。即使没配置,maven的中央仓库也依然会被查找。  
  :omit-default-repositories true  
  :repositories {"java.net" "http://download.java.net/maven/2"  
                 "sonatype"  
                 {:url "http://oss.sonatype.org/content/repositories/releases"  
                  ;; If a repository contains  releases only; setting :snapshots  
                  ;; to false will speed up dependency checking.  
                  :snapshots false  
                  ;; You can also set the policies for how to handle :checksum  
                  ;; failures to :fail, :warn, or :ignore. In :releases, :daily,  
                  ;; :always, and :never are supported.  
                  :releases {:checksum :fail  
                             :update :always}}  
                 ;; Repositories named "snapshots" and "releases" automatically  
                 ;; have their :snapshots and :releases disabled as appropriate.  
                 "snapshots" {:url "http://blueant.com/archiva/snapshots"  
                              ;; Also supports :private-key and :passphrase.  
                              :username "milgrim" :password "locative.1"}  
                 "releases" {:url "http://blueant.com/archiva/internal"  
                             :username "milgrim" :password "locative.1"}}  
    
  ;; 开发依赖的仓库  
  :deploy-repositories {"releases" {:url "http://blueant.com/archiva/internal/releases"  
                                    :username "milgrim" :password "locative.1"}  
                        "snapshots" "http://blueant.com/archiva/internal/snapshots"}  
    
  ;; 源文件路径  
  :source-path "src/main/clojure"  
  ;; 编译后的文件路径  
  :compile-path "target/classes" ; for .class files  
  ;; 打包所需jar文件路径  
  :library-path "target/dependency" ; for .jar files  
  ;; 单元测试源文件路径  
  :test-path "src/test/clojure"  
  ;; 配置文件路径  
  :resources-path "src/main/resource" ; non-code files included in classpath/jar  
  ;; 测试配置文件路径  
  :dev-resources-path "src/test/resource" ; added to dev classpath but not jar  
  ;; 本地依赖查找路径  
  :native-path "src/native"        ; where to look for native dependencies  
  ;; 目标路径  
  :target-dir "target/  "          ; where to place the project's jar file  
  ;; 额外的类路径  
  :extra-classpath-dirs ["script"] ; more classpath entries not included in jar  
  ;; jar包名  
  :jar-name "sample.jar"           ; name of the jar produced by 'lein jar'  
  ;; 一样的,jar包名。uberjar是可执行的jar包  
  :uberjar-name "sample-standalone.jar" ; as above for uberjar  
  ;; 从~/.m2自定义classpath,而不是拷贝到:library-path.  
  :local-repo-classpath true  
    
  ;; java文件的编译目录  
  :javac-options {:destdir "classes/"}  
  :java-source-path "src/main/java" ; location of Java source  
  ;; Leave the contents of :source-path out of jars (for AOT projects)  
  :omit-source true  
  ;; 匹配的jar包将被排除  
  :jar-exclusions [#"(?:^|/).svn/"]  
  ;; 一样的东东,只针对于uberjar  
  :uberjar-exclusions [#"META-INF/DUMMY.SF"]  
  ;; 对jar's manifest设置任意的键值对。  
  :manifest {"Project-awesome-level" "super-great"}  
    
  ;; 设置jvm选项  
  :jvm-opts ["-Xmx1g"]  
  
  ;; 如果你的项目是一个Leiningen插件,设置这个跳过subprocess步骤  
  :eval-in-leiningen false  
    
  ;; 解决Clojure's agent的线程池问题。   
  ;; If you see RejectedExecutionException using  
  ;; futures or agents, you may be working with a plugin that doesn't  
  ;; take this workaround into account yet--see the "Threads" section  
  ;; of doc/PLUGINS.md. This key will disable Leiningen's workaround.  
  ;; It may cause some other plugins to fail to exit when they finish.  
  :skip-shutdown-agents true  
    
  ;; 设置一个多模块的maven项目的父项目。  
  :parent [org.example/parent "0.0.1" :relative-path "../parent/pom.xml"])  
  
;; You can use Robert Hooke to modify behaviour of any task function,  
;; but the prepend-tasks function is shorthand that is more convenient  
;; on tasks that take a single project argument.  
  
;; 你可以使用Robert Hooke去修改任何task函数的行为。  
(use '[leiningen.core :only [prepend-tasks]]  
     '[leiningen.deps :only [deps]]  
     '[leiningen.clean :only [clean]]  
     '[leiningen.pom :only [pom]])  
  
;; 缩略名字参数调用  
(prepend-tasks #'deps clean pom)  

参考: https://www.jianshu.com/p/27e1fa8fc5cc https://www.cnblogs.com/pekkle/p/6901764.html https://www.iteye.com/blog/clojure-1310586 https://blog.csdn.net/qq_35885488/article/details/96563411

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2021-11-29 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • 1、单独运行的clojure
  • 2、安装leiningen
    • 安装方法1:
      • 安装方法2:
      • 3、使用Intellij Idea作为开发IED
      • 4、运行和打包lein项目
      • 5、leiningen构建工具的project.clj配置
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档