开发者

Does Clojure have a configuration file similar to ~/.clisprc.lisp?

On my platform, I need to add (set! *compile-path* (str *compile-path* ":.")) in order for (compile) to开发者_高级运维 find my scripts. I'd prefer not to have to type that every time I want to compile something.


The easiest way to handle setting your "compile path" in Clojure is to use a build tool like Leiningen or Cake to manage your project. Using these tools, you get an idiomatic project structure, all your source code automatically on the compile/class path, and nice command-line tools to handle dependency retrieval, running unit tests and building your projects.

Here are some of the basic command-line tasks defined by Leiningen, and thus available to you in any project:

classpath   Show the classpath of the current project.
clean       Remove compiled artifacts and jars from project.
compile     Compile Clojure source into .class files.
deps        Download all dependencies and place them in the :library-path.
help        Display a list of tasks or help for a given task.
install     Install the current project or download the project specified.
interactive Enter interactive shell for calling tasks without relaunching JVM.
jar         Package up all the project's files into a jar file.
javac       Compile Java source files.
new         Create a new project skeleton.
plugin      Manage user-level plugins.
pom         Write a pom.xml file to disk for Maven interop.
repl        Start a repl session either with the current project or standalone.
run         Run a -main function with optional command-line arguments.
swank       Launch swank server for Emacs to connect.
test        Run the project's tests.
test!       Run a project's tests after cleaning and fetching dependencies.
uberjar     Package up all the project's files and dependencies into a jar file.

So you start a new project by running lein new <name of project>, which generates a standard directory structure for a Clojure project. After you've written your code, you can run lein compile to simply compile your Clojure source, or you can go right to lein jar to package your code as a Jar file. For an executable jar that includes the Clojure language and all dependencies necessary to run your program, use lein uberjar instead.

If you don't use these tools, then you need to manage the classpath manually, to include where you store your dependency jars and where your source code lives. I highly recommend using one of the above-mentioned build tools.


You can specify -i when running Clojure to have it evaluate a file when starting up. Below is the script I use to run Clojure as an example:

#!/bin/bash

# GUI mode
if [ "$1" != "--no-fork" ]; then
    gnome-terminal -t Clojure -x $0 --no-fork $* &
    exit
fi

shift

breakchars="(){}[],^%$#@\"\";:''|\\"
if [ -f project.clj ]; then
    lein repl
else
    rlwrap --remember -c -b "$breakchars" \
           java -Djava.ext.dirs=$HOME/.clojure clojure.main \
           -i $HOME/.clojurerc --repl
fi


Leiningen will load ~/.lein/init.clj every time it launches. In addition, you can add a :repl-init key to your project.clj files to have that namespace loaded in each repl. Clojure is really not meant to be used standalone without any supporting tools, so calling (compile [...]) on your own is almost never the right answer.


In Clojure this is managed at the Java level (classpath etc) rather than having a .rc file. When I first started programming in Clojure I had a bash script that I would run, but now I use Leiningen.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜