search
HomeJavajavaTutorialA complete guide to installing and configuring Maven on Mac systems

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

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

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

touch ~/.bash_profile

Add the following content to the .bash_profile file:

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

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

source ~/.bash_profile

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

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"

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

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

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
  1. Run the project

Execute the following command to run the project:

mvn exec:java -Dexec.mainClass="HelloWorld"

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!

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
How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?Mar 17, 2025 pm 05:46 PM

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?Mar 17, 2025 pm 05:45 PM

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?Mar 17, 2025 pm 05:44 PM

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?Mar 17, 2025 pm 05:43 PM

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

How does Java's classloading mechanism work, including different classloaders and their delegation models?How does Java's classloading mechanism work, including different classloaders and their delegation models?Mar 17, 2025 pm 05:35 PM

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools