Over a million developers have joined DZone.
Silver Partner

clojure: lein tar

· Java Zone

The Java Zone is brought to you by Stormpath—offering a pre-built identity API for developers. Easily build powerful user management, authentication, and authorization into your web and mobile applications. Check out this tutorial to build a simple web app with Spring Boot and Spring Security in 15 minutes.

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 by Stormpath—offering a pre-built, streamlined user management API for building web and mobile applications. Learn how token authentication with JWTs (JSON Web Tokens) helps secure websites and avoids the pitfalls of using cookies and sessions.

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 }}