Leiningen と profile を使って設定ファイルを切り替える
Leiningen と profile を使って設定ファイルを切り替える
Leiningen には profile を切り替える機能がある。 profile を切り替えることで本番用の設定と切り替えたりできるのだが、具体例が少ないように思えたので書いておく。
開発時に使える dev-resources
ドキュメントに出ているのかよくわからないがデフォルトの profile では dev-resources が使用出来る。
デフォルトの確認
pprint で見てみる。
$ lein pprint
結果:
{:compile-path "/home/ma2/lib/codebox/clojure/projects/prof/target/classes", :group "prof", :license {:name "Eclipse Public License", :url "http://www.eclipse.org/legal/epl-v10.html"}, :global-vars {}, :checkout-deps-shares [:source-paths :test-paths :resource-paths :compile-path #<Var@77b52d12: #<classpath$checkout_deps_paths leiningen.core.classpath$checkout_deps_paths@47f6473>>], :dependencies ([org.clojure/clojure "1.5.1"] [sonian/carica "1.0.3" :exclusions ([cheshire/cheshire])] [org.clojure/tools.nrepl "0.2.3" :exclusions ([org.clojure/clojure])] [clojure-complete/clojure-complete "0.2.3" :exclusions ([org.clojure/clojure])]), :plugin-repositories [["central" {:snapshots false, :url "http://repo1.maven.org/maven2/"}] ["clojars" {:url "https://clojars.org/repo/"}]], :test-selectors {:default (constantly true)}, :target-path "/home/ma2/lib/codebox/clojure/projects/prof/target", :name "prof", :deploy-repositories [["clojars" {:username :gpg, :url "https://clojars.org/repo/", :password :gpg}]], :root "/home/ma2/lib/codebox/clojure/projects/prof", :offline? false, :source-paths ("/home/ma2/lib/codebox/clojure/projects/prof/src"), :certificates ["clojars.pem"], :version "0.1.0-SNAPSHOT", :jar-exclusions [#"^\."], :prep-tasks ["javac" "compile"], :url "http://example.com/FIXME", :repositories [["central" {:snapshots false, :url "http://repo1.maven.org/maven2/"}] ["clojars" {:url "https://clojars.org/repo/"}]], :resource-paths ("/home/ma2/lib/codebox/clojure/projects/prof/dev-resources" "/home/ma2/lib/codebox/clojure/projects/prof/resources"), :aot :all, :uberjar-exclusions [#"(?i)^META-INF/[^/]*\.(SF|RSA|DSA)$"], :main prof.core, :jvm-opts ["-XX:+TieredCompilation" "-XX:TieredStopAtLevel=1"], :eval-in :subprocess, :plugins ([lein-pprint/lein-pprint "1.1.1"] [lein-environ/lein-environ "0.4.0"] [lein-midje/lein-midje "3.0.0"] [lein-ancient/lein-ancient "0.4.4"] [lein-ritz/lein-ritz "0.7.0"]), :native-path "/home/ma2/lib/codebox/clojure/projects/prof/target/native", :description "FIXME: write description", :test-paths ("/home/ma2/lib/codebox/clojure/projects/prof/test"), :clean-targets [:target-path], :aliases nil}
確かに resource-paths の先頭に dev-resources が追加されている。 dev-resources はデフォルトの leiningen のテンプレートでは作成されないので自分で用意する必要がある。
実際に切り替えてみよう
実際に切り替えてみよう。 構成は
. ├── LICENSE ├── README.md ├── dev-resources │ └── config.clj ├── doc │ └── intro.md ├── project.clj ├── resources │ ├── config.clj │ └── test.properties ├── src │ └── prof │ └── core.clj └── test └── prof └── core_test.clj
dev-resources/config.clj :
{:name "TEST"}
resources/config.clj :
{:name "production"}
main 関数で表示してみる。
(ns prof.core (:gen-class) (:use [carica.core])) (defn -main [& args] (println (config :name)))
設定を carica で読み込んでどちらが表示されるか確認する。
$ lein run Compiling prof.core TEST
デフォルトでは開発リソースである dev-resources の設定が読み込まれてい る。 他のプロファイルに切り替えてみる。
$ lein with-profile production run Performing task 'run' with profile(s): 'production' production
デフォルトの profile 以外では dev-resources ではなく resources が優先 になっている。 結果、開発時には dev-resources を使えばよい。 実際には何も指定してない場合には :base プロファイルが使われている。 dev-resources は :base プロファイルで設定されているのでそのまま使用できる。
uberjar
配布時にはどうなるのか? uberjar で試してみる。
$ lein uberjar Created /home/ma2/lib/codebox/clojure/projects/prof/target/prof-0.1.0-SNAPSHOT.jar Created /home/ma2/lib/codebox/clojure/projects/prof/target/prof-0.1.0-SNAPSHOT-standalone.jar $ java -jar target/prof-0.1.0-SNAPSHOT-standalone.jar production
uberjar の場合は :uberjar プロファイルがデフォルトで使われるので dev-resources は有効にならないようだ。 開発版を配布したい場合には明示的に指定することで対応できる。 :base プロファイルを追加してみる。
$lein with-profile +base uberjar Performing task 'uberjar' with profile(s): 'default,base' Warning: skipped duplicate file: config.clj Created /home/ma2/lib/codebox/clojure/projects/prof/target/prof-0.1.0-SNAPSHOT.jar Created /home/ma2/lib/codebox/clojure/projects/prof/target/prof-0.1.0-SNAPSHOT-standalone.jar $ java -jar target/prof-0.1.0-SNAPSHOT-standalone.jar TEST
無事開発版の設定ファイルが読み込まれている。