Home  >  Article  >  Java  >  A step-by-step guide to packaging with the Maven plugin

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

WBOY
WBOYOriginal
2024-02-19 15:35:06821browse

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

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

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!");
    }
}

4 . Configure the packaging plug-in

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

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.2.2</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>com.example.HelloWorld</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>

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

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

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!

Statement:
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