Jay Fields is a software developer at DRW Trading. He has a passion for discovering and maturing innovative solutions. His most recent work has been in the Domain Specific Language space where he's delivered applications that empowered subject matter experts to write the business rules of the applications. He is also very interested in maturing software design through software testing. Jay is a DZone MVB and is not an employee of DZone and has posted 115 posts at DZone. You can read more from them at their website. View Full User Profile

clojure: lein tar

10.15.2012
| 2160 views |
  • submit to reddit

A co-worker recently asked how I package and deploy my clojure code. There's nothing special about the code, but I'm making it available here for anyone who wants to cut and paste. Deploy is the easy part - scp a tar to the prod box. Building the tar is very easy as well. I've run this on a few different linux distros without issue, but YMMV. Without further ado.

(ns leiningen.tar
  (:require leiningen.jar leiningen.clean
            [clojure.java.io :as io])
  (:use clojure.java.shell))

(defn normalize-path [root path]
  (let [f (io/file path)]
    (.getAbsolutePath (if (.isAbsolute f) f (io/file root path)))))

(defn tar [project & args]
  (let [build-version (-> (System/getenv "BUILD_VERSION")
                          (or (first args))
                          (or "0.0.0.0"))
        release (str (:description project) "-" build-version)
        build-dir (normalize-path (:root project) (str "deploy/" release))
        jars-dir (normalize-path build-dir "jars")
        jar-file (leiningen.jar/jar project)
        tar-file (clojure.java.io/file (:root project) (str release ".tar.gz"))]
    (.delete tar-file)
    (sh "rm" "-rf" build-dir)
    (sh "mkdir" "-p" jars-dir)
    (sh "cp" "-R" (normalize-path (:root project) "src/web") build-dir)
    (sh "cp" "-R" (normalize-path (:root project) "lib/jars") build-dir)
    (sh "mv" jar-file jars-dir)
    (sh "tar" "--gzip" "--create"
      (str "--file=" (.getName tar-file))
      (str "--directory=" (normalize-path (:root project) "deploy/")) release)
    (println "Wrote" (.getName tar-file))))

I'm sure there are easier ways, and I know I could do it programically - but this works and is easy to maintain. That's good enough for me. 

Published at DZone with permission of Jay Fields, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)