Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 126 lines (89 sloc) 4.231 kb
14e3059 Matjaz Gregoric Initial commit.
authored
1 # lein-oneoff
2
3 Dealing with dependencies and the classpath can be a
4 pain. [Leiningen](http://github.com/technomancy/leiningen) takes most
5 of the pain away, but creating a new leiningen project for a simple
6 one-off script may sometimes feel like overkill. This is where
7 [lein-oneoff](http://github.com/mtyaka/lein-oneoff) comes in.
8
9 With the help of lein-oneoff you can open a file, declare
d2d8d15 Matjaz Gregoric Update the readme.
authored
10 dependencies at the top, and write the rest of the code as
14e3059 Matjaz Gregoric Initial commit.
authored
11 usually. lein-oneoff will let you run the file, open a repl or start a swank
12 server while taking care of fetching dependencies and constructing the
13 classpath automatically.
14
d2d8d15 Matjaz Gregoric Update the readme.
authored
15 You might find lein-oneoff useful when you want to play with a brand
16 new alpha release of clojure, but would rather not dowload the jar
17 manually, when you think you know the answer to a question about a
18 particular library posted to the clojure mailing list, but would
19 rather test your idea out in the repl before posting the answer, or when
20 you quickly want to analyse and plot some data using
21 [Incanter](http://incanter.org/).
22
14e3059 Matjaz Gregoric Initial commit.
authored
23 ## Usage
24
25 lein-oneoff scripts usually consist of a single file. Dependencies
094471e Tavis Rudd allow the defdeps form to be prefixed with the #_ reader macro
tavisrudd authored
26 should be stated at the top using the `defdeps` form. You may
27 optionally prefix the `defdeps` form with the reader macro #_ (ignore
28 next form). Here's an example:
14e3059 Matjaz Gregoric Initial commit.
authored
29
094471e Tavis Rudd allow the defdeps form to be prefixed with the #_ reader macro
tavisrudd authored
30 #_(defdeps
0364751 Matjaz Gregoric Fix identation.
authored
31 [[org.clojure/clojure "1.2.0"]
32 [compojure "0.5.2"]
33 [ring/ring-jetty-adapter "0.3.3"]])
14e3059 Matjaz Gregoric Initial commit.
authored
34
35 (ns example
49a914b Matjaz Gregoric Fix broken example in README.
authored
36 (:use [compojure.core :only [defroutes GET]]
14e3059 Matjaz Gregoric Initial commit.
authored
37 [ring.adapter.jetty :only [run-jetty]]))
38
39 (defroutes routes
40 (GET "/" [] "Hello world!"))
41
42 (def server
43 (run-jetty routes {:port 8080 :join? false}))
44
45 Save this file as `example.clj`, then run it with:
46
47 $ lein oneoff example.clj
48
49 This command will check the specified dependencies and install them
50 into the local maven repository (`~/.m2/repository`) unless already
51 installed, and then run `example.clj` with the necessary dependencies
52 in the classpath. Note that the dependencies are referenced directly
53 from the local maven repository.
54
55 ### The defdeps form
56
57 The `defdeps` form must be the first form in the file. It has the following
58 signature:
59
60 (defdeps dependencies additional-entries?)
61
62 where dependencies should be specified as a vector using the same
63 syntax as inside regular leiningen `defproject` form under the
64 `:dependencies` key. The second argument is an optional map of
094471e Tavis Rudd allow the defdeps form to be prefixed with the #_ reader macro
tavisrudd authored
65 additional standard `defproject` entries. Please note that not all of
66 the available leinigen options make sense for a one-off script and
67 might not work correctly. Adding a `#_` prefix will make it possible
5efee9d Matjaz Gregoric Fix a typo.
authored
68 to ignore the `defdeps` form when re-compiling the file in a repl.
14e3059 Matjaz Gregoric Initial commit.
authored
69
70 One of the entries that can be useful is the `:repositories` entry. Here's
71 an example:
72
73 (defdeps
74 [[org.clojure/clojure "1.3.0-alpha3"]
75 [org.apache.pivot/pivot-web "1.5.2"]]
76 {:repositories
77 {"apache" "https://repository.apache.org/content/repositories/releases/"}})
78
79 The `defdeps` form may be omitted in which case the only assumed
80 dependency is `org.clojure/clojure` of the same version as your leiningen
81 installation is using.
82
83 ### repl
84
85 To start a repl in the context of a one-off script, use the `--repl`
86 command (or its shorter equivalent, `-r`):
87
88 $ lein oneoff --repl example.clj
89 $ lein oneoff -r example.clj
90
91 ### swank
92
93 A swank server can be started with the `--swank` (or `-s`)
94 command:
95
96 $ lein oneoff --swank example.clj
97 $ lein oneoff -s example.clj
98
99 Please note that for the swank command to work, you'll need to have
56a57ad Matjaz Gregoric Update notes about minimal swank-clojure version.
authored
100 `swank-clojure 1.3.0` or newer installed as a user-level leiningen
101 plugin.
14e3059 Matjaz Gregoric Initial commit.
authored
102
103 ### classpath
104
105 lein-oneoff offers an equivalent to leiningen's built-in `classpath`
106 task which prints the project's classpath for one-off scripts:
107
108 $ lein oneoff --classpath example.clj
c249aec Matjaz Gregoric Change -c option to -cp to match the leiningen alias.
authored
109 $ lein oneoff -cp example.clj
14e3059 Matjaz Gregoric Initial commit.
authored
110
111 ## Installation
112
d2d8d15 Matjaz Gregoric Update the readme.
authored
113 This plugin should be installed as a user-level leiningen
114 plugin. Leiningen 1.4.0 comes with a built-in task for installing
115 user-level plugins:
e76dc2e Matjaz Gregoric Update installation instructions.
authored
116
de86d1b Matjaz Gregoric Release 0.2.0.
authored
117 $ lein plugin install lein-oneoff 0.2.0
14e3059 Matjaz Gregoric Initial commit.
authored
118
d2d8d15 Matjaz Gregoric Update the readme.
authored
119 lein-oneoff works with leiningen 1.4.0 or newer.
14e3059 Matjaz Gregoric Initial commit.
authored
120
121 ## License
122
123 Copyright (C) 2010 Matjaz Gregoric
124
125 Distributed under the Eclipse Public License, the same as Clojure.
Something went wrong with that request. Please try again.