Getting the version of the current clojure project in the repl

后端 未结 8 1610
太阳男子
太阳男子 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 20:59

    As vemv said, Leiningen project files are just Clojure data. So, it's easy to access your project as an ordinary hash-map:

    (->> "project.clj"
         slurp
         read-string
         (drop 2)
         (cons :version)
         (apply hash-map)
         (def project))
    

    If you need this variable only in your repl, you can add it to repl-options to your project.clj:

    (defproject yourproject "0.1.0"
      :description ""
      :url ""
      :dependencies [ [org.clojure/clojure  "1.4.0"]]
      :repl-options { :init (->>  "project.clj"
                                  slurp
                                  read-string
                                  (drop 2)
                                  (cons :version)
                                  (apply hash-map)
                                  (def project))})
    

    Now, you have project variable in your repl. So, to access the version of your project you can simply type (:version project).

    Of course, you can simply use native Leiningen code to parse you project file:

    (defproject yourproject "0.1.0"
      :description ""
      :url ""
      :dependencies [ [org.clojure/clojure  "1.4.0"]
                      [leiningen-core       "2.1.3"]]
      :repl-options { :init (do (require 'leiningen.core.project)
                                (def project
                                     (leiningen.core.project/read)))})
    

    But, if you need only the version of your project and nothing more, then it's best to use Ankur's solution.

提交回复
热议问题