views:

125

answers:

2

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.

A: 

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")))
Ralph
A: 

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")))
Ralph