Fork me on GitHub
#tools-build
<
2023-01-08
>
Hachmaninow20:01:02

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)20:01:33

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

Alex Miller (Clojure team)20:01:59

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)

2
dpsutton20:01:53

> 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

jumar05:01:55

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

👍 2
t-hanks 2
Hachmaninow20:01:45

> 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.

Hachmaninow20:01:50

> 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.