Clojure program reading its own MANIFEST.MF

我是研究僧i 提交于 2019-12-06 02:56:20

问题


How can a Clojure program find its own MANIFEST.MF (assuming it is packaged in a JAR file).

I am trying to do this from my "-main" function, but I can't find a class to use in the following code:

  (.getValue
    (..
      (java.util.jar.Manifest.
        (.openStream
          (java.net.URL.
            (str
              "jar:"
              (..
                (class **WHAT-GOES-HERE**)
                getProtectionDomain
                getCodeSource
                getLocation)
              "!/META-INF/MANIFEST.MF"))))
      getMainAttributes)
    "Build-number"))

Thanks.


回答1:


This seems to work reliably:

(defn set-version
  "Set the version variable to the build number."
  []
  (def version
    (.getValue (.. (Manifest.
      (.openStream
        (URL.
          (str "jar:"
            (.getLocation
              (.getCodeSource
                (.getProtectionDomain org.example.myproject.thisfile)))
            "!/META-INF/MANIFEST.MF"))))
      getMainAttributes)
      "Build-number")))



回答2:


I find my version a little easier on the eyes:

(defn manifest-map
  "Returns the mainAttributes of the manifest of the passed in class as a map."
  [clazz]
  (->> (str "jar:"
           (-> clazz
               .getProtectionDomain 
               .getCodeSource
               .getLocation)
           "!/META-INF/MANIFEST.MF")
      clojure.java.io/input-stream
      java.util.jar.Manifest.
      .getMainAttributes
      (map (fn [[k v]] [(str k) v]))
      (into {})))

(get (manifest-map MyClass) "Build-Number")



回答3:


Here's a readable version, about as simple as I could get it!

(when-let [loc (-> (.getProtectionDomain clazz) .getCodeSource .getLocation)]
  (-> (str "jar:" loc "!/META-INF/MANIFEST.MF")
      URL. .openStream Manifest. .getMainAttributes
      (.getValue "Build-Number")))



回答4:


There is also clj-manifest that essentially provides this functionality, using the a call similar to other answers found here.




回答5:


I have found an answer that works, however I am open to suggestions for improving it, particularly replacing the call to Class/forName.

(defn -main [& args]
  (println "Version "
    (.getValue
      (..
        (Manifest.
          (.openStream
            (URL.
              (str
                "jar:"
                (..
                  (Class/forName "org.example.myproject.thisfile")
                  getProtectionDomain
                  getCodeSource
                  getLocation)
                "!/META-INF/MANIFEST.MF"))))
        getMainAttributes)
      "Build-number")))


来源:https://stackoverflow.com/questions/2751033/clojure-program-reading-its-own-manifest-mf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!