Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the ways to package Maven?

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what are the ways of Maven packaging". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the ways of Maven packaging"?

Maven can package the project using the mvn package directive, and if you run the jar file using Java-jar xxx.jar execution, there will be errors such as "no main manifest attribute, in xxx.jar" (no Main-Class is set), ClassNotFoundException (dependency package not found), and so on.

For jar packages to run directly through java-jar xxx.jar, you need to meet the following requirements:

1. Specify Main-Class in the META-INF/MANIFEST.MF in the jar package to determine where the entrance to the program is.

2. Be able to load into the dependency package.

There are several ways to generate jar packages that can be run directly with Maven, and you can choose an appropriate one as needed.

Method 1: package with maven-jar-plugin and maven-dependency-plugin plug-ins

Configure in pom.xml:

Maven-jar-plugin is used to generate parts of the META-INF/MANIFEST.MF file. Com.xxg.Main specifies that Main-Class,true in MANIFEST.MF will add a Class-Path entry to MANIFEST.MF and configure the dependency package, and lib/ specifies the directory where the dependency package resides.

For example, the following is a snippet of a MANIFEST.MF file generated by the maven-jar-plugin plug-in:

Just generating the MANIFEST.MF file is not enough; the maven-dependency-plugin plug-in is used to copy the dependency package to the location specified by ${project.build.directory} / lib, that is, the lib directory.

After the configuration is completed, the package is packaged with the mvn package instruction. The jar package is generated in the target directory, and the dependency package is copied to the target/lib directory. The directory structure is as follows:

With Main-Class specified, with dependent packages, you can run the jar package directly through java-jar xxx.jar.

The disadvantage of generating jar packages in this way is that too many jar packages are generated and are not easy to manage. The following two ways only generate a jar file that contains the project's own code, resources, and all dependent packages.

Method 2: package using the maven-assembly-plugin plug-in

Configure in pom.xml:

Packing method:

After mvn package assembly:single is packaged, a xxx-jar-with-dependencies.jar file is generated in the target directory, which contains not only the code and resources from your own project, but also the contents of all dependent packages. So you can run it directly through java-jar.

You can also package directly through mvn package without the need for assembly:single, but you need to add some configuration:

Package and single means that when performing package packaging, assembly:single is executed, so you can use mvn package packaging directly.

However, if Spring Framework is used in a project, packages typed in this way will go wrong at run time, which can be dealt with using method 3 below.

Method 3: package using the maven-shade-plugin plug-in

Configure in pom.xml:

After the configuration is complete, execute mvn package to package. Two jar packages are generated in the target directory, not the original-xxx.jar file, but the other one. Like maven-assembly-plugin, the generated jar file contains all the dependencies, so you can run it directly.

If Spring Framework is used in the project, typing the dependency into a jar package, there will be an error reading the XML schema file at run time. The reason is that multiple jar packages of Spring Framework contain the same files spring.handlers and spring.schemas, and if you generate a jar package, they will overwrite each other. To avoid interaction, you can use AppendingTransformer to append the merge to the contents of the file:

Thank you for your reading, these are the contents of "what are the ways of Maven packaging?" after the study of this article, I believe you have a deeper understanding of the way of Maven packaging, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report