In this Maven tutorial for beginner, I’d like to guide you guys how to get started with Maven on Mac operating system by downloading, installing Apache Maven, and then creating and building a simple Java project using Maven command line tool.

You know, Maven is very well known as a build tool for Java projects management. It is a free, open-source project developed by the Apache Software foundation.

NOTE: To use Maven command line tool on macOS, you must have Java Development Kit (JDK) pre-installed. Check out this article to know how to install JDK on macOS.

 

1. Download and Install Apache Maven on macOS

Apache Maven is distributed as a binary archive so you need to download a zip file from its official home page here. Click the link next to Binary zip archive, as shown below:

download maven zip file

It will download apache-maven-xxx-bin.zip file into your Downloads directory. You will install it in your user home directory, so open a new Terminal window, and type the following command:

      mv Downloads/apache-maven-xxx-bin.zip $HOME

This command moves the zip file into your user home directory. And type the following command to unzip it:

unzip apache-maven-xxx-bin.zip

The result is a folder named apache-maven-xxx created, which contains binary files of Maven.

Next, you need to configure system environment variables so you can use the Maven command line tool (mvn) anywhere in terminal.

If you’re using macOS with ZShell, type the following commands to create a text file named .zshrc (ZShell resource file) with the following content:

cat > .zshrc

export MAVEN_HOME=$HOME/apache-maven-xxx

export PATH=$MAVEN_HOME/bin:$PATH

Hit Enter and press Ctrl + D to save the file. The following screenshot illustrates the above steps:

maven unzip and configure system variable

Then quit the terminal and open a new one for the change to system environment variables take effect. In the new terminal, type mvn -v to check version of Maven:

run mvn v command

If you see this screen, that means you have successfully installed Maven on macOS. You see, the command prints version information of Maven, Java and operating system.


2. Use Maven to Create and Build a Simple Java Project on macOS

Next, I’ll show you how to use Maven command line tool to create a simple Java project, then compile and build it. Type the following command:

mvn archetype:generate -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4

This command will generate a Java project with the Maven archetype quickstart and archetype version 1.4. You’ll be asked to provide information about the project as shown below:

maven generate project

Enter groupId, artifactId, version and package as shown above, then type Y to confirm. Then you should see the BUILD SUCCESS message like this:

maven create project success

Explore the generated project under MyJavaApp directory. You should see the Maven project file (pom.xml), the main class (App.java) and the test class (AppTest.java).

Next, let’s compile and build the project into a Java Archive (JAR) file. Type the following commands:

cd MyJavaApp

mvn package

You’ll see the following output:

mvn package build success

The result is a JAR file name MyJavaApp-1.0.jar created under target directory. To run this Java program, type the following command:

                java -cp target\MyJavaApp-1.0.jar net.codejava.App

You can see it print “Hello World!” message.

That’s my tutorial about download, install Apache Maven on macOS. You also learned how to create a simple Java project using Maven command line tool, and how to build and run it.

To see the steps in action, watch the following video:

 

Other Maven Articles:


About the Author:

is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.



Add comment