Want this course?
Creating JARs with Leiningen
Course: JVM Fundamentals for Clojure
We learn how to create JARs and UberJARs with Leiningen.
All right, so we're now looking at how to generate at JAR file, using Leiningen. And it's all about, there's two commands and a few configuration options that you have to set up and it does everything for you. So, this is an application called nord, that I deploy it as an uberjar because it's an application, and I want to be able one file that I zip around, that's got all the dependencies in it. So, I make an uberjar. Typically, when you're doing a library, you don't want to ship around all the dependencies, you just want the code. You don't even want it compiled in there, you just want the clojure code. So, we're going to look at configuring this to do both of those.
All right, so, if you want to generate a library, if you want to make a library to like, to put onto clojars or something, as a dependency, you should use the jar. And I'm calling it nord-skinny, why don't I call it nord-library. And the library is... Is, is, going to not have anything compiled in it. It doesn't have clojure source code.
The uberjar, I wanted to have it compiled because that means it will load faster. It doesn't have to read in the clojure source code, compile it, and then run it, it can just load up class files right away, when it starts up. And so, what I have to do, to do that, is tell it: uberjar aot all. So, when I run the command in Leiningen to make an uberjar, it's going to run under the uberjar profile.
So, if I set aot ahead of time, compile to all its going to compile all the closure sources into, uh, into into byte code. Into java byte code. And then those class files and those are all going to be in class files. Those are going to be put into the uberjar directly and they can run directly. Uh, the other thing I'm setting is the main. So this can be an executable jar. Uh, there's one last thing we have to do. So you have to open up that nord core. And I have to add the gen-class to that name space. And if we look we'll see that there is main function in there. So this will be able to be run as an executable jar file.
Okay, let's skip back to the project and then we'll go to the terminal, okay. So we see here it's just got the normal stuff for a closure project. It's got the project clj. It's got our source directory. It's got our resources. What it doesn't have, right now, is a target directory. So if I do lein jar. Now notice it's compiling nord dot core. The reason it's compiling that is because that is has got the main option set for it. Where it's only compiling one namespace, nord dot core. That means that this an executable jar. You can run it from the command line. But it doesn't have all the dependencies in it, so you better be sure to have all the dependencies on the class path when this runs.
Okay, let's take a look at... That new directory it just created and see this is a 2.4 megabyte jar file. And now let's do the uberjar. Which is what I typically do for this. And we'll see the difference. See already it is compiled this one, extra. And it's compiling all these other namespaces that are in that project. It creates that library jar again. So it will overwrite whatever was there, but notice it created that one corresponding to this option. Uberjar name nord dot jar. Okay, so that's the one that is the uberjar. So let's do... Let's look at those. So we still have this one. That got over written, 2.4 megabytes. And now the one with all the dependencies, is 10 megabytes, okay.
The static stuff that I need. All my icons and stuff. They're all in that jar file. Now I think they're in the skinny one too. Not skinny, library I mean. Right, they're in there too. All the resources get bundled in there. Which is probably why it's so big, 2.4 megabytes. So that's Leiningen. There's, you know, you just configure it the way you want it. It gets set and it gets run every time. I do wanna give one tip. Don't name your jar and the uberjar the same. Money it doesn't like that.