Maven Package executable jar

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.