(blog-of ‘Alex’)

A personal blog about software development and other things. All opinions expressed here are my own, unless explicitly stated.

How to create standalone JAR using maven?

After getting stuck several times with maven assembly plugin I decided to make a note on how to create standalone jar with maven.

Standalone jar allows you to easily distribute your application within single JAR by packing all your dependencies into one. Standalone JAR can be started without specifying extra classpaths.

Here is the sample configuration that does the trick:

<build>
    <plugins>
        <!-- Packaging configuration -->
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>com.mysite.App</mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
        </plugin>
    </plugins>
</build>

Once you have similar configuration for your app, you can create standalone jar via mvn clean package assembly:assembly.

Voila!