开发者

How do I create an executable file in clojure?

开发者 https://www.devze.com 2023-02-15 04:19 出处:网络
I\'ve been learning clojure on the REPL environment with Clojure Box. How can I make an exe开发者_高级运维cutable file (.jar)?

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:

  1. write clojure code on notepad and name it project.clj

  2. compile project.clj

  3. 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:

  1. Download the Leiningen package from Github.
  2. Unzip it in your home folder (e.g. /Users/vikbehal)
  3. 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's PATH value by adding /Users/vikbehal/lein (or whatever the folder is called) to your PATH, making sure to keep that entry separated from others by a semicolon (;).
  4. 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:

  1. 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.)
  2. Make sure you add a (:gen-class :main true) declaration in the ns namespace declaration at the top of the file that contains your -main function.
  3. Add a :main my.main.namespace entry in your project.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.

0

精彩评论

暂无评论...
验证码 换一张
取 消