intellij maven build jar

IntelliJ: Maven Build & Jar

By David Rodriguez
AD

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:

AD
Study Spell Jar, Spells Jars, Spell Jar Ideas, Creativity Spell, Witchy Jars, Spell Jars Recipes, Witch Spells, Glamour Spell Jar, Mini Spell Jars
Study Spell Jar, Spells Jars, Spell Jar Ideas, Creativity Spell, Witchy Jars, Spell Jars Recipes, Witch Spells, Glamour Spell Jar, Mini Spell Jars
Full Moon Jar Spell, Spell Jars Witchcraft, Spell Jar Ideas, Mini Spell Jars, Insight Spell Jar, Dreamless Sleep Potion, Jar Magic, Dream Spell, Witches Jar
Full Moon Jar Spell, Spell Jars Witchcraft, Spell Jar Ideas, Mini Spell Jars, Insight Spell Jar, Dreamless Sleep Potion, Jar Magic, Dream Spell, Witches Jar
Maven Essentials for Java Developers | Maven Commands, POM & Dependencies
Maven Essentials for Java Developers | Maven Commands, POM & Dependencies

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.

a man is smiling and pointing at the camera with his finger up in front of him
a man is smiling and pointing at the camera with his finger up in front of him
I built JARVIS from Iron Man with AI (NO CODE!)
I built JARVIS from Iron Man with AI (NO CODE!)
Get Names of Classes Inside a JAR File | Baeldung
Get Names of Classes Inside a JAR File | Baeldung

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.

AD

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:

Maven - IntelliJ IDEA IDE Integration
Maven - IntelliJ IDEA IDE Integration
git days 12
git days 12
10 Best IntelliJ Themes and Color Schemes
10 Best IntelliJ Themes and Color Schemes
the open source stack is shown in this screenshote screen shot from an iphone
the open source stack is shown in this screenshote screen shot from an iphone
Java for Beginners 6 - Tools: IntelliJ Idea, Junit, Maven
Java for Beginners 6 - Tools: IntelliJ Idea, Junit, Maven
the useful tools for building products are displayed in this screenshote image with caption below
the useful tools for building products are displayed in this screenshote image with caption below

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 `` tag:

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

AD

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.

a poster with the words inteluli idea and some type of information on it's side
a poster with the words inteluli idea and some type of information on it's side

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.

AD