How to build an executable jar in Maven

Introduction

I ran into a wall called Maven a while ago and learned from the fact that Maven was the first place to building an executable jar file.

This time, I will introduce the simplest way to make an executable jar collected from various places, which also serves as a memorandum of what I learned at that time.

If you know of any other easier way, I would appreciate it if you could point out. I wrote a separate article about the basic usage of Maven, so please see that as well.

1. What is an executable jar?

java -jar <jar file name> A jar file that can be executed with the command. Normally, a program made with java must be built as an executable jar when it is executed.

java -classpath <jar file name> <class name with main> <command line arguments>

It is necessary to specify the jar file to be executed and the class path separately as shown in, but if you build it as an executable jar, you can execute it with only the command introduced at the beginning. If it is a GUI environment such as Windows, it can be executed by double-clicking. In that case, it's tough for console applications, but ...

The story goes awry, but I'm planning to write an article about how to create a GUI program using java, so if you are interested, please wait for a while.

2. Build with maven

From here, I will show you how to build an executable jar using Maven. In this article, we will use Eclipse and assume that you understand the basic usage of Maven. For those who say "I've heard of Maven, but I don't know what it is", please read the rest of the article after you can use Maven to some extent.

I have written such an article once, so please see here if you like.

2.1. Creating a Maven project

Either Eclipse or Maven's Komano can be used, but for the time being, create a Maven project. This time, anything is fine as long as the executable jar can be built, so make a Hello World source file and put it in.

2.2. Introduction of maven-assembly-plugin

To build an executable jar file with Maven, use a Maven plugin called assembly-plugin.

Please describe the following contents in pom.xml and add the plugin.

<!-Plugins for executable jar files-> org.apache.maven.plugins maven-assembly-plugin 3.0.0 test <!-Bundle all dependent resources in a jar-> jar-with-dependencies main.Main <!-The id tag can be any string-> make-assembly package single

2.3. Generate jar file

At this point, all Maven settings are complete. All you have to do now is build with the familiar commands. Execute the following command in the directory containing pom.xml of the created project.

mvn install

Then maven should be running and a jar file should be generated in the target directory. I think that two jar files have been generated, but the one with "jar-with-dependencies" is the executable jar file.

Execute the jar file with a command. java -jar Generated jar file What do you think? I think the jar file has been executed.

3. Finally

So far we have shown you how to use maven to generate an executable jar file. I spent a lot of time studying the maven series myself, so I think I'll post another article. I hope it will be useful for those who want to use maven, or who have no choice but to use it but do not know how to use it.

Recommended Posts

How to build an executable jar in Maven
What happened in "Java 8 to Java 11" and how to build an environment
How to add local jar to maven pom.xml
How to build an Apache Flink application from scratch in 5 minutes
How to solve an Expression Problem in Java
How to build android-midi-lib
[Rails] How to build an environment with Docker
How to build the simplest blockchain in Ruby
How to debug the generated jar file in Eclipse
How to write an external reference key in FactoryBot
[Rails] How to display an image in the view
How to create an application
How to handle an instance
How to use Maven to place resource files outside the JAR
How to pass an object to Mapper in MyBatis without arguments
How to change a string in an array to a number in Ruby
How to retrieve the hash value in an array in Ruby
Set up Gradle multi-project in IntelliJ to build JAR file
[Ruby] How to batch convert strings in an array to numbers
How to use Lombok in Spring
How to find May'n in XPath
How to hide scrollbars in WebView
How to run JUnit in Eclipse
How to iterate infinitely in Ruby
[Rails] How to write in Japanese
How to run Ant in Gradle
How to master programming in 3 months
How to learn JAVA in 7 days
How to get parameters in Spark
How to install Bootstrap in Ruby
Make multi-project executable JAR in Gradle
How to insert an external library
How to use InjectorHolder in OpenAM
How to use classes in Java?
How to make a Maven project
How to name variables in Java
How to set Lombok in Eclipse
How to build vim on Ubuntu 20.04
How to build CloudStack using Docker
How to concatenate strings in java
How to install Swiper in Rails
[Ruby] How to count even or odd numbers in an array
How to create a new Gradle + Java + Jar project in Intellij 2016.03
How to deploy an app that references a local jar to heroku
How to install the language used in Ubuntu and how to build the environment
How to get the length of an audio file in java
How to crop an image with libGDX
Make an executable jar using Android Studio
[swift5] How to specify color in hexadecimal
How to implement search functionality in Rails
How to implement date calculation in Java
Multilingual Locale in Java How to use Locale
How to change app name in rails
How to get date data in Ruby
How to use custom helpers in rails
How to use named volume in docker-compose.yml
How to filter JUnit Test in Gradle
How to blur an image (super easy)
How to standardize header footer in Thymeleaf
How to include Spring Tool in Eclipse 4.6.3?
Don't know how to build task'credentials: edit'