• Stars
    star
    299
  • Rank 139,269 (Top 3 %)
  • Language
    Clojure
  • Created over 13 years ago
  • Updated about 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Leiningen plugin for executing Clojure scripts

lein-exec

Leiningen plugin to execute Clojure scripts

Installation

Lein 2 users

The recommended way is to install as a global plugin in ~/.lein/profiles.clj:

{:user {:plugins [[lein-exec "0.3.7"]]}}

You may also install as a project plugin in project.clj:

:plugins [[lein-exec "0.3.7"]]

Lein 1.x users

Either install as a plugin:

$ lein plugin install lein-exec "0.1"

Or, include as a dev-dependency:

:dev-dependencies [[lein-exec "0.1"]]

Usage

Lein 2 users

This blog post covers lein-exec with examples:

Scripting Clojure with Leiningen 2

Synopsis

lein exec [-p]
lein exec -e[p] <string-s-expr>
lein exec [-p] <script-path> [args]

When invoked without args it reads S-expressions from STDIN and evaluates them. When only option -p is specified, it evaluates STDIN in project context.

-e  evaluates the following string as an S-expression
-ep evaluates the following string as an S-expression in project (w/classpath)
-p  indicates the script should be evaluated in project (with classpath)

Examples

cat foo.clj | lein exec
lein exec -e '(println "foo" (+ 20 30))'
lein exec -ep "(use 'foo.bar) (pprint (map baz (range 200)))"
lein exec -p script/run-server.clj -p 8088
lein exec ~/common/delete-logs.clj

Optional args after script-path are bound to clojure.core/*command-line-args*

Getting dependencies from within script

Thanks to the pomegranate library in Leiningen, lein-exec exposes an API to specify dependencies that can be added dynamically to the CLASSPATH:

(use '[leiningen.exec :only (deps)])
(deps '[[ring/ring-core "1.0.0"]
        [ring/ring-jetty-adapter "1.0.0"]])
(deps '[[foo/bar "1.2.3"]]
      :repositories {"myrepo" "http://mycorp.com/repositories/"})

This downloads dependencies from Maven Central and Clojars if required, and uses the local repo on subsequent runs.

Executable scripts

This may be applicable to Unix-like systems only.

To run executable Clojure script files, you need to download the lein-exec and lein-exec-p scripts, make them executable, and put them in PATH. After downloading the scripts, you may also need to edit them in order to specify the correct name for the Leiningen executable.

$ wget https://raw.github.com/kumarshantanu/lein-exec/master/lein-exec
$ wget https://raw.github.com/kumarshantanu/lein-exec/master/lein-exec-p
$ chmod a+x lein-exec lein-exec-p
$ mv lein-exec lein-exec-p ~/bin  # assuming ~/bin is in PATH

Executable Clojure script files that need not run only in project-scope should have the following on the first line (shebang):

#!/usr/bin/env lein-exec

or,

#!/bin/bash lein-exec

Executable Clojure script files that are supposed to run only in project-scope should have the following on the first line (shebang):

#!/usr/bin/env lein-exec-p

or,

#!/bin/bash lein-exec-p

Windows users

Windows users may not have the shebang header goodness, but they can use the provided scripts (URLs below) as a convenience:

Lein 1.x users

You can execute scripts as follows in a project:

$ lein exec scripts/foobar.clj              # mention script path
$ lein exec scripts/run-server.clj -p 4000  # with arguments

The script would have project dependencies and source packages on CLASSPATH.

Writing code to eval with lein-exec

It needs to be written as if would be eval'ed (rather than compiled) - example below:

(require '[clojure.string :as str]
         '[clojure.pprint :as ppr]
         '[com.example.foo :as foo])


(defn baz
  [y]
  (let [x (str/join y (foo/quux :bla-bla))]
    (ppr/pprint [x foo/nemesis])))

(foo/bar :some-stuff)

(do
  (println *command-line-args*)  ; command-line args as a list
  (foo/bar :some-stuff)
  (baz ", "))

Execute an ns having -main using lein-exec

Append the following to the namespace having -main fn:

(try (require 'leiningen.exec)
     (when @(ns-resolve 'leiningen.exec '*running?*)
       (apply -main (rest *command-line-args*)))
     (catch java.io.FileNotFoundException e))

Note: This works only on lein-exec 0.3.4 and later.

Getting in touch

On Twitter: @kumarshantanu

On Leiningen mailing list: http://groups.google.com/group/leiningen

Contributors

License

Copyright (C) 2011-2017 Shantanu Kumar and contributors

Distributed under the Eclipse Public License, the same as Clojure.

More Repositories

1

lein-localrepo

Work with local Maven repository
Clojure
215
star
2

promenade

Take program design oddities in stride with Clojure/ClojureScript
Clojure
106
star
3

lein-sub

Leiningen plugin for executing tasks on sub-projects
Clojure
67
star
4

lein-clr

Leiningen plugin to automate build tasks for ClojureCLR projects
Clojure
58
star
5

clj-liquibase

Clojure wrapper for Liquibase for database change management and migration
Clojure
48
star
6

calfpath

À la carte Ring request matching, routing and reverse-routing for Clojure/Script
Clojure
33
star
7

lein-servlet

A Leiningen 2 plugin to work with servlet-based webapps
Clojure
31
star
8

dime

Dependency Injection Made Easy for Clojure
Clojure
30
star
9

stringer

Fast string operations for Clojure
Clojure
28
star
10

quiddity

S-Expression evaluation library for Clojure and ClojureScript
Clojure
17
star
11

ring-sse-middleware

Ring middleware to stream Server-sent Events
Clojure
17
star
12

clj-dbcp

Clojure wrapper for Apache DBCP to create JDBC connections pools
Clojure
16
star
13

keypin

Key lookup on steroids in Clojure
Clojure
14
star
14

citius

Comparative benchmarking using Criterium
Clojure
13
star
15

espejito

Latency breakup for single-threaded processing
Clojure
11
star
16

cljfreechart

Clojure wrapper for the JFreeChart library
Clojure
10
star
17

basil

A general purpose template library for Clojure and ClojureScript
Clojure
9
star
18

lein-idefiles

Generate IDE files for Leiningen projects
Clojure
9
star
19

lein-cascade

A Leiningen plugin to execute cascading task dependencies
Clojure
8
star
20

asphalt

A Clojure library for JDBC access.
Clojure
7
star
21

lein-viz

Visualize graph and tree data
Clojure
7
star
22

emacs24-config

Emacs 24 config for Clojure and web development using HTML/CSS/JavaScript
Emacs Lisp
6
star
23

cambium

PROJECT HAS MOVED - see https://cambium-clojure.github.io/
Clojure
5
star
24

ring-jdbc-session

JDBC based HTTP session store for Ring (Clojure)
Clojure
3
star
25

clj-jdbcutil

DEPRECATED - Clojure JDBC utility functions
Clojure
3
star
26

lein-docbkx

ABANDONED - Leiningen plugin for rendering Docbook documents (using Docbkx-tools)
Clojure
3
star
27

lein-javac-resources

Copy resources from :java-source-paths to compile path
Clojure
2
star
28

preflex

Resilience, instrumentation and metrics for Clojure
Clojure
2
star
29

hive-0.7.1-hbase-json

DEPRECATED - Hive 0.7.1 HBase Handler with JSON codec
Java
2
star
30

oss-jdbc

Aggregation of Open Source JDBC drivers on public Maven repos
2
star
31

clj-miscutil

Misclleneous utility functions and macros in Clojure
Clojure
2
star
32

coattail

Clojure/Script OpenAPI tooling to afford instant leverage
Clojure
1
star
33

vim7.3-config

Vim 7.3 config for Clojure and Ruby
Vim Script
1
star
34

lein-project-edn

Leiningen plugin to emit project details as EDN
Clojure
1
star
35

sqlrat

SQL template library for Clojure, ClojureScript and ClojureCLR
Clojure
1
star
36

fountain-jdbc

DEPRECATED - Clojure wrapper for SpringJDBC
Clojure
1
star
37

clip-test

A subset of `clojure.test` for Clojure and ClojureScript
Clojure
1
star
38

ringbelt

Utility tool belt for Ring web applications
Clojure
1
star
39

thingy

Data abstraction of 'things' in Clojure
Clojure
1
star
40

ide-files

DEPCRATED. Leiningen template to generate IDE files for existing projects. Superseded by the lein-idefiles plugin.
Clojure
1
star
41

cumulus

Obtain JDBC connection parameters for popular databases
Clojure
1
star