IntelliJ: Maven Build & Jar
Ever found yourself wanting to build a JAR file from your Maven project using IntelliJ IDEA? You're not alone. This article guides you through the process, from understanding why you might need to build a JAR file to the straightforward steps to achieve this within IntelliJ.
But first, why build a JAR file? A JAR (Java ARchive) is a file format used by Java to package middleware or reusable software components known as Java libraries. Building a JAR file allows you to create a distributable library that can be easily shared and used in other projects.
Setting Up Your IntelliJ Maven Project
Before we dive into building a JAR file, ensure your project is properly set up in IntelliJ IDEA with Maven. Here are some prerequisites:



Installing and Enabling the Maven Plugin
IntelliJ IDEA requires the Maven plugin to be installed and enabled. You can check this by opening the 'Settings' dialog (Ctrl+Alt+S), navigating to 'Plugins', searching for 'Maven Integration', and ensuring it's enabled.
If it's not installed, click on 'Browse repositories', search for 'Maven Integration', and install it.



Configuring Your Maven Settings
IntelliJ uses the Maven settings from your system's `~/.m2/settings.xml` file. If you have custom settings, ensure they're correctly configured in this file.
You can access this file in IntelliJ by navigating to 'File' > 'Settings' > 'Build, Execution, Deployment' > 'Build Tools' > 'Maven' and clicking on the 'Settings' button.
Building a JAR File with Maven in IntelliJ
Now that your project is properly set up, let's build a JAR file. Here's how:






Creating a Maven Assembly
Before building the JAR file, you need to create a Maven 'assembly' that specifies what should be included in the JAR. In your project's `pom.xml` file, add the following code within the `
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<appendAssemblyId>true</appendAssemblyId>
<assembly>
<id>jar</id>
<includeBaseDirectory>true</includeBaseDirectory>
<fileSet>
<directory>${basedir}/src/main/java</directory>
<outputDirectory>/</outputDirectory>
</fileSet>
</assembly>
</configuration>
</plugin>This creates an assembly with your project's source files. You can customize this to include other files or directories.
Building the JAR File
With the assembly in place, you're ready to build the JAR file. In IntelliJ, open the 'Maven Projects' tool window (View > Tool Windows > Maven), right-click on your project, and select 'Lifecycle' > 'package'. IntelliJ will then build the JAR file located in your project's `target` directory.

Building a JAR file from your Maven project in IntelliJ IDEA is a straightforward process once you understand the basics. As you continue to develop your projects, remember to update your 'assembly' configuration as needed to ensure all necessary files are included in your JAR.




