tools-build

2023-01-08T20:42:02.737879Z

Hi 👋 ! So far I never needed to compile my application, but now I would like to compile it ahead-of-time (mainly to Dockerize it). I have followed the instructions in the tools.build docs to setup a build.clj and the appropriate steps to assemble an uber-jar. I was a bit surprised by the fact that def statements are actually executed during the compilation, but that’s just how it works right? I get this by now. But, my question that I could not answer by researching so far is, how to work around it? I just have a couple of defs, e.g. to have an atom holding the database connection and another one for some Compojure routes and similar. It cannot really be best practice to require having a database server ready during compilation or can it?

Alex Miller (Clojure team) 2023-01-08T20:43:33.455339Z

The best practice is to not store state in top level defs

Alex Miller (Clojure team) 2023-01-08T20:45:59.307809Z

So you can either have some function that creates the server and passes it on, or initializes the atom, or use something like wrapping it in a delay and deref on use (so it’s not immediately created)

âž• 1
dpsutton 2023-01-08T20:53:53.665899Z

> would like to compile it ahead-of-time (mainly to Dockerize it). Want to push back on this a bit and say that compiling the clj files to class files into the uberjar is not necessary. And I don't see how a container would change this in any way

jumar 2023-01-09T05:52:55.157989Z

I find AOT compilation useful, especially in terms of startup performance. It is not necessary but a default choice for all our production apps

👍 1
1
2023-01-09T20:50:45.867879Z

> So you can either have some function that creates the server and passes it on, or initializes the atom Thanks a lot! That worked out relatively smooth.

2023-01-09T20:51:50.367409Z

> compiling the clj files to class files into the uberjar is not necessary. And I don’t see how a container would change this in any way Sorry, this was not well formulated. I also used depstar before and it did not compile aot. I think I was under the impression it’s necessary with tools.build. But probably not even that, I just did not find an example how to do it. Thanks for the advice.