Home> Java> javaTutorial> body text

A step-by-step guide to packaging with the Maven plugin

WBOY
Release: 2024-02-19 15:35:06
Original
738 people have browsed it

A step-by-step guide to packaging with the Maven plugin

Using Maven to build projects is a common operation in Java development, and using the Maven packaging plug-in can easily package the project into an executable file. This article will teach you step by step how to use Maven to package plug-ins, with specific code examples.

1. Make sure the environment is configured

First, make sure you have installed Maven and configured the environment variables. You can check whether Maven has been installed correctly by entering the following command in the command line window:

mvn -v
Copy after login

If the Maven version information is displayed, it means that Maven has been installed successfully.

2. Write a Maven project

Next, we need to create a Maven project. First, execute the following command on the command line:

mvn archetype:generate -DgroupId=com.example -DartifactId=my-project -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
Copy after login

This will create a Maven project named "my-project".

3. Write code

Enter the project directory and write a Java class in the src/main/java/com/example directory, such as HelloWorld.java:

package com.example; public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } }
Copy after login

4 . Configure the packaging plug-in

Add the following plug-in configuration in the project's pom.xml file:

   org.apache.maven.plugins maven-jar-plugin 3.2.2    true com.example.HelloWorld      
Copy after login

The above configuration will tell Maven to use the maven-jar-plugin plug-in to package the project, and specify the main The class is com.example.HelloWorld.

5. Execute the packaging command

In the project root directory, execute the following command to package:

mvn package
Copy after login

6. Run the program

After completing the packaging , you can find the generated JAR file in the target directory, such as my-project-1.0-SNAPSHOT.jar. Run the JAR file from the command line:

java -jar target/my-project-1.0-SNAPSHOT.jar
Copy after login

If everything goes well, you will see "Hello, World!" output.

At this point, you have successfully used the Maven packaging plug-in to package the project into an executable JAR file. Hope this practical guide is helpful!

The above is the detailed content of A step-by-step guide to packaging with the Maven plugin. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn