Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Use maven to package swing projects into executable jar packages (including dependencies)

Shulou Source: shulou.com Published: 2022-06-03 06:29:33 09月10日 Update

This article introduces the method of breaking a project into an executable jar package through maven.

This article requires readers to have basic knowledge of maven, understand the general configuration of maven, plug-in configuration, understand the concept of maven phase, goal, and understand the role of dependency and manifest.

Prerequisites:

(the method described in this paper is applicable to the characteristics of the project)

1. The project must be a maven project

3. There must be a main entry class in the project, which can be executed when the user double-clicks the packaged jar file.

Package:

The first case: stand-alone projects with no third-party dependency packages

In this case, we need maven's maven-jar-plugin plug-in to help us pack. Please add the following to the plugin configuration in the project pom.xml: (see the comments section)

... Maven-jar-plugin 2.3.2 true lib/ com.yourProjectPackage.YourEntranceClass...

Then use mvn clean install to compile and install your project. At the end of the command execution, a jar file and a lib folder are generated (if your project references 3rd-party dependencies). Double-click the jar file and you will see the effect immediately. Projects with interfaces should be able to see the main interface (if your program is right), without interfaces, you should be able to see relevant information from your machine's java console, or the project itself has log output or magical effects, you can directly view the output results.

The second case: project + third-party dependency package

In this case, you cannot use the maven-jar-plugin plug-in to package. We are going to use another plugin, maven-assembly-plugin. This plug-in can help us package our projects flexibly (not only for jar projects, but also for ear, war and other projects. This article only describes how to package executable jar packages). Here, we use this plug-in to combine our source code compilation results with the project's dependencies to form a jar file.

Here is the configuration part of the plugin in the pom.xml of the project:

Maven-assembly-plugin 2.6 com.your.package.YourEntranceClass jar-with-dependencies make-assembly package single

After adding this plugin, you can clean install your maven project. Then you will find something like this in the console after the execution of the maven command:

-maven-install-plugin:2.4:install (default-install) @ MvnCfgSwitcher-

Installing F:\ uuuu\ vvvv\ Wwwww\ target\ Wwwww-1.0-SNAPSHOT.jar to Y:\ mvn_repo\ com\ yourname\ Wwwww\ 1.0-SNAPSHOT\ Wwwww-1.0-SNAPSHOT.jar

Installing F:\ uuuu\ vvvv\ Wwwww\ pom.xml to Y:\ mvn_repo\ com\ yourname\ Wwwww\ 1.0-SNAPSHOT\ Wwwww-1.0-SNAPSHOT.pom

Installing F:\ uuuu\ vvvv\ Wwwww\ target\ Wwwww-1.0-SNAPSHOT-jar-with-dependencies.jar to Y:\ mvn_repo\ com\ yourname\ Wwwww\ 1.0-SNAPSHOT\ Wwwww-1.0-SNAPSHOT-jar-with-dependencies.jar

The blue identification part is the executable jar file of the generated all-in-one.

Double-click the package result and try it, dear!

Tags: Project plug-in file situation configuration interface result part content command that is effect method third party build compile output magic function information Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Apple NVidia Shulou Technology MariaDB