In daily development we often need to package the project into a CLI (command line) package that can be run directly on the command lines. The default maven-generated jar package contains only the. class files and project resource files that were compiled, and a jar file that can be run directly from the command line through a Java command, and also satisfies two conditions:/meta-inf/manifest in the jar package. The MF metadata file must contain main-class information. All dependencies of the project must be in Classpath.
MAVEN provides the Maven-shade-plugin plugin with the convenience of helping us with this task, which allows the user to configure the value of Main-class and then fill the value in/meta-inf/manifest when packaging. MF file. With regard to project dependencies, it is very clever to extract all the dependent jar files and then merge the resulting. class files with the. class files of the current project into the final CLI package, so that all the required classes are in Classpath when executing the CLI jar file.
Here is a sample Pom.xml configuration:
<project> ... <build> <plugins> <plugin> <groupid>org.apache.maven.plu Gins</groupid> <artifactId>maven-shade-plugin</artifactId> <version>2.4.2</vers
ion> <executions> <execution> <phase>package</phase>
<goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation= "ORG.APACHE.MAVEN.PLUGINS.SHADE.RESOURCE.M
Anifestresourcetransformer "> <mainClass>org.sonatype.haven.HavenCli</mainClass> </transformer> </transformers> </configuration> </executio n> </executions> </plugin> </plugins> </build> ... </project>
You only need to modify Main-class to the class with the main method in the current project.
Reference Link: https://maven.apache.org/plugins/maven-shade-plugin/examples/executable-jar.html