Embed version string from leiningen project in application
Asked Answered
T

5

14

I want to be able to query or embed the version string set by the leiningen project and display that value to user. Uses include displaying the version string from the CLI, or as a header in a ring application. Is this possible?

Tinaret answered 27/6, 2012 at 22:4 Comment(0)
B
10

If you're running inside Leiningen using lein run or the like, it's available as a system property: (System/getProperty "myproject.version"). If you're producing a jar you need to read pom.properties.

Brause answered 28/6, 2012 at 5:9 Comment(0)
W
7

I like technomancy's answer, but I had to go look up how to "read pom.properties". It turns out that for the maven artifact com.example/my-project, there is a file on the classpath under

META-INF/maven/com.example/my-project/pom.properties

which you can read into a java.util.Properties and get out a "version" key.

(ns com.example.version
  (:require [clojure.java.io :as io])
  (:import (java.util Properties)))

(defn read-project-version [groupid artifact]
  (-> (doto (Properties.)
        (.load (-> "META-INF/maven/%s/%s/pom.properties"
                 (format groupid artifact)
                 (io/resource)
                 (io/reader))))
    (.get "version")))
Weeper answered 26/10, 2015 at 19:38 Comment(1)
Tip: if groupid is not specified in your project, leiningen sets it the same as the project name, so "META-INF/maven/my-project/my-project/pom.properties" does the jobCallum
V
3

Someone has written a nice library for handling this: https://github.com/trptcolin/versioneer

It works for lein projects, both running in lein and from an uberjar. Use it like the following (taken from the Github page):

Add this to your project.clj:

[trptcolin/versioneer "0.2.0"]

Then, in your code, do something like this, where GROUP-ID and ARTIFACT-ID are the usual Leiningen/Maven identifiers for your project.

user=> (require '[trptcolin.versioneer.core :as version])
nil
user=> (version/get-version "GROUP-ID" "ARTIFACT-ID")
"1.2.3-SNAPSHOT"
Virile answered 23/9, 2016 at 13:42 Comment(0)
S
1

if you build a jar file you can extract the current version from the jar file.

otherwise if you are not doing AOT then you can read the project.clj file (because it's a map) and then extract the version.

I certainty hope there is a more elegant solution short of writing a leiningen plugin for this (unless someone else wants to write this, hint hint..)

Sofer answered 27/6, 2012 at 22:30 Comment(1)
Unfortunatly it appears to not work, the following returns nil, when called using java on an uberjar from an AOT class: (println "blargl" (.getSpecificationVersion (.getPackage (class *ns*))) "\n"))Tinaret
A
0

Here is how I read my project name and version, note that the project.clj file is read during compilation and jar file simply contains the string:

(defmacro proj-version []
  (some->> (slurp "project.clj") clojure.edn/read-string (drop 1) (take 2) (str/join " ")))

(defn -main []
  (println (proj-version))

so for (defproject abc "1.2.3" ..., when you run -main it will print:

abc 1.2.3
Architectural answered 20/1, 2017 at 3:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.