Home >Java >javaTutorial >Java Maven Build Tool: Getting Started Guide

Java Maven Build Tool: Getting Started Guide

王林
王林Original
2024-04-17 18:39:01627browse

Maven is an open source tool for building and managing Java projects. It provides features such as dependency management, build automation, and documentation generation. After installing Maven, you can create a project by running the mvn archetype:create command. The pom.xml in the project directory contains metadata about the project, build instructions, and dependencies. Building Java applications using Maven involves compiling source code, running tests and creating jar files. You can run the application by executing the mvn clean package command and running the jar file with the java -jar command.

Java Maven构建工具:上手指南

Java Maven Build Tool: Getting Started Guide

Maven is a popular open source tool for building and managing Java projects. It provides rich functionality, including dependency management, build automation, and project documentation generation.

Install Maven

First, install Maven in your system. You can download the latest version from the Maven official website: https://maven.apache.org/.

Extract the downloaded file and add it to the system path. For Windows users:

set PATH=%PATH%;C:\path\to\maven\bin

For Mac and Linux users:

export PATH=$PATH:/path/to/maven/bin

Create Project

To create a Maven-based project, run the following command :

mvn archetype:create -DgroupId=my.group.id -DartifactId=my-project -Dversion=1.0-SNAPSHOT

This command will create a project directory containing basic Maven configuration files and structure.

pom.xml

The pom.xml file in the project directory is the main configuration file of Maven, containing metadata about the project, Build instructions and dependencies.

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>my.group.id</groupId>
  <artifactId>my-project</artifactId>
  <version>1.0-SNAPSHOT</version>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.13.2</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

</project>

Practical Case: Building a Java Application

The following is an example of using Maven to build a simple Java application:

// App.java
public class App {
    public static void main(String[] args) {
        System.out.println("Hello Maven!");
    }
}
// pom.xml
<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>my.group.id</groupId>
  <artifactId>maven-test</artifactId>
  <version>1.0-SNAPSHOT</version>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.1</version>
        <configuration>
          <source>11</source>
          <target>11</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

Execute the build

In order to build the project, run the following command:

mvn clean package

This command will compile the source code, run the tests and create an executable jar file.

Run the application

To run the application, use the following command:

java -jar target/maven-test-1.0-SNAPSHOT.jar

You will see "Hello Maven!" output in the console .

Conclusion

Maven is a powerful build tool that simplifies the development and maintenance of Java projects. By following this guide, you will understand its basic concepts and how to use it to build and run Java applications.

The above is the detailed content of Java Maven Build Tool: Getting Started Guide. 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