leiningen plugins and scripting

75 views
Skip to first unread message

Saul

unread,
Dec 30, 2009, 11:51:20 AM12/30/09
to Clojure
Hello,
I'm moving a small project over from maven built scala to leiningen
built clojure. One of the features that was available with the maven
scala plugin was the ability to run scala scripts:

mvn scala:script -DscriptFile=scripts/build_db.scala

This is useful for doing project specific tasks that are too
specialised for a plugin such as downloading content or sending
emails. It would be really cool to be able to do something like this
in leiningen.

One idea I had was to have a leingingen plugin in my src directory
with the namespace leingingen.builddb and then simply call:

lein builddb

However, as far as I can tell, the classpath of the compiled source
code is not available to leingingen while building. Another option is
to develop a script plugin and run a command such as:

lein script scripts/builddb.clj

Is this a feature anybody else would be interested in? Are there any
views on what the best approach would be?

Thanks in advance
Saul

Zef Hemel

unread,
Dec 31, 2009, 1:32:11 AM12/31/09
to Clojure
I had exactly the same problem. It would be nice if we could add
directories/jars to the classpath somehow. I noticed the repl task
does this, but it's hacked into the lein script, which is a shame.

A script plug-in would also require the adaptation of the classpath,
so you could branch leiningen itself and add it to the lein script,
but that's not really neat.

Zef

Phil Hagelberg

unread,
Jan 2, 2010, 1:27:32 AM1/2/10
to clo...@googlegroups.com
Saul <sha...@gmail.com> writes:

> Hello,
> I'm moving a small project over from maven built scala to leiningen
> built clojure. One of the features that was available with the maven
> scala plugin was the ability to run scala scripts:
>
> mvn scala:script -DscriptFile=scripts/build_db.scala
>
> This is useful for doing project specific tasks that are too
> specialised for a plugin such as downloading content or sending
> emails. It would be really cool to be able to do something like this
> in leiningen.

For small tasks, you can simply define them in project.clj. Just use with-ns:

(use 'clojure.contrib.with-ns)

(with-ns 'leiningen.build-db
(defn build-db
"Build a DB from scratch."
[project & args]
(do things to build the db)))

Tasks are simply functions named $TASK defined in the leiningen.$TASK
namespace. They take a project arg as well as any further command-line
arguments.

Of course, for anything longer than a few lines you're not going to want
it in project.clj. But you can include a src/leiningen/ for your longer
custom tasks. There shouldn't be any problems with that, but I haven't
tried it myself to confirm.

-Phil

Reply all
Reply to author
Forward
0 new messages