To create an executable JAR file that includes all dependencies (an “uber-JAR”) using Maven, you can use plugins like the Maven Shade Plugin or Maven Assembly Plugin. Below are step-by-step instructions for both approaches:
Method 1: Using the Maven Shade Plugin (Recommended)
The Maven Shade Plugin is the most common and modern way to bundle dependencies into a single JAR.
Step 1: Add the Shade Plugin to pom.xml
Include the following configuration in your project’s pom.xml file:
<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-shade-plugin</artifactId>
      <version>3.5.1</version>
      <executions>
        <execution>
          <phase>package</phase>
          <goals>
              <goal>shade</goal>
          </goals>
          <configuration>
              <transformers>
                  <!-- Specify the main class -->
                  <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                      <mainClass>com.yourpackage.MainClass</mainClass>
                  </transformer>
              </transformers>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>Step 2: Replace com.yourpackage.MainClass
- Replace com.yourpackage.MainClasswith the fully qualified name of your application’s main class (the one containing thepublic static void main(String[] args)method).
Step 3: Build the JAR
Run the following command in your terminal:
mvn clean packageStep 4: Run the JAR
The executable JAR will be generated in the target directory. Run it with:
java -jar target/your-project-name-version-shaded.jarMethod 2: Using the Maven Assembly Plugin
The Maven Assembly Plugin is an older approach but still valid.
Step 1: Add the Assembly Plugin to pom.xml
<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-assembly-plugin</artifactId>
      <version>3.7.1</version>
      <configuration>
          <!-- Specify the main class -->
          <archive>
              <manifest>
                  <mainClass>com.yourpackage.MainClass</mainClass>
              </manifest>
          </archive>
          <descriptorRefs>
              <!-- This bundles all dependencies into one JAR -->
              <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
      </configuration>
      <executions>
          <execution>
              <phase>package</phase>
              <goals>
                  <goal>single</goal>
              </goals>
          </execution>
      </executions>
    </plugin>
  </plugins>
</build>Step 2: Replace com.yourpackage.MainClass
- Replace com.yourpackage.MainClasswith your main class.
Step 3: Build the JAR
mvn clean packageStep 4: Run the JAR
The JAR will be named your-project-name-version-jar-with-dependencies.jar:
java -jar target/your-project-name-version-jar-with-dependencies.jarMethod 3: Spring Boot Applications
For Spring Boot projects, use the Spring Boot Maven Plugin:
Step 1: Add the Spring Boot Plugin to pom.xml
<build>
  <plugins>
    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
  </plugins>
</build>Step 2: Build the JAR
mvn clean packageStep 3: Run the JAR
java -jar target/your-project-name-version.jarKey Differences
| Plugin | Output | Use Case | 
|---|---|---|
| Maven Shade Plugin | Single JAR (shaded) | General-purpose applications | 
| Maven Assembly Plugin | JAR with -jar-with-dependencies | Legacy projects | 
| Spring Boot Plugin | Executable JAR (includes embedded Tomcat) | Spring Boot applications | 
Verification
- Check if the JAR contains dependencies:
  jar tf target/your-project-name.jar | grep dependency-nameBy using these plugins, you can create a self-contained, executable JAR that includes all required dependencies.