Home  >  Article  >  Java  >  How to easily install and configure Maven from the basics on Mac systems

How to easily install and configure Maven from the basics on Mac systems

PHPz
PHPzOriginal
2024-01-28 09:14:061157browse

How to easily install and configure Maven from the basics on Mac systems

Easily install and configure Maven on Mac systems from scratch, specific code examples required

Maven is a powerful tool for building and managing Java projects. It can help us effectively manage project dependencies, build projects and generate executable files. This article will introduce how to install and configure Maven on Mac systems and provide specific code examples.

Step One: Install Homebrew
Homebrew is a powerful package manager on the Mac system, through which we can install Maven. To install Homebrew, we can open a terminal and execute the following command:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

This command will download and install Homebrew.

Step 2: Use Homebrew to install Maven
Execute the following command in the terminal to install Maven:

brew install maven

This command will download and install the latest version of Maven.

Step 3: Verify whether Maven installation is successful
Execute the following command in the terminal to verify whether Maven is installed successfully:

mvn -version

If Maven is installed successfully, you will see something similar to the following Output:

Maven home: /usr/local/Cellar/maven/3.8.3/libexec
Java version: 1.8.0_291, vendor: AdoptOpenJDK, runtime: /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "11.6", arch: "x86_64", family: "mac"

This output indicates that Maven has been successfully installed and can run.

Step 4: Configure Maven environment variables
In order to use Maven conveniently, we need to configure some environment variables. First, execute the following command in the terminal to edit the bash configuration file:

nano ~/.bash_profile

This command will open a text editor. In the editor, add the following:

export MAVEN_HOME=/usr/local/Cellar/maven/3.8.3/libexec
export PATH=$PATH:$MAVEN_HOME/bin

Press Ctrl X to save and exit the editor.

Then, execute the following command to make the configuration file take effect:

source ~/.bash_profile

Now, we can verify whether the environment variables are configured correctly by executing the following command in the terminal:

echo $MAVEN_HOME

If the path you see in the output is consistent with the Maven version you installed, then your environment variables have been configured correctly.

Step 5: Create a simple Maven project
To demonstrate the use of Maven, we will create a simple Java project. Create a new folder in the directory of your choice and go to the folder in the terminal. Then, execute the following command to create a new Maven project:

mvn archetype:generate -DgroupId=com.example -DartifactId=example-project -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

This command will use Maven’s Quick Start template to create a project named example-project.

Step 6: Build and run the project
Enter the project directory you just created in the terminal and execute the following command to build the project:

mvn clean package

This command will compile the project and generate a executable executable file.

Then, execute the following command to run the project:

java -cp target/example-project-1.0-SNAPSHOT.jar com.example.App

This command will run the App class in the project.

At this point, you have successfully installed and configured Maven on your Mac system, and created and run a simple Java project.

Summary:
This article introduces how to install and configure Maven from scratch on a Mac system, and provides specific code examples. By following these steps, you can easily start using Maven to build and manage Java projects. Have fun using Maven!

The above is the detailed content of How to easily install and configure Maven from the basics 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