Refactor jar and uberjar tasks.

This commit is contained in:
Phil Hagelberg 2010-07-21 23:16:25 -07:00
parent 6c36bbb0da
commit bca8cf3a29
3 changed files with 50 additions and 44 deletions

View file

@ -1,14 +1,13 @@
(ns leiningen.clean
"Remove compiled files and dependencies from project."
(:use [leiningen.jar :only [get-jar-filename get-default-jar-name
get-default-uberjar-name]]
(:use [leiningen.jar :only [get-jar-filename get-default-uberjar-name]]
[clojure.contrib.io :only [file delete-file delete-file-recursively]]))
(defn clean
"Remove compiled files and dependencies from project."
[project]
(println "Cleaning up.")
(doseq [f [(get-jar-filename project (get-default-jar-name project))
(doseq [f [(get-jar-filename project)
(get-jar-filename project (get-default-uberjar-name project))
(:compile-path project)]]
(delete-file-recursively f true)))

View file

@ -66,15 +66,35 @@
(or (:jar-name project)
(str (:name project) "-" (:version project) ".jar")))
(defn get-jar-filename [project jar-name]
(let [jar-dir (:jar-dir project)]
(.mkdirs (file jar-dir))
(str jar-dir "/" jar-name)))
(defn get-jar-filename
([project jar-name]
(let [jar-dir (:jar-dir project)]
(.mkdirs (file jar-dir))
(str jar-dir "/" jar-name)))
([project] (get-jar-filename project (get-default-jar-name project))))
(defn get-default-uberjar-name [project]
(or (:uberjar-name project)
(str (:name project) \- (:version project) "-standalone.jar")))
(defn- filespecs [project]
[{:type :bytes
:path (format "meta-inf/maven/%s/%s/pom.xml"
(:group project)
(:name project))
:bytes (make-pom project)}
{:type :bytes
:path (format "meta-inf/maven/%s/%s/pom.properties"
(:group project)
(:name project))
:bytes (make-pom-properties project)}
(when (and (:resources-path project)
(.exists (file (:resources-path project))))
{:type :path :path (:resources-path project)})
{:type :path :path (:compile-path project)}
{:type :path :path (:source-path project)}
{:type :path :path (str (:root project) "/project.clj")}])
(defn jar
"Create a $PROJECT-$VERSION.jar file containing the compiled .class files as
well as the source .clj files. If project.clj contains a :main symbol, it will
@ -82,24 +102,8 @@ be used as the main-class for an executable jar."
([project jar-name]
(binding [compile/*silently* true]
(compile/compile project))
(let [jar-path (get-jar-filename project jar-name)
filespecs [{:type :bytes
:path (format "meta-inf/maven/%s/%s/pom.xml"
(:group project)
(:name project))
:bytes (make-pom project)}
{:type :bytes
:path (format "meta-inf/maven/%s/%s/pom.properties"
(:group project)
(:name project))
:bytes (make-pom-properties project)}
(when (and (:resources-path project)
(.exists (file (:resources-path project))))
{:type :path :path (:resources-path project)})
{:type :path :path (:compile-path project)}
{:type :path :path (:source-path project)}
{:type :path :path (str (:root project) "/project.clj")}]]
(write-jar project jar-path filespecs)
(let [jar-path (get-jar-filename project jar-name)]
(write-jar project jar-path (filespecs project))
(println "Created" jar-path)
jar-path))
([project] (jar project (get-default-jar-name project))))

View file

@ -5,8 +5,7 @@
[clojure.java.io :only [file copy]]
[clojure.contrib.zip-filter.xml :only [xml-> tag=]]
[leiningen.clean :only [clean]]
[leiningen.jar :only [get-default-jar-name get-jar-filename
get-default-uberjar-name jar]])
[leiningen.jar :only [get-jar-filename get-default-uberjar-name jar]])
(:import [java.util.zip ZipFile ZipOutputStream ZipEntry]
[java.io File FileOutputStream PrintWriter]))
@ -39,6 +38,22 @@
[(into skip-set (copy-entries zipfile out #(skip-set (.getName %))))
(concat components (read-components zipfile))]))
(defn- write-components [deps out]
;; TODO: need documentation; I have no idea what this is for. Alex!
(let [[_ components] (reduce (partial include-dep out)
[#{"META-INF/plexus/components.xml"} nil]
deps)]
(when-not (empty? components)
(.putNextEntry out (ZipEntry. "META-INF/plexus/components.xml"))
(binding [*out* (PrintWriter. out)]
(xml/emit {:tag :component-set
:content
[{:tag :components
:content
components}]})
(.flush *out*))
(.closeEntry out))))
(defn uberjar
"Create a jar like the jar task, but including the contents of each of
the dependency jars. Suitable for standalone distribution."
@ -46,24 +61,12 @@ the dependency jars. Suitable for standalone distribution."
(clean project)
(jar project)
(let [standalone-filename (get-jar-filename project uberjar-name)]
(with-open [out (-> (file standalone-filename)
(FileOutputStream.) (ZipOutputStream.))]
(with-open [out (-> standalone-filename
(FileOutputStream.)
(ZipOutputStream.))]
(let [deps (->> (.listFiles (file (:library-path project)))
(filter #(.endsWith (.getName %) ".jar"))
(cons (file (get-jar-filename
project (get-default-jar-name project)))))
[_ components] (reduce (partial include-dep out)
[#{"META-INF/plexus/components.xml"} nil]
deps)]
(when-not (empty? components)
(.putNextEntry out (ZipEntry. "META-INF/plexus/components.xml"))
(binding [*out* (PrintWriter. out)]
(xml/emit {:tag :component-set
:content
[{:tag :components
:content
components}]})
(.flush *out*))
(.closeEntry out))))
(cons (file (get-jar-filename project))))]
(write-components project out)))
(println "Created" standalone-filename)))
([project] (uberjar project (get-default-uberjar-name project))))