use composite profiles for :default

You can now overide :default in your project.clj to change what profiles are
active when none are specified. As part of this change, default is no longer
used to for built-in settings; these are now stored in the :base profile.
This makes it possible to override :default while still including the :base
profile.
This commit is contained in:
Justin Balthrop 2012-07-18 17:20:18 -07:00
parent c36274b52e
commit b41db4d03c
5 changed files with 41 additions and 22 deletions

View file

@ -7,7 +7,7 @@ without including them in the jar, or you may want to have Swank
Clojure available in every project you hack on without modifying every
single project.clj you use.
By default the `:dev`, `:user`, and `:default` profiles are activated
By default the `:dev`, `:user`, and `:base` profiles are activated
for each task, but the settings they provide are not propagated
downstream to projects that depend upon yours. Each profile is defined
as a map which gets merged into your project map.
@ -74,19 +74,6 @@ vectors even though they behave like maps (because it only makes sense
to have a single version of a given dependency present at once). The
replace/displace metadata hints still apply though.
## Composite Profiles
Sometimes it is useful to define a profile as a combination of other
profiles. To do this, just use a vector instead of a map as the profile value.
This can be used to avoid duplication:
```clj
{:shared {:port 9229, :protocol \"https\"}
:qa [:shared {:servers [\"qa.mycorp.com\"]}]
:stage [:shared {:servers [\"stage.mycorp.com\"]}]
:production [:shared {:servers [\"prod1.mycorp.com\", \"prod1.mycorp.com\"]}]}
```
## Activating Profiles
Another use of profiles is to test against various sets of dependencies:
@ -114,6 +101,24 @@ Multiple profiles may be executed in series with colons:
$ lein with-profile 1.3:1.4 test :database
## Composite Profiles
Sometimes it is useful to define a profile as a combination of other
profiles. To do this, just use a vector instead of a map as the profile value.
This can be used to avoid duplication:
```clj
{:shared {:port 9229, :protocol \"https\"}
:qa [:shared {:servers [\"qa.mycorp.com\"]}]
:stage [:shared {:servers [\"stage.mycorp.com\"]}]
:production [:shared {:servers [\"prod1.mycorp.com\", \"prod1.mycorp.com\"]}]}
```
Composite profiles are used by Leiningen internally for the `:default` profile,
which is the profile used if you don't change it using `with-profile`. The
`:default` profile is defined to be a composite of `[:dev :user :base]`, but you
can change this in your `project.clj` just like any other profile.
## Debugging
To see how a given profile affects your project map, use the

View file

@ -149,11 +149,12 @@
(def default-profiles
"Profiles get merged into the project map. The :dev and :user
profiles are active by default."
(atom {:default {:resource-paths ["dev-resources"]
:plugins [['lein-newnew "0.3.4"]]
:checkout-deps-shares [:source-paths
:resource-paths
:compile-path]}
(atom {:default [:dev :user :base]
:base {:resource-paths ["dev-resources"]
:plugins [['lein-newnew "0.3.4"]]
:checkout-deps-shares [:source-paths
:resource-paths
:compile-path]}
:leiningen/test {:injections [hooke-injection]}
:update {:update :always}
:offline {:offline? true}
@ -346,5 +347,5 @@
(ns-unmap 'leiningen.core.project 'project)
(-> (reduce apply-middleware @project (:middleware @project))
(merge-profiles profiles)))))
([file] (read file [:dev :user :default]))
([file] (read file [:default]))
([] (read "project.clj")))

View file

@ -149,6 +149,19 @@
(merge-profiles [:a])
(dissoc :profiles))))))
(deftest test-override-default
(let [expected-result {:A 1, :B 2, :C 3
:repositories {"central" {:url "http://repo1.maven.org/maven2"}
"clojars" {:url "https://clojars.org/repo/"}}
:dependencies [], :compile-path "classes"}]
(is (= expected-result
(-> {:profiles {:a {:A 1 :B 2}
:b {:B 2 :C 2}
:c {:C 3}
:default [:c :b :a]}}
(merge-profiles [:default])
(dissoc :profiles))))))
(deftest test-unmerge-profiles
(let [expected-result {:A 1 :C 3 :profiles {:a {:A 1}
:b {:B 2}

View file

@ -195,7 +195,7 @@ Create a $PROJECT-$VERSION.jar file containing project's source files as well
as .class files if applicable. If project.clj contains a :main key, the -main
function in that namespace will be used as the main-class for executable jar."
[project]
(let [project (project/unmerge-profiles project [:default :dev :user])]
(let [project (project/unmerge-profiles project [:default])]
(eval/prep project)
(let [jar-file (get-jar-filename project)]
(write-jar project jar-file (filespecs project []))

View file

@ -88,7 +88,7 @@ as well as defining a -main function."
(with-open [out (-> standalone-filename
(FileOutputStream.)
(ZipOutputStream.))]
(let [project (project/unmerge-profiles project [:default :dev :user])
(let [project (project/unmerge-profiles project [:default])
deps (->> (classpath/resolve-dependencies :dependencies project)
(filter #(.endsWith (.getName %) ".jar")))
jars (cons (io/file (jar/get-jar-filename project)) deps)]