Getting the version of the current clojure project in the repl

后端 未结 8 1603
太阳男子
太阳男子 2020-12-08 20:27

Is it possible to grab the project information within the clojure repl?

For example, if there was a project defined:

(defproject blahproject \"0.1.2\         


        
8条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-08 21:06

    As described in this discussion.

    (ns myproject.example
      (:require [clojure.java.io :as io])
      (:import java.util.Properties))
    
    (defn get-version [dep]
      (let [path (str "META-INF/maven/" (or (namespace dep) (name dep))
                      "/" (name dep) "/pom.properties")
            props (io/resource path)]
        (when props
          (with-open [stream (io/input-stream props)]
            (let [props (doto (Properties.) (.load stream))]
              (.getProperty props "version"))))))
    
    (get-version 'myproject) ; => 0.1.0
    (get-version 'org.clojure/clojure) ; => 1.3.0
    

提交回复
热议问题