Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>[UPDATE April 2013]</strong></p> <p>Leiningen 2, which has been officially released for some time, includes the concept of project templates. By default, Leiningen provides an <code>app</code> template that provides what you need out of the box. Try:</p> <p><code> lein new app my-project </code></p> <p>You will see that Leiningen creates the familiar project template, but also includes:</p> <ul> <li>The default namespace of <code>my-project.core</code> as the <code>:main</code> entry in your <code>project.clj</code> file</li> <li>The <code>:gen-class</code> form in the namespace declaration of <code>my-project.core</code></li> <li>A default <code>-main</code> function in the <code>my-project.core</code> namespace</li> </ul> <p>For those who cannot yet use Leiningen 2, the <a href="https://github.com/Raynes/lein-newnew" rel="noreferrer">lein-newnew</a> plugin provides an equivalent experience under Leiningen 1.</p> <p><strong>[/UPDATE]</strong></p> <p>To build a project that, when run, prints "Hello World!", you'd do as follows (revision of your process above):</p> <h2>Setup</h2> <pre><code>lein new my-project cd my-project lein deps </code></pre> <p>You should now have a basic structure in place and the Clojure jar in your <code>lib</code> folder.</p> <h2>Write a Function</h2> <p>Now edit <code>src/my_project/core.clj</code> with your editor of choice, adding the following below the <code>(ns ...)</code> form:</p> <pre><code>(defn -main [] (println "Hello World!")) </code></pre> <p>This function is inside your <code>my-project.core</code> namespace. To ensure this gets run as your main, let's add a <code>gen-class</code> parameter to your namespace definition at the top, so that it now looks like this at the top of <code>core.clj</code>:</p> <pre><code>(ns my-project.core (:gen-class :main true)) </code></pre> <p>So all together, your <code>core.clj</code> file looks like this:</p> <pre><code>(ns my-project.core (:gen-class :main true)) (defn -main [] (println "Hello World!")) </code></pre> <h2>Configure it as the Main Function</h2> <p>Once you've got <code>src/my_project/core.clj</code> edited as above, you need to tell Leiningen (the build tool) where the "main" function for your project lives. Here's an example <code>defproject</code> form that does this:</p> <pre><code>(defproject my-project "1.0.0-SNAPSHOT" :description "My Project" :dependencies [[org.clojure/clojure "1.2.1"]] :main my-project.core) </code></pre> <p>Now the <code>-main</code> function inside <code>my-project.core</code> becomes the entry-point for your program.</p> <h2>Run It</h2> <p>You can now have two options for running this project:</p> <ul> <li>Use <code>lein run</code> at the command-line while at the root of your <code>my-project</code> project</li> <li>Create a standalone jar file by running <code>lein uberjar</code>. You can then run the resultant jar file by running <code>java -jar my-project-1.0.0-SNAPSHOT-standalone.jar</code></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload