Skip to content
Bill La Forge edited this page Jan 19, 2016 · 15 revisions

Snippets

Access a version string at runtime, from a jar

Often it is desirable to print the version of the source used to build the jar. This may be a version string or a git hash. One way to achieve this is to add the version number to a file in the classpath:

;; in build.boot

(def version "1.0")

;; or from the environment
;; (def version (or (System/getenv "CIRCLE_SHA1") "SNAPSHOT"))

(deftask add-version-txt []
  (with-pre-wrap fs
    (let [t (tmp-dir!)]
      (spit (clojure.java.io/file t "version.txt") version)
      (-> fs (add-resource t) commit!))))

(deftask dist []
  (comp (pom :project 'my-project
             :version version)
        (add-version-txt)
        (uber)
        (aot :namespace '#{my-project.core})
        (jar :main 'my-project.core)))
;; in your app code

(defn get-version []
   (some-> "version.txt" clojure.java.io/resource slurp clojure.string/trim))

Accessing test resources

In lein, you just create a dev-resources directory for your testing resources. Boot is almost as easy. Just as you need to add the source files you use for testing to the source-paths, you also need to add to the resource-paths:

(deftask testing
  "Profile setup for running tests."
  []
  (set-env! :source-paths #(conj % "test/clj"))
  (set-env! :resource-paths #(conj % "dev-resources"))
  identity)
Clone this wiki locally