Home > Java > javaTutorial > body text

A complete guide to installing and configuring Maven on Mac systems

WBOY
Release: 2024-01-28 09:42:06
Original
768 people have browsed it

A complete guide to installing and configuring Maven on Mac systems

Detailed explanation of the methods and techniques of installing Maven on Mac system

As a developer, installing Maven on Mac system is a very common requirement, because Maven is a very Popular build tool for managing dependencies and build processes of Java projects. This article will introduce in detail the methods and techniques of installing Maven on Mac system, and provide specific code examples.

1. Download Maven

First, you need to download the latest version of Maven from the official website (https://maven.apache.org/download.cgi). On the website, two versions can be found: binary and source. If you just want to use Maven without participating in Maven development, it is recommended to download the binary version. Click the link to download the binary version and select a suitable download image.

2. Install Maven

After the download is completed, find the download file, usually in .tar.gz or .zip format. Taking the .tar.gz format as an example, extract it to a suitable directory, such as the /opt directory. Open the terminal and enter the following command:

cd /opt
tar -zxvf apache-maven-3.6.3-bin.tar.gz
Copy after login

After decompression is completed, an apache-maven-3.6.3 folder will be generated in the directory /opt, which contains all Maven files. Next, we need to configure Maven into environment variables.

3. Configure environment variables

Open the terminal and enter the following command:

vim ~/.bash_profile
Copy after login

If there is no .bash_profile file, enter the following command to create the file:

touch ~/.bash_profile
Copy after login

Add the following content to the .bash_profile file:

export M2_HOME=/opt/apache-maven-3.6.3
export PATH=$PATH:$M2_HOME/bin
Copy after login

Save the file and exit edit mode. Enter the following command to make the modification effective:

source ~/.bash_profile
Copy after login

In this way, Maven is successfully configured into the environment variables.

4. Verify the installation

Open the terminal and enter the following command:

mvn -version
Copy after login

If the installation is successful, the following information will be displayed:

Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /opt/apache-maven-3.6.3
Java version: 1.8.0_271, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk1.8.0_271.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "11.0.1", arch: "x86_64", family: "mac"
Copy after login

This means Maven installation Successful, and the Maven version information and Java related information are displayed.

5. Use Maven

After the installation is complete, you can use Maven to create, build and manage Java projects. Below is a simple example that demonstrates how to use Maven to create a Java project.

  1. Create project directory

First, create an empty directory as your project directory. Enter the following command in the terminal:

mkdir my-project
cd my-project
Copy after login
  1. Create project structure

Create the following structure in the project directory:

mkdir -p src/main/java
mkdir -p src/main/resources
mkdir -p src/test/java
mkdir -p src/test/resources
Copy after login
  1. Create Java class

Create a Java class in the src/main/java directory, such as HelloWorld.java. Enter the following command in the terminal:

cd src/main/java
touch HelloWorld.java
Copy after login

Use a text editor to open the HelloWorld.java file and add the following code:

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

Save the file and exit the editor.

  1. Compile the project

Return to the project root directory and execute the following command to compile the project:

cd ../..
mvn compile
Copy after login
  1. Run the project

Execute the following command to run the project:

mvn exec:java -Dexec.mainClass="HelloWorld"
Copy after login

If everything goes well, you will see the output in the terminal: "Hello, Maven!".

6. Summary

Through the introduction of this article, you should have learned how to install Maven on a Mac system and use Maven to create and build Java projects. I hope this content will be helpful to you when using Maven in development. Happy programming!

The above is the detailed content of A complete guide to installing and configuring Maven on Mac systems. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template