I've been learning clojure on the REPL environment with Clojure Box.
How can I make an exe开发者_高级运维cutable file (.jar)?
I'm wondering if something like this is possible:
write clojure code on notepad and name it project.clj
compile project.clj
get executable file
Step #2 doesn't have to be done in command line. IDE is fine.
As Erhan Bagdemir answered, you should use Leiningen. Here's how I do it on Windows:
Install Leiningen on Windows
You're going to download the Leiningen script and put it somewhere on your PATH
. If you have a folder already on your path that you use for executable scripts, if not, do the following:
- Download the Leiningen package from Github.
- Unzip it in your home folder (e.g.
/Users/vikbehal
) - Add the folder that was created by unzipping to your
PATH
. To do this, open the Control Panel, and go to the System area where you can edit your Environment Variables. Edit either the system or your user'sPATH
value by adding/Users/vikbehal/lein
(or whatever the folder is called) to yourPATH
, making sure to keep that entry separated from others by a semicolon (;
). - Press
Ok
to save your changes.
You can verify that you've successfully installed Leiningen by opening up a command prompt and running lein help
. You should see Leiningen installing some initial files. If you get an error, make sure you've properly added the folder where Leiningen lives to your PATH
.
Use Leiningen to Develop
Leiningen gives you a good default directory structure, handles dependency management, and ensures that Java's classpath is set properly when running a REPL or, in this case, jarring your application.
Here's a run-down of Leiningen standard commands:
# Start a blank project
lein new my-project
# Pull down dependencies defined in your project.clj file
lein deps
# Create a "library" jar of your project
lein jar
# Create an executable jar of your project
lein uberjar
It looks like that last command is what you want, right? First, you need to configure a few things, so that Leiningen can build your executable jar properly.
Configuring for an Executable/Standalone Jar
You need to do three things to make lein uberjar
work properly:
- Write a
-main
function that will act as the "entry point" to the execution of your jar file. (Note the-
, it denotes that this function is more like a "Java method" than a regular Clojure function.) - Make sure you add a
(:gen-class :main true)
declaration in thens
namespace declaration at the top of the file that contains your-main
function. - Add a
:main my.main.namespace
entry in yourproject.clj
Now run lein uberjar
. While developing, you can alternatively use lein run
if you want to test how your application would behave as an executable jar, but don't want to explicitly re-jar every time you make a change.
Use Leiningen to create an uberjar.
with Leiningen:
http://www.bagdemir.com/2011/07/04/creating-new-projects-with-leiningen/
Just for variety I though I would mention the maven clojure plugin for those who want to keep to their Java roots.
精彩评论