- Why not make the jar available at compile time as well? (e.g. maven
scope="provided")

- You could also use a delay:

(def *version* (delay (get-version)))

and then write @*version* when you want the version information at runtime.

- Equivalently, you could memoize get-version and just call that at
runtime whenever you want the version.

(def get-version (memoize (fn [] (-> (str "jar:" ... )))))

// Ben


On Sat, Oct 23, 2010 at 14:53, Ralph <grkunt...@gmail.com> wrote:
> Won't work. The "def" gets executed at compile time, before the JAR
> file exists.
>
> On Oct 22, 3:55 pm, ataggart <alex.tagg...@gmail.com> wrote:
>> (defn get-version []
>>   (-> (str "jar:" (-> my.ns.name (.getProtectionDomain)
>>                                  (.getCodeSource)
>>                                  (.getLocation))
>>                   "!/META-INF/MANIFEST.MF")
>>     (URL.)
>>     (.openStream)
>>     (Manifest.)
>>     (.. getMainAttributes)
>>     (.getValue "Build-number")))
>>
>> (def *version* (get-version))
>>
>> On Oct 22, 11:56 am, Ralph <grkunt...@gmail.com> wrote:
>>
>>
>>
>> > I have a Clojure program that I build as a JAR file using Maven.
>> > Embedded in the JAR Manifest is a build-version number, including the
>> > build timestamp.
>>
>> > I can easily read this at runtime from the JAR Manifest using the
>> > following code:
>>
>> > (defn set-version
>> >   "Set the version variable to the build number."
>> >   []
>> >   (def version
>> >     (-> (str "jar:" (-> my.ns.name (.getProtectionDomain)
>> >                                    (.getCodeSource)
>> >                                    (.getLocation))
>> >                     "!/META-INF/MANIFEST.MF")
>> >       (URL.)
>> >       (.openStream)
>> >       (Manifest.)
>> >       (.. getMainAttributes)
>> >       (.getValue "Build-number"))))
>> > but I've been told that it is bad karma to use def inside defn.
>>
>> > What is the Clojure-idiomatic way to set a constant at runtime? I
>> > obviously do not have the build-version information to embed in my
>> > code as a def, but I would like it set once (and for all) from the
>> > main function when the program starts. It should then be available as
>> > a def to the rest of the running code.
>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure@googlegroups.com
> Note that posts from new members are moderated - please be patient with your 
> first post.
> To unsubscribe from this group, send email to
> clojure+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to