Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to use Maven to generate jar packages that can run directly

Shulou Source: shulou.com Published: 2022-06-01 02:19:51 10月04日 Update

Editor to share with you how to use Maven to generate jar packages that can be run directly, I believe most people don't know much about it, so share this article for your reference. I hope you can learn a lot after reading this article. Let's learn about it together.

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:

[html] view plain copy

Org.apache.maven.plugins

Maven-jar-plugin

2.6

True

Lib/

Com.xxg.Main

Org.apache.maven.plugins

Maven-dependency-plugin

2.10

Copy-dependencies

Package

Copy-dependencies

${project.build.directory} / lib

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:

[plain] view plain copy

Class-Path: lib/commons-logging-1.2.jar lib/commons-io-2.4.jar

Main-Class: com.xxg.Main

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:

[html] view plain copy

Org.apache.maven.plugins

Maven-assembly-plugin

2.5.5

Com.xxg.Main

Jar-with-dependencies

Packing method:

[plain] view plain copy

Mvn package assembly:single

After packaging, 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:

[html] view plain copy

Org.apache.maven.plugins

Maven-assembly-plugin

2.5.5

Com.xxg.Main

Jar-with-dependencies

Make-assembly

Package

Single

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:

[html] view plain copy

Org.apache.maven.plugins

Maven-shade-plugin

2.4.1

Package

Shade

Com.xxg.Main

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:

[html] view plain copy

Org.apache.maven.plugins

Maven-shade-plugin

2.4.1

Package

Shade

Com.xxg.Main

META-INF/spring.handlers

META-INF/spring.schemas

The above is all the contents of the article "how to use Maven to generate jar packages that can run directly". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Build file run directory configuration method content plug-in project method article use code copy instruction resource appropriate same not enough not much Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Microsoft Shulou Tech Info MySQL Linux