Two ways to run the jar package for Maven. 1, built-in dependency, 2, external dependence.

Source: Internet
Author: User


Maven hits two ways to run a jar package


Maven common packaging methods are divided into Pom,jar,war, as for these packaging types are not introduced.


If you do not make a special configuration, the packaged jar package is not operational. Can only be used as a normal dependency package.


Here are two ways maven can run a jar package:


Mode one: built-in packing method

<plugins><!--  Built-in packing method  --><plugin><groupid>org.apache.maven.plugins</groupid ><!--  Use this MAVEN package plugin  --><artifactid>maven-shade-plugin</artifactid><version >2.3</version><executions><execution><phase>package</phase><goals>< goal>shade</goal></goals><configuration><!--  The default value is true. Note This property, if you use this plugin to deploy, or publish to a central repository, this property will reduce your pom file, will take your reliance on <dependency> kill  --><createdependencyreducedpom>false</ Createdependencyreducedpom><transformers><transformerimplementation= " Org.apache.maven.plugins.shade.resource.AppendingTransformer "><resource>meta-inf/spring.handlers</ Resource></transformer><transformerimplementation= " Org.apache.maven.plugins.shade.resource.AppendingTransformer "><resource>meta-inf/spring.schemas</ Resource></transformer><transformerimplementation= "Org.apache.maven.plugins.shade.resoUrce. Manifestresourcetransformer "><!--  This is your program entry file  --><mainClass> Com.alibaba.dubbo.container.main</mainclass></transformer></transformers></configuration ></execution></executions></plugin><plugin><groupid>org.apache.maven.plugins </groupId><artifactId>maven-resources-plugin</artifactId><version>2.4</version> <configuration><!--  Set character encoding set  --><encoding>utf-8</encoding></configuration ></plugin></plugins>


You can get a bunch of files after using Maven install above.


/target/directory to find a, you can find a < your project name >.jar


This package is relatively large (your dependency package + entity class = total size) and can be run directly


Run Example: Java-jar file path/< your project name >.jar



Mode two: using external Dependency package method

<plugins><!--  Package jar file, configure the manifest file, add the Lib package jar dependencies  --><plugin><groupId> Org.apache.maven.plugins</groupid><artifactid>maven-jar-plugin</artifactid><configuration ><classesDirectory>target/classes/</classesDirectory><archive><manifest>< mainclass>com.alibaba.dubbo.container.main</mainclass><!--   manifest when packing. MF file does not record timestamp version  --><useuniqueversions>false</useuniqueversions><addclasspath>true</ Addclasspath><classpathprefix>lib/</classpathprefix></manifest><manifestentries> <class-path>.</class-path></manifestentries></archive></configuration></ Plugin><plugin><groupid>org.apache.maven.plugins</groupid><artifactid> maven-dependency-plugin</artifactid><executions><execution><id>copy-dependencies</ id><phase>package</phase><goals><goal>copy-dependencies</goal></goals><configuration><type>jar</type>< includetypes>jar</includetypes><useuniqueversions>false</useuniqueversions>< Outputdirectory>${project.build.directory}/lib</outputdirectory></configuration></execution ></executions></plugin></plugins>

You can get a bunch of files after using Maven install above.


/target/directory to find, and the above is not the same, you will find < your project name >.jar not so big.


and there will be a folder called Lib, and this Lib folder is full of your dependent jar package


Then in use,< your project name >.jar will load the external lib inside the jar package.


When deploying the project, you need to copy the Lib folder and < your project name >.jar together to use


Note: Two need to be in the same directory OH


Run Example: Java-jar file path/< your project name >.jar


This article is from the "Green Years" blog, be sure to keep this source http://alex233.blog.51cto.com/8904951/1885759

Two ways to run the jar package for Maven. 1, built-in dependency, 2, external dependence.

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.