How to create runnable JAR file with Maven

How to create runnable JAR file using Maven assembly plugin.

By default if you run goal package, a JAR file will be created. But without dependencies and inside META-INF/MANIFEST.MF won't be specified Main-Class.

 

How to create a runnable Java application? Using Maven assembly plugin.

Add inside pom.xml file:

<build>
  <plugins>
    <plugin>
    <artifactId>maven-assembly-plugin</artifactId>
      <configuration>
        <archive>
          <manifest>
            <mainClass>com.javavids.main.Main</mainClass>
          </manifest>
        </archive>
        <descriptorRefs>
          <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
      </configuration>
    </plugin>
  </plugins>
</build>

and then run goal:

 clean package assembly:single 

and inside target directory will be created a JAR file with packaged dependencies