{{ !articles[0].partner.isSponsoringArticle ? "Platinum" : "Portal" }} Partner
java,languages,tips and tricks,clojure

clojure: lein tar

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 ""))
        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 {{ articles[0].authors[0].realName }}, DZone MVB. (source)

Opinions expressed by DZone contributors are their own.

{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks