Mac 配置安装 Clojure 及 Idea 环境

2016-08-21  本文已影响1527人  木猫尾巴

[TOC]

安装基础环境

IDEA 2016

homebrew

jdk 1.8

ClojureScript 需要最新版本的 Java 本文用的是 jdk 1.8

安装clojure

brew update
brew search clojure
# info start
clojurescript                            homebrew/versions/clojure14
homebrew/emacs/clojure-mode

If you meant "clojure" precisely:

Clojure isn't really a program but a library managed as part of a
project and Leiningen is the user interface to that library.

To install Clojure you should install Leiningen:
  brew install leiningen
and then follow the tutorial:
  [https://github.com/technomancy/leiningen/blob/stable/doc/TUTORIAL.md](https://github.com/technomancy/leiningen/blob/stable/doc/TUTORIAL.md)

# info end

# 所以需要安装 leiningen
brew install leiningen

安装leiningen

可以选择安装

brew install clojurescript

检查安装

lein repl

这个是会等待一下,执行很多下载什么的执行完成后显示内容

REPL-y 0.3.7, nREPL 0.2.12
Clojure 1.8.0
Java HotSpot(TM) 64-Bit Server VM 1.7.0_79-b15
    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=>

这时就在ClojureScript Repl 中了
让我们能快速执行 ClojureScript 并且查看结果

如果要离开 repl 你可以按下 control+d

安装IDEA 插件

在IDEA中搜索插件,安装

重启后,配置 Leiningen的位置,因为是使用brew安装的路径是

/usr/local/Cellar/leiningen/2.6.1

当然这个根据版本不一样,路径有微调

查看版本命令

brew info Leiningen

开始在IDEA中编写clojure工程

工程建立

在一个目录下执行命令

lein new sinlov.clojure/base_learn
cd base_learn
# 生成pom
lein pom

lein new groupId/artifactId。groupId和artifactId和Maven里的概念一致
lein pom 会生成对应的pom文件,这个工程就使用maven管理了

然后在IDEA中使用File->New->Project from Existing Source
选中刚才生成的pom,然后使用IDEA打开,一路next, 注意选择jdk 1.8
然后等候各种插件于工具加载完毕

编译执行

在工程的 src/sinlov.clojure/ 中有文件 base_learn.clj 修改内容

(defn -main [& args] (println "Hello, World!"))

增加main函数后,在IDEA中最上边的菜单中点Run,这一次运行是失败的,不要慌

选择Run 右边的箭头菜单里的 Edit Configurations

在弹错的对话框中,选中Run main function in the script namespace,点OK

这个时候才会真正运行打印 Hello,world!

lein run 运行错误

如果执行lein run, 会说“No :main namespace specified in project.clj”

需要修改project.clj


(defproject sinlov.clojure/learn/base_learn "0.1.0-SNAPSHOT"
  :main sinlov.clojure.base_learn
  :dependencies [[org.clojure/clojure "1.6.0"]])

执行lein run,输出"Hello, world!"

Error

java.util.regex.PatternSyntaxException

如果看到这个错误

Exception in thread "main" java.util.regex.PatternSyntaxException:    Unknown inline modifier near index 2 (?U)^[\p{Alpha}_$]^, compiling:(cljs/util.clj:158:33)

就需要去更新java

如果出现错误 already installed

brew unlink brew-cask
brew install brew-cask
brew cask install java

去修复这个问题


参考

rakhmad/clojure.md](https://gist.github.com/rakhmad/2407109))

上一篇 下一篇

猜你喜欢

热点阅读