Over a million developers have joined DZone.

clojure: lein tar

· Java Zone

Discover how AppDynamics steps in to upgrade your performance game and prevent your enterprise from these top 10 Java performance problems, brought to you in partnership with AppDynamics.

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. 

The Java Zone is brought to you in partnership with AppDynamics. AppDynamics helps you gain the fundamentals behind application performance, and implement best practices so you can proactively analyze and act on performance problems as they arise, and more specifically with your Java applications. Start a Free Trial.

Topics:

Published at DZone with permission of Jay Fields , DZone MVB .

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}