Use leiningen aliases to specify JVM flags
Asked Answered
Z

2

6

How can I specify JVM flags so that they apply only to one alias in a project.clj file?

Specifically I want to try the built in server capability in Clojure 1.8.0.

I can do this with an uberjar and the command:

java -Dclojure.server.interactive="{:port 8411 :accept srv.action/process}" -jar target\uberjar\srv-0.1.0-SNAPSHOT-standalone.jar

But I was hoping I could set that -D... from within a lein alias. I tried this

:aliases {
        "serve" [:jvm-opts ["-Dclojure.server.interactive={:port 8411 :accept srv.action/process}"] "run"]
}

But I get

java.lang.ClassCastException: clojure.lang.Keyword cannot be cast to java.lang.String

Is it possible to do this? I am using "Leiningen 2.6.1 on Java 1.8.0_92 Java HotSpot(TM) 64-Bit Server VM"

Zippy answered 27/6, 2016 at 13:16 Comment(0)
G
8

Leiningen profiles are definitely the way to do this. You can define a profile with any of the usual options, in your case :jvm-opts. In your profile.clj include something similar to the following:

:profiles {:clj-server {:jvm-opts ["-Dclojure.server.interactive={:port 8411 :accept srv.action/process}"]}}

Then you can tell leiningen you wish to use this profile via with-profile.

lein with-profile clj-server run

However, this will only use the options specified in the new profile. If you wish to activate the new profile in addition to the default profiles (dev, test, etc) you need to prepend the profile with a +.

lein with-profile +clj-server run

If you're lazy, like me, you can define an alias to run different tasks utilizing this newly defined profile:

:aliases {"clj-server-run" ["with-profile" "+clj-server" "run"]}

Then it's as easy as calling lein clj-server-run.

Hopefully this helps. I truly recommend reading through the leiningen documentation provided as well as its extremely useful.

Gametophore answered 29/6, 2016 at 10:35 Comment(1)
This is pretty much what I ended up doing. Using a '+' with the profile name is a useful tip.Zippy
Z
3

I found an answer using profiles:

  :profiles {:uberjar {:aot :all}
          :server {:jvm-opts ["-Dclojure.server.interactive={:port 8411 :accept srv.action/process}"]}}
  :aliases {
        "serve" [ "with-profile" "server" "run"]})

I added a 'server' profile, which is allowed to specify its own :jvm-opts and then an alias to run that profile. It can be run with lein serve.

Any comments welcome.

Zippy answered 28/6, 2016 at 12:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.