Leiningen: How to define constants in project.clj?
Asked Answered
S

1

12

I have some values repeated in my project.clj like below:

(defproject foo "0.1.0-SNAPSHOT"
  ...

  :dependencies [[bar "3.4.5"]
                 [baz "3.4.5"]
                 [bat "3.4.5"]]

  ...)

I would like to use some sort of constant here so, when these libraries get updated to 3.5.0 for example, I have to change just one place.

What is the best practise here? Best I can come up with is this:

(def deps-version "3.4.5")

(defproject foo "0.1.0-SNAPSHOT"
  ...

  :dependencies [[bar ~deps-version]
                 [baz ~deps-version]
                 [bat ~deps-version]]

  ...)
Suckerfish answered 29/6, 2015 at 11:43 Comment(5)
I added an answer but thought you were unable to get the constants to work, is this the case? You're missing the ~ char in your example, i.e. [bar ~deps-version]. Your own solution is something I've used outside of leiningen (in gradle for selenium driver versions) so there's only one change to make, and fits well enough.Bushire
so what's the essence of your question now? if it's just "should I use versions like this?" then answer's probably simply, yes.Bushire
@MarkFisher, muhuk is asking what the best practice is. You have stated what you believe the best practice is, so you could offer it as an Answer.Gibbie
which seems a little subjective to me as a 'question'Bushire
@MarkFisher you are right, I probably didn't phrase my question right. But I was wondering if there were other, better ways. To give a somewhat contrived example, you can do #=(eval (...)) and read constants from a file. (Which is perhaps not too absurd since you can make this file a resource and read it within the app too.) Feel free to edit the question BTW, the gist is how to define constants.Suckerfish
F
6

Using a definition before defproject is totally fine and common practice.

You can also so more advanced stuff like depending on the build environment (I would tag the effective project version too that case). A nice example is found in the answers to this question "Leiningen: How to define constants in project.clj?"

Fretwork answered 19/8, 2015 at 15:0 Comment(1)
The link, uh, leads to this same page. To understand recursion one must understand recursion...Maryammaryann

© 2022 - 2024 — McMap. All rights reserved.