Skip to content
Fix Code Error

How can I create an executable JAR with dependencies using Maven?

March 13, 2021 by Code Error
Posted By: soemirno

I want to package my project in a single executable JAR for distribution.

How can I make a Maven project package all dependency JARs into my output JAR?

Solution

<build>
  <plugins>
    <plugin>
      <artifactId>maven-assembly-plugin</artifactId>
      <configuration>
        <archive>
          <manifest>
            <mainClass>fully.qualified.MainClass</mainClass>
          </manifest>
        </archive>
        <descriptorRefs>
          <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
      </configuration>
    </plugin>
  </plugins>
</build>

and you run it with

mvn clean compile assembly:single

Compile goal should be added before assembly:single or otherwise the code on your own project is not included.

See more details in comments.


Commonly this goal is tied to a build phase to execute automatically. This ensures the JAR is built when executing mvn install or performing a deployment/release.

<plugin>
  <artifactId>maven-assembly-plugin</artifactId>
  <configuration>
    <archive>
      <manifest>
        <mainClass>fully.qualified.MainClass</mainClass>
      </manifest>
    </archive>
    <descriptorRefs>
      <descriptorRef>jar-with-dependencies</descriptorRef>
    </descriptorRefs>
  </configuration>
  <executions>
    <execution>
      <id>make-assembly</id> <!-- this is used for inheritance merges -->
      <phase>package</phase> <!-- bind to the packaging phase -->
      <goals>
        <goal>single</goal>
      </goals>
    </execution>
  </executions>
</plugin>
Answered By: Unanswered

Related Articles

  • Maven2: Missing artifact but jars are in place
  • Building executable jar with maven?
  • Plugin org.apache.maven.plugins:maven-clean-plugin:2.5 or…
  • UnsatisfiedDependencyException: Error creating bean with…
  • Failed to execute goal…
  • Specifying java version in maven - differences between…
  • npm install error in vue
  • Why am I getting a "401 Unauthorized" error in Maven?
  • Unable to Build using MAVEN with ERROR - Failed to execute…
  • Spring Boot - Cannot determine embedded database driver…

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

Generating random whole numbers in JavaScript in a specific range?

Next Post:

How can I install packages using pip according to the requirements.txt file from a local directory?

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Get code errors & solutions at akashmittal.com
© 2022 Fix Code Error