2010-06-16 01:58:14 +00:00
|
|
|
# Tutorial
|
|
|
|
|
|
|
|
For those of you new to the JVM who have never touched Ant or Maven in
|
|
|
|
anger: don't panic. Leiningen is designed with you in mind. This
|
2010-06-17 04:10:56 +00:00
|
|
|
tutorial will help you get started and explain Leiningen's take on
|
2011-07-06 03:43:25 +00:00
|
|
|
project automation and JVM-land dependency management.
|
2010-06-16 01:58:14 +00:00
|
|
|
|
2012-05-24 05:31:08 +00:00
|
|
|
Also keep in mind that Leiningen ships with fairly comprehensive help;
|
|
|
|
`lein help` gives a list of tasks while `lein help task` provides
|
|
|
|
details. Further documentation such as the readme, sample
|
|
|
|
configuration, and even this tutorial are also provided.
|
|
|
|
|
2010-06-16 01:58:14 +00:00
|
|
|
## Creating a Project
|
|
|
|
|
|
|
|
We'll assume you've got Leiningen installed as per the
|
2012-05-25 01:27:04 +00:00
|
|
|
[README](https://github.com/technomancy/leiningen/blob/preview/README.md).
|
2010-06-16 01:58:14 +00:00
|
|
|
Generating a new project is easy:
|
|
|
|
|
2012-01-13 06:48:44 +00:00
|
|
|
$ lein new my-stuff
|
2010-06-16 01:58:14 +00:00
|
|
|
|
2012-01-13 06:48:44 +00:00
|
|
|
Generating a project called my-stuff based on the 'default' template.
|
2010-06-16 01:58:14 +00:00
|
|
|
|
2012-01-13 06:48:44 +00:00
|
|
|
$ cd my-stuff
|
2010-06-16 01:58:14 +00:00
|
|
|
$ tree
|
|
|
|
.
|
|
|
|
|-- project.clj
|
2012-01-13 06:48:44 +00:00
|
|
|
|-- README.md
|
2010-06-16 01:58:14 +00:00
|
|
|
|-- src
|
2012-03-06 22:49:39 +00:00
|
|
|
| `-- my_stuff
|
|
|
|
| `-- core.clj
|
2010-06-16 01:58:14 +00:00
|
|
|
`-- test
|
2012-03-06 22:49:39 +00:00
|
|
|
`-- my_stuff
|
|
|
|
`-- core_test.clj
|
2010-06-16 01:58:14 +00:00
|
|
|
|
2012-01-13 06:48:44 +00:00
|
|
|
Here we've got your project's README, a `src/` directory containing the
|
|
|
|
code, a `test/` directory, and a `project.clj` file which describes your
|
2012-03-20 20:53:09 +00:00
|
|
|
project to Leiningen. The `src/my_stuff/core.clj` file corresponds to
|
|
|
|
the `my-stuff.core` namespace.
|
2010-06-16 01:58:14 +00:00
|
|
|
|
2012-03-06 22:49:39 +00:00
|
|
|
Note that we use `my-stuff.core` instead of just `my-stuff` since
|
|
|
|
single-segment namespaces are discouraged in Clojure. Also note that
|
|
|
|
namespaces with dashes in the name will have the corresponding file
|
|
|
|
named with underscores instead since the JVM has trouble loading files
|
2012-06-14 23:26:52 +00:00
|
|
|
with dashes in the name. The intricacies of namespaces are a common
|
|
|
|
source of confusion for newcomers, and while they are mostly outside
|
|
|
|
the scope of this tutorial, you can
|
|
|
|
[read up on them elsewhere](http://blog.8thlight.com/colin-jones/2010/12/05/clojure-libs-and-namespaces-require-use-import-and-ns.html).
|
2010-06-16 01:58:14 +00:00
|
|
|
|
|
|
|
## project.clj
|
|
|
|
|
|
|
|
$ cat project.clj
|
|
|
|
|
2011-11-11 20:16:52 +00:00
|
|
|
```clj
|
2012-01-13 06:48:44 +00:00
|
|
|
(defproject my-stuff "0.1.0-SNAPSHOT"
|
2011-11-11 20:16:52 +00:00
|
|
|
:description "FIXME: write description"
|
2012-03-06 22:49:39 +00:00
|
|
|
:url "http://example.com/FIXME"
|
|
|
|
:license {:name "Eclipse Public License"
|
|
|
|
:url "http://www.eclipse.org/legal/epl-v10.html"}
|
2012-01-13 06:48:44 +00:00
|
|
|
:dependencies [[org.clojure/clojure "1.3.0"]])
|
2011-11-11 20:16:52 +00:00
|
|
|
```
|
2010-06-16 01:58:14 +00:00
|
|
|
|
2012-05-24 05:31:08 +00:00
|
|
|
If you don't fill in the `:description` with a short sentence, your
|
|
|
|
project will be harder to find in search results, so start there. Be
|
|
|
|
sure to fix the `:url` as well. At some point you'll need to flesh out
|
|
|
|
the README too, but for now let's skip ahead to setting
|
|
|
|
`:dependencies`. Note that Clojure is just another dependency here.
|
|
|
|
Unlike most languages, it's easy to swap out any version of Clojure.
|
2010-06-19 03:56:36 +00:00
|
|
|
|
2010-06-16 01:58:14 +00:00
|
|
|
## Dependencies
|
|
|
|
|
2011-06-24 05:49:00 +00:00
|
|
|
By default, Leiningen projects download dependencies from
|
2012-05-24 05:31:08 +00:00
|
|
|
[Clojars](http://clojars.org) and [Central](http://search.maven.org).
|
|
|
|
Clojars is the Clojure community's centralized jar repository, while
|
|
|
|
Central is for the wider JVM community.
|
|
|
|
|
|
|
|
Libraries for the JVM are packaged up as .jar files, which are
|
|
|
|
basically just .zip files with a little extra JVM-specific metadata.
|
|
|
|
They usually contain .class files (JVM bytecode) and .clj source
|
|
|
|
files, but they can also contain other things like config
|
|
|
|
files.
|
|
|
|
|
|
|
|
You can [search Clojars](http://clojars.org/search?q=clj-http) using its
|
2012-05-28 05:00:21 +00:00
|
|
|
web interface. On the page for `clj-http` it shows this:
|
2012-03-30 20:12:26 +00:00
|
|
|
|
2012-05-24 05:31:08 +00:00
|
|
|
[clj-http "0.4.1"]
|
2011-06-24 05:49:00 +00:00
|
|
|
|
2012-05-28 05:00:21 +00:00
|
|
|
There are two different ways of specifying a dependency on the latest
|
|
|
|
stable version of the `clj-http` library, one in Leiningen format
|
|
|
|
shown above and one in Maven format. We'll skip the Maven one for now,
|
|
|
|
though you'll need to learn to read it for Java libraries from
|
2012-05-25 01:26:46 +00:00
|
|
|
[Central](http://search.maven.org). You can copy the Leiningen version
|
|
|
|
directly into the `:dependencies` vector in `project.clj`.
|
2012-05-24 05:31:08 +00:00
|
|
|
|
|
|
|
Within the vector, "clj-http" is referred to as the "artifact id".
|
|
|
|
"0.4.1" is the version. Some libraries will also have "group ids",
|
|
|
|
which are displayed like this:
|
2012-04-29 21:56:54 +00:00
|
|
|
|
|
|
|
[com.cedarsoft.utils.legacy/hibernate "1.3.4"]
|
|
|
|
|
2012-05-03 17:45:41 +00:00
|
|
|
The group-id is the part before the slash. Especially for Java
|
|
|
|
libraries, it's often a reversed domain name. Clojure libraries often
|
2012-05-24 05:31:08 +00:00
|
|
|
use the same group-id and artifact-id (as with clj-http), in which case
|
2012-05-03 17:45:41 +00:00
|
|
|
you can omit the group-id. If there is a library that's part of a
|
|
|
|
larger group (such as `ring-jetty-adapter` being part of the `ring`
|
|
|
|
project), the group-id is often the same across all the sub-projects.
|
2010-06-22 02:21:13 +00:00
|
|
|
|
2010-06-16 01:58:14 +00:00
|
|
|
Sometimes versions will end in "-SNAPSHOT". This means that it is not
|
2010-06-19 03:56:36 +00:00
|
|
|
an official release but a development build. Relying on snapshot
|
|
|
|
dependencies is discouraged but is sometimes necessary if you need bug
|
2010-12-07 18:33:22 +00:00
|
|
|
fixes, etc. that have not made their way into a release yet. However,
|
|
|
|
snapshot versions are not guaranteed to stick around, so it's
|
2012-05-24 05:31:08 +00:00
|
|
|
important that non-development releases never depend upon snapshot versions that
|
2010-12-07 18:33:22 +00:00
|
|
|
you don't control. Adding a snapshot dependency to your project will
|
|
|
|
cause Leiningen to actively go seek out the latest version of the
|
2012-05-24 05:31:08 +00:00
|
|
|
dependency daily (whereas normal release versions are cached in the local
|
2012-01-13 06:48:44 +00:00
|
|
|
repository) so if you have a lot of snapshots it will slow things
|
|
|
|
down.
|
2010-06-16 01:58:14 +00:00
|
|
|
|
2011-05-04 02:42:31 +00:00
|
|
|
Note that some libraries make their group-id and artifact-id
|
|
|
|
correspond with the namespace they provide inside the jar, but this is
|
|
|
|
just a convention. There is no guarantee they will match up at all, so
|
2012-01-13 06:48:44 +00:00
|
|
|
consult the library's documentation before writing your `:require`
|
2012-05-24 05:31:08 +00:00
|
|
|
and `:import` clauses.
|
2011-05-04 02:42:31 +00:00
|
|
|
|
2012-01-13 06:48:44 +00:00
|
|
|
You can add third-party repositories by setting the `:repositories` key
|
2011-06-24 05:49:00 +00:00
|
|
|
in project.clj. See the
|
2012-05-24 05:31:08 +00:00
|
|
|
[sample.project.clj](https://github.com/technomancy/leiningen/blob/preview/sample.project.clj).
|
|
|
|
|
|
|
|
## Running Code
|
2010-06-16 01:58:14 +00:00
|
|
|
|
2012-05-25 01:27:04 +00:00
|
|
|
Enough setup; let's see some code running. Start with a REPL
|
|
|
|
(read-eval-print loop):
|
|
|
|
|
|
|
|
$ lein repl
|
|
|
|
nREPL server started on port 40612
|
|
|
|
Welcome to REPL-y!
|
|
|
|
Clojure 1.4.0
|
|
|
|
Exit: Control+D or (exit) or (quit)
|
|
|
|
Commands: (user/help)
|
|
|
|
Docs: (doc function-name-here)
|
|
|
|
(find-doc "part-of-name-here")
|
|
|
|
Source: (source function-name-here)
|
|
|
|
(user/sourcery function-name-here)
|
|
|
|
Javadoc: (javadoc java-object-or-class-here)
|
|
|
|
Examples from clojuredocs.org: [clojuredocs or cdoc]
|
|
|
|
(user/clojuredocs name-here)
|
|
|
|
(user/clojuredocs "ns-here" "name-here")
|
|
|
|
|
|
|
|
user=>
|
|
|
|
|
|
|
|
The REPL is an interactive prompt where you can enter arbitrary code
|
|
|
|
to run in the context of your project. Since we've added `clj-http` to
|
|
|
|
`:dependencies`, we are able to load it here along with code from the
|
|
|
|
`my-stuff.core` namespace in your project's own `src/` directory:
|
|
|
|
|
|
|
|
user=> (require 'my-stuff.core)
|
|
|
|
nil
|
|
|
|
user=> (my-stuff.core/-main)
|
|
|
|
Hello, World!
|
|
|
|
nil
|
|
|
|
user=> (require '[clj-http.client :as http])
|
|
|
|
nil
|
|
|
|
user=> (def response (http/get "http://leiningen.org"))
|
|
|
|
#'user/response
|
|
|
|
user=> (keys response)
|
|
|
|
(:trace-redirects :status :headers :body)
|
|
|
|
|
|
|
|
The call to `-main` shows both println output ("Hello, World!") and
|
|
|
|
the return value (nil) together.
|
|
|
|
|
|
|
|
Built-in documentation is available via `doc`, while `clojuredocs`
|
|
|
|
offers more thorough examples from the
|
|
|
|
[ClojureDocs](http://clojuredocs.org) site:
|
|
|
|
|
|
|
|
user=> (doc reduce)
|
|
|
|
-------------------------
|
|
|
|
clojure.core/reduce
|
|
|
|
([f coll] [f val coll])
|
|
|
|
f should be a function of 2 arguments. If val is not supplied,
|
|
|
|
returns the result of applying f to the first 2 items in coll, then
|
|
|
|
applying f to that result and the 3rd item, etc. If coll contains no
|
|
|
|
items, f must accept no arguments as well, and reduce returns the
|
|
|
|
result of calling f with no arguments. If coll has only 1 item, it
|
|
|
|
is returned and f is not called. If val is supplied, returns the
|
|
|
|
result of applying f to val and the first item in coll, then
|
|
|
|
applying f to that result and the 2nd item, etc. If coll contains no
|
|
|
|
items, returns val and f is not called.
|
|
|
|
|
|
|
|
user=> (user/clojuredocs pprint)
|
|
|
|
Loading clojuredocs-client...
|
|
|
|
========== vvv Examples ================
|
|
|
|
user=> (def *map* (zipmap
|
|
|
|
[:a :b :c :d :e]
|
|
|
|
(repeat
|
|
|
|
(zipmap [:a :b :c :d :e]
|
|
|
|
(take 5 (range))))))
|
|
|
|
#'user/*map*
|
|
|
|
user=> *map*
|
|
|
|
{:e {:e 4, :d 3, :c 2, :b 1, :a 0}, :d {:e 4, :d 3, :c 2, :b 1, :a 0}, :c {:e 4, :d 3, :c 2, :b 1, :a 0}, :b {:e 4, :d 3, :c 2, :b 1, :a 0}, :a {:e 4, :d 3, :c 2, :b 1, :a 0}}
|
|
|
|
|
|
|
|
user=> (clojure.pprint/pprint *map*)
|
|
|
|
{:e {:e 4, :d 3, :c 2, :b 1, :a 0},
|
|
|
|
:d {:e 4, :d 3, :c 2, :b 1, :a 0},
|
|
|
|
:c {:e 4, :d 3, :c 2, :b 1, :a 0},
|
|
|
|
:b {:e 4, :d 3, :c 2, :b 1, :a 0},
|
|
|
|
:a {:e 4, :d 3, :c 2, :b 1, :a 0}}
|
|
|
|
nil
|
|
|
|
========== ^^^ Examples ================
|
|
|
|
1 example found for clojure.pprint/pprint
|
|
|
|
|
|
|
|
You can even examine the source of functions:
|
|
|
|
|
|
|
|
user=> (source my-stuff.core/-main)
|
|
|
|
(defn -main
|
|
|
|
"I don't do a whole lot."
|
|
|
|
[& args]
|
|
|
|
(println "Hello, World!"))
|
|
|
|
|
|
|
|
user=> ; use control+d to exit
|
|
|
|
|
|
|
|
If you already have code in a `-main` function ready to go and don't
|
|
|
|
need to enter code interactively, the `run` task is simpler:
|
|
|
|
|
|
|
|
$ lein run -m my-stuff.core
|
|
|
|
Hello, World!
|
|
|
|
|
|
|
|
Providing an alternate `-m` argument will tell Leiningen to look for
|
|
|
|
the `-main` function in another namespace. Setting a default `:main` in
|
|
|
|
`project.clj` lets you omit `-m`.
|
|
|
|
|
|
|
|
For long-running `lein run` processes, you may wish to save memory
|
2012-08-16 17:40:25 +00:00
|
|
|
with the higher-order trampoline task, which allows the Leiningen JVM
|
2012-05-25 01:27:04 +00:00
|
|
|
process to exit before launching your project's JVM.
|
|
|
|
|
|
|
|
$ lein trampoline run -m my-stuff.server 5000
|
2012-05-24 05:31:08 +00:00
|
|
|
|
|
|
|
## Tests
|
|
|
|
|
2012-08-16 17:40:25 +00:00
|
|
|
We haven't written any tests yet, but we can run the failing tests
|
|
|
|
included from the project template:
|
2010-06-16 01:58:14 +00:00
|
|
|
|
|
|
|
$ lein test
|
|
|
|
|
2012-05-24 05:31:08 +00:00
|
|
|
lein test my.test.stuff
|
2012-01-13 06:48:44 +00:00
|
|
|
|
|
|
|
FAIL in (a-test) (stuff.clj:7)
|
|
|
|
FIXME, I fail.
|
|
|
|
expected: (= 0 1)
|
|
|
|
actual: (not (= 0 1))
|
|
|
|
|
2010-06-16 01:58:14 +00:00
|
|
|
Ran 1 tests containing 1 assertions.
|
|
|
|
1 failures, 0 errors.
|
|
|
|
|
2012-08-16 17:40:25 +00:00
|
|
|
Once we fill it in the test suite will become more useful. Sometimes
|
2012-01-13 06:48:44 +00:00
|
|
|
if you've got a large test suite you'll want to run just one or two
|
2012-07-12 05:05:19 +00:00
|
|
|
namespaces at a time; `lein test my.test.stuff` will do that.. You
|
|
|
|
also might want to break up your tests using test selectors; see `lein
|
|
|
|
help test` for more details.
|
2010-11-10 06:20:22 +00:00
|
|
|
|
2012-03-06 22:49:39 +00:00
|
|
|
Running `lein test` from the command-line is suitable for regression
|
|
|
|
testing, but the slow startup time of the JVM makes it a poor fit for
|
|
|
|
testing styles that require tighter feedback loops. In these cases,
|
|
|
|
either keep a repl open for running the appropriate call to
|
2012-01-13 06:48:44 +00:00
|
|
|
[clojure.test/run-tests](http://clojuredocs.org/clojure_core/1.3.0/clojure.test/run-tests)
|
|
|
|
or look into editor integration such as
|
|
|
|
[clojure-test-mode](https://github.com/technomancy/clojure-mode).
|
2010-08-11 03:21:19 +00:00
|
|
|
|
2012-08-16 17:40:25 +00:00
|
|
|
Keep in mind that while keeping a running process around is convenient,
|
2010-11-21 06:22:51 +00:00
|
|
|
it's easy for that process to get into a state that doesn't reflect
|
2011-05-04 02:42:31 +00:00
|
|
|
the files on disk—functions that are loaded and then deleted from the
|
2010-11-21 06:22:51 +00:00
|
|
|
file will remain in memory, making it easy to miss problems arising
|
2011-05-04 02:42:31 +00:00
|
|
|
from missing functions (often referred to as "getting
|
2012-01-13 06:48:44 +00:00
|
|
|
slimed"). Because of this it's advised to do a `lein test` run with a
|
2012-03-06 22:49:39 +00:00
|
|
|
fresh instance periodically in any case, perhaps before you commit.
|
2010-11-21 06:22:51 +00:00
|
|
|
|
2010-06-29 19:58:23 +00:00
|
|
|
## What to do with it
|
2010-06-16 01:58:14 +00:00
|
|
|
|
2010-06-29 19:58:23 +00:00
|
|
|
Generally speaking, there are three different goals that are typical
|
|
|
|
of Leiningen projects:
|
2010-06-17 04:10:56 +00:00
|
|
|
|
2010-06-29 19:58:23 +00:00
|
|
|
* An application you can distribute to end-users
|
|
|
|
* A server-side application
|
2011-05-13 15:15:54 +00:00
|
|
|
* A library for other Clojure projects to consume
|
2010-06-17 04:10:56 +00:00
|
|
|
|
2012-05-24 05:31:08 +00:00
|
|
|
For the first, you typically build an uberjar. For libraries,
|
2012-03-06 22:49:39 +00:00
|
|
|
you will want to have them published to a repository like Clojars or a
|
|
|
|
private repository. For server-side applications it varies as
|
2012-07-12 05:05:19 +00:00
|
|
|
described below. Generating a project with `lein new app myapp` will
|
|
|
|
start you out with a few extra defaults suitable for non-library projects.
|
2010-06-19 03:56:36 +00:00
|
|
|
|
2010-06-29 19:58:23 +00:00
|
|
|
### Uberjar
|
|
|
|
|
2010-08-18 05:21:53 +00:00
|
|
|
The simplest thing to do is to distribute an uberjar. This is a single
|
2010-09-05 22:19:18 +00:00
|
|
|
standalone executable jar file most suitable for giving to
|
|
|
|
nontechnical users. For this to work you'll need to specify a
|
2012-01-13 06:48:44 +00:00
|
|
|
namespace as your `:main` in `project.clj`. By this point our
|
|
|
|
`project.clj` file should look like this:
|
2010-06-29 19:58:23 +00:00
|
|
|
|
2011-11-11 20:16:52 +00:00
|
|
|
```clj
|
2012-01-13 06:48:44 +00:00
|
|
|
(defproject my-stuff "0.1.0-SNAPSHOT"
|
2012-03-06 22:49:39 +00:00
|
|
|
:description "FIXME: write description"
|
|
|
|
:url "http://example.com/FIXME"
|
|
|
|
:license {:name "Eclipse Public License"
|
|
|
|
:url "http://www.eclipse.org/legal/epl-v10.html"}
|
2012-01-13 06:48:44 +00:00
|
|
|
:dependencies [[org.clojure/clojure "1.3.0"]
|
2011-11-11 20:16:52 +00:00
|
|
|
[org.apache.lucene/lucene-core "3.0.2"]
|
2012-05-24 05:31:08 +00:00
|
|
|
[clj-http "0.4.1"]]
|
2012-03-06 22:49:39 +00:00
|
|
|
:profiles {:dev {:dependencies [[midje "1.3.1"]]}}
|
2012-01-13 06:48:44 +00:00
|
|
|
:test-selectors {:default (complement :integration)
|
|
|
|
:integration :integration
|
|
|
|
:all (fn [_] true)}
|
|
|
|
:main my.stuff)
|
2011-11-11 20:16:52 +00:00
|
|
|
```
|
2010-06-29 19:58:23 +00:00
|
|
|
|
2012-01-13 06:48:44 +00:00
|
|
|
The namespace you specify will need to contain a `-main` function that
|
|
|
|
will get called when your standalone jar is run. This namespace should
|
|
|
|
have a `(:gen-class)` declaration in the `ns` form at the top. The
|
|
|
|
`-main` function will get passed the command-line arguments. Let's try
|
|
|
|
something simple in `src/my/stuff.clj`:
|
2010-06-17 04:10:56 +00:00
|
|
|
|
2011-11-11 20:16:52 +00:00
|
|
|
```clj
|
2012-01-13 06:48:44 +00:00
|
|
|
(ns my.stuff
|
2011-11-11 20:16:52 +00:00
|
|
|
(:gen-class))
|
2010-06-16 01:58:14 +00:00
|
|
|
|
2011-11-11 20:16:52 +00:00
|
|
|
(defn -main [& args]
|
|
|
|
(println "Welcome to my project! These are your args:" args))
|
|
|
|
```
|
2010-06-17 04:10:56 +00:00
|
|
|
|
2010-06-29 19:58:23 +00:00
|
|
|
Now we're ready to generate your uberjar:
|
2010-06-17 04:10:56 +00:00
|
|
|
|
|
|
|
$ lein uberjar
|
2012-01-13 06:48:44 +00:00
|
|
|
Compiling my.stuff
|
|
|
|
Compilation succeeded.
|
|
|
|
Created /home/phil/src/leiningen/my-stuff/target/my-stuff-0.1.0-SNAPSHOT.jar
|
|
|
|
Including my-stuff-0.1.0-SNAPSHOT.jar
|
2012-05-24 05:31:08 +00:00
|
|
|
Including clj-http-0.4.1.jar
|
|
|
|
Including clojure-1.3.0.jar
|
2010-06-29 19:58:23 +00:00
|
|
|
Including lucene-core-3.0.2.jar
|
2012-03-06 22:49:39 +00:00
|
|
|
Created /home/phil/src/leiningen/my-stuff/target/my-stuff-0.1.0-SNAPSHOT-standalone.jar
|
2010-06-17 04:10:56 +00:00
|
|
|
|
|
|
|
This creates a single jar file that contains the contents of all your
|
2012-01-13 06:48:44 +00:00
|
|
|
dependencies. Users can run it with a simple `java` invocation,
|
2010-06-17 04:19:04 +00:00
|
|
|
or on some systems just by double-clicking the jar file.
|
2010-06-17 04:10:56 +00:00
|
|
|
|
2012-01-13 06:48:44 +00:00
|
|
|
$ java -jar my-stuff-0.1.0-standalone.jar Hello world.
|
2010-06-29 19:58:23 +00:00
|
|
|
Welcome to my project! These are your args: (Hello world.)
|
|
|
|
|
2012-01-13 06:48:44 +00:00
|
|
|
You can run a regular (non-uber) jar with the `java`
|
2010-06-29 19:58:23 +00:00
|
|
|
command-line tool, but that requires constructing the classpath
|
|
|
|
yourself, so it's not a good solution for end-users.
|
|
|
|
|
2012-05-27 09:12:04 +00:00
|
|
|
Of course if your users already have Leiningen installed, you can
|
2012-05-25 01:27:04 +00:00
|
|
|
instruct them to use `lein run` as described above.
|
2011-06-24 05:49:00 +00:00
|
|
|
|
2011-03-16 01:45:04 +00:00
|
|
|
### Server-side Projects
|
|
|
|
|
|
|
|
There are many ways to get your project deployed as a server-side
|
2012-07-12 05:05:19 +00:00
|
|
|
application. Aside from the obvious uberjar approach, simple
|
2012-05-24 05:31:08 +00:00
|
|
|
programs can be packaged up as tarballs with accompanied shell scripts
|
|
|
|
using the [lein-tar plugin](https://github.com/technomancy/lein-tar)
|
|
|
|
and then deployed using
|
|
|
|
[pallet](http://hugoduncan.github.com/pallet/),
|
2012-08-11 01:51:43 +00:00
|
|
|
[chef](http://opscode.com/chef/), or other mechanisms.
|
2012-07-12 05:05:19 +00:00
|
|
|
Web applications may be deployed as uberjars using embedded Jetty with
|
|
|
|
`ring-jetty-adapter` or as .war (web application archive) files
|
|
|
|
created by the
|
|
|
|
[lein-ring plugin](https://github.com/weavejester/lein-ring). For
|
|
|
|
things beyond uberjars, server-side deployments are so varied that they
|
|
|
|
are better-handled using plugins rather than tasks that are built-in
|
|
|
|
to Leiningen itself.
|
|
|
|
|
|
|
|
If you do end up involving Leiningen in production via something like
|
|
|
|
`lein trampoline run`, it's very important to ensure you take steps to
|
|
|
|
freeze all the dependencies before deploying, otherwise it could be
|
|
|
|
easy to end up with
|
2012-03-06 22:49:39 +00:00
|
|
|
[unrepeatable deployments](https://github.com/technomancy/leiningen/wiki/Repeatability).
|
2012-07-12 05:05:19 +00:00
|
|
|
Consider including `~/.m2/repository` in your unit of deployment along
|
|
|
|
with your project code. It's recommended to use Leiningen to create a
|
|
|
|
deployable artifact in a continuous integration setting. For example,
|
|
|
|
you could have a [Jenkins](http://jenkins-ci.org) CI server run your
|
|
|
|
project's full test suite, and if it passes, upload a tarball to S3.
|
|
|
|
Then deployment is just a matter of pulling down and extracting the
|
2012-08-16 17:40:25 +00:00
|
|
|
known-good tarball on your production servers.
|
2012-07-12 05:05:19 +00:00
|
|
|
|
|
|
|
Also remember that the `run` task defaults to including the `user`, `dev`,
|
|
|
|
and `default` profiles, which are not suitable for production. Using
|
|
|
|
`lein trampoline with-profile production run -m myapp.main` is
|
2012-08-16 17:40:25 +00:00
|
|
|
recommended. By default the production profile is empty, but if your
|
|
|
|
deployment includes the `~/.m2/repository` directory from the CI run
|
|
|
|
that generated the tarball, then you should add its path as
|
|
|
|
`:local-repo` along with `:offline? true` to the `:production`
|
|
|
|
profile. Staying offline prevents the deployed project from diverging
|
|
|
|
at all from the version that was tested in the CI environment.
|
2011-05-13 15:15:54 +00:00
|
|
|
|
2011-03-16 01:45:04 +00:00
|
|
|
### Publishing Libraries
|
2010-06-29 19:58:23 +00:00
|
|
|
|
|
|
|
If your project is a library and you would like others to be able to
|
|
|
|
use it as a dependency in their projects, you will need to get it into
|
2011-05-04 02:42:31 +00:00
|
|
|
a public repository. While it's possible to
|
2012-05-24 05:31:08 +00:00
|
|
|
[maintain your own private repository](https://github.com/technomancy/leiningen/blob/preview/doc/DEPLOY.md)
|
2012-08-11 01:51:43 +00:00
|
|
|
or get it into [Central](http://search.maven.org), the easiest way is
|
|
|
|
to publish it at [Clojars](http://clojars.org). Once you have
|
2012-05-31 03:24:54 +00:00
|
|
|
[created an account](https://clojars.org/register) there, publishing
|
|
|
|
is easy:
|
|
|
|
|
|
|
|
$ lein deploy clojars
|
|
|
|
Created ~/src/my-stuff/target/my-stuff-0.1.0-SNAPSHOT.jar
|
|
|
|
Wrote ~/src/my-stuff/pom.xml
|
|
|
|
No credentials found for clojars
|
|
|
|
See `lein help deploying` for how to configure credentials.
|
|
|
|
Username: me
|
|
|
|
Password:
|
|
|
|
Retrieving my-stuff/my-stuff/0.1.0-SNAPSHOT/maven-metadata.xml (1k)
|
|
|
|
from https://clojars.org/repo/
|
|
|
|
Sending my-stuff/my-stuff/0.1.0-SNAPSHOT/my-stuff-0.1.0-20120531.032047-14.jar (5k)
|
|
|
|
to https://clojars.org/repo/
|
|
|
|
Sending my-stuff/my-stuff/0.1.0-SNAPSHOT/my-stuff-0.1.0-20120531.032047-14.pom (3k)
|
|
|
|
to https://clojars.org/repo/
|
|
|
|
Retrieving my-stuff/my-stuff/maven-metadata.xml (1k)
|
|
|
|
from https://clojars.org/repo/
|
|
|
|
Sending my-stuff/my-stuff/0.1.0-SNAPSHOT/maven-metadata.xml (1k)
|
|
|
|
to https://clojars.org/repo/
|
|
|
|
Sending my-stuff/my-stuff/maven-metadata.xml (1k)
|
|
|
|
to https://clojars.org/repo/
|
2010-06-29 19:58:23 +00:00
|
|
|
|
|
|
|
Once that succeeds it will be available as a package on which other
|
2012-05-31 03:24:54 +00:00
|
|
|
projects may depend. For instructions on storing your credentials so
|
2012-08-11 01:51:43 +00:00
|
|
|
they don't have to be re-entered every time, see `lein help
|
|
|
|
deploying`. When deploying a release that's not a snapshot, Leiningen
|
|
|
|
will attempt to sign it using [GPG](http://gnupg.org) to prove your
|
|
|
|
authorship of the release. See the
|
2012-07-12 05:05:19 +00:00
|
|
|
[deploy guide](https://github.com/technomancy/leiningen/blob/preview/doc/DEPLOY.md).
|
2012-08-11 01:51:43 +00:00
|
|
|
for details of how to set that up. The deploy guide includes
|
|
|
|
instructions for deploying to other repositories as well.
|
2010-06-17 04:10:56 +00:00
|
|
|
|
|
|
|
## That's It!
|
|
|
|
|
2011-03-16 01:45:04 +00:00
|
|
|
Now go start coding your next project!
|