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.
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.
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".
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!"); } }
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.
In the project root directory, execute the following command to package:
mvn package
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!