Using the MAVEN Assembly plug-in to make a jar package to implement a dependency pack archive

Source: Internet
Author: User
Tags unpack
If your project is built with Maven, you may encounter such problems when the project is online and deployed to the server. The problem is that there is no MAVEN environment on the server, that is, the jar packages in the warehouses (repository) that the project relies on, you need to extract them separately and upload them to the server.

I know that if the Pom type is war, the MVN Package command will automatically hit the project-dependent jar package into the Lib folder under Web-inf. However, if the pom type is a jar, when you invoke the MVN Package command, the dependent third party package is not extracted during execution.

In the past, my practice was, wait until the project is on line to change the Pom type to war and then execute the MVN Package command so that the package that you rely on is extracted and then the POM type is changed to a jar, so that although the task can be accomplished, it's always a bit awkward.

So recently looked for a better way, in fact very simple, is the use of MAVEN assembly Plug-ins, add the following plug-ins in Pom.xml:

[XHTML] View Plain copy print? <project> [...] <build> [...] <plugins> <plugin> <!--note:we don ' t need a groupId Specificati On because the ' group is org.apache.maven.plugins ... which is assumed by default. --> <artifactId>maven-assembly-plugin</artifactId> <version>2.2-beta-5</version> < configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </ Descriptorrefs> </configuration> [...] </project> <project> [...] <build> [...] <plugins > <plugin> <!--note:we don ' t need a groupId specification because the group is org.apache.maven.plugins ... W Hich is assumed by default. --> <artifactId>maven-assembly-plugin</artifactId> <version>2.2-beta-5</version> < configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </ Descriptorrefs> </configuration> [...] </projeCt>

Then enter on the command line:

MVN assembly:assembly

You will find the newly generated {Artifactid}-jar-with-dependencies.jar file under the ${project}/target folder.

In the execution of the above command, MAVEN exports the package that the jar package relies on, and extracts (unpackage) and puts it in the

This {Artifactid}-jar-with-dependencies.jar package, which is handy for the deployment of the program, even if your project relies on a number of third-party packages, will be merged into a assembly during deployment.

But the problem is, in the process of deployment, we often hope that the various third-party packages are individually deployed, such as Ibatis to Ibatis package, Spring package to spring package, so that a separate Third-party package upgrade is also more convenient.

In fact, there are solutions:

Used before

[XHTML] view plain copy print? <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs>

This jar-with-dependencies is a assembly, assembly description Reference (assembly descriptor) Let's take a look at this XML file that defines this assembly description (Assemly descriptor)

[XHTML] View Plain copy print? <assembly xmlns= "http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0" xmlns:xsi= "http:// Www.w3.org/2001/XMLSchema-instance "xsi:schemalocation=" http://maven.apache.org/plugins/maven-assembly-plugin/ assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd "> <id>jar-with-dependencies</id> <formats> <format>jar</format> </formats> <includebasedirectory>false</ includebasedirectory> <dependencySets> <dependencySet> <unpack>true</unpack> <scope >runtime</scope> </dependencySet> </dependencySets> <fileSets> <fileSet> < directory>${project.build.outputdirectory}</directory> </fileSet> </fileSets> </assembly > <assembly xmlns= "http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0" xmlns:xsi= "http:// Www.w3.org/2001/XMLSchema-instance "xsi:schemalocation=" http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd "> <id> jar-with-dependencies</id> <formats> <format>jar</format> </formats> < Includebasedirectory>false</includebasedirectory> <dependencySets> <dependencySet> < unpack>true</unpack> <scope>runtime</scope> </dependencySet> </dependencySets> <fileSets> <fileSet> <directory>${project.build.outputDirectory}</directory> </fileset > </fileSets> </assembly>

In fact, as long as the XML file in the above

[XHTML] view plain copy print? <dependencySet> <unpack>true</unpack> <scope>runtime</scope> </dependencySet> <dependencySet> <unpack>true</unpack> <scope>runtime</scope> </dependencySet>

Change into:

[XHTML] view plain copy print? <dependencySet> <unpack>false</unpack> <scope>runtime</scope> </dependencyset > <dependencySet> <unpack>false</unpack> <scope>runtime</scope> </ Dependencyset>

Create the Src.xml file under Main/assembly, and write the text that you have just modified into the file:

[XHTML] View Plain copy print? <assembly xmlns= "http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0" xmlns:xsi= "http:// Www.w3.org/2001/XMLSchema-instance "xsi:schemalocation=" http://maven.apache.org/plugins/maven-assembly-plugin/ assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd "> <id>jar-with-dependencies</id> <formats> <format>jar</format> </formats> <includebasedirectory>false</ includebasedirectory> <dependencySets> <dependencySet> <unpack>false</unpack> <scope >runtime</scope> </dependencySet> </dependencySets> <fileSets> <fileSet> < directory>${project.build.outputdirectory}</directory> </fileSet> </fileSets> </assembly > <assembly xmlns= "http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0" xmlns:xsi= "http:// Www.w3.org/2001/XMLSchema-instance "xsi:schemalocation=" http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd "> <id> jar-with-dependencies</id> <formats> <format>jar</format> </formats> < Includebasedirectory>false</includebasedirectory> <dependencySets> <dependencySet> < unpack>false</unpack> <scope>runtime</scope> </dependencySet> </dependencySets> <fileSets> <fileSet> <directory>${project.build.outputDirectory}</directory> </fileset > </fileSets> </assembly>

Change the plugin in the previous pom.xml to the following:

[XHTML] view plain copy print? <plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> <descriptors > <descriptor>src/main/assembly/src.xml</descriptor> </descriptors> </configuration> </plugin> <plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> <descriptors> <descriptor>src/main/assembly/src.xml</descriptor> </descriptors> </ Configuration> </plugin>

Re-execution

MVN assembly:assembly
This will see the newly created {Artifactid}-jar-with-dependencies.jar jar package in the target folder

All the Third-party packages that the project relies on will be packaged inside, as shown below:

Reproduced from: http://blog.csdn.net/mozhenghua/article/details/5671133

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.