Create JAR File With IntelliJ

I’ve recently been doing some work with Java and Groovy for the first time in a year and some change. I’ve historically done some random scripting in Groovy, but I’m now at the point where I’m making some libraries that I can use in other places and greatly simplify some areas of my (coding) life. I’m typically someone who mainly writes code with tools like Neovim, Sublime, and VS Code. Unfortunately, at least in my experience, a lot of Java and Groovy-specific things don’t work so well in these editors, especially when compared to a dedicated IDE. This is even pretty apparent when looking at the IDE plugin feature matrix for Groovy:

The link to Netbeans on this page doesn’t even work anymore…

After some testing with different editors, I quickly found IntelliJ to be the best solution. Everything works extremely well with it, and my Groovy code is already orders of magnitude better than it was when I was muddling about with Groovy in VS Code back in the day. IntelliJ also makes it very easy to keep my Groovy and Java versions in alignment for each project. However, the difference between an IDE like IntelliJ and an editor is that there’s a lot of extra tooling in the IDE. It took me some time to figure out how to do things like take a local JAR file and add that to my project as a dependency, for example. My biggest struggle, though, was figuring out how to take the code I had been working on for a library and create a JAR file I could use elsewhere. I’m writing it up here in the hopes that it helps someone else getting started with IntelliJ… even if that “someone else” is me in the future on a new laptop.

The first step is to go to File > Project Settings. In the new window that appears, go to Project Settings > Artifacts > + > JAR > From modules with dependencies.

If the library in question needs a Main class (e.g. if the library itself needs to be directly executable), one can be specified in the next pop-up. Otherwise, just ignore everything and click the OK button.

This has specified a build artifact for the project, but that doesn’t automatically build it. For that, go to Build > Build Artifacts. On the small pop-up, click Build, and this will create the JAR file.

It can be found in the project’s folder structure under out > artifacts.