03. Setting up Maven step by step tutorial

This is mainly for the beginners of Java to get started with Maven. This is required not only for learning Java, but also conducive to creating your self-taught Java projects. Extends Setting up Java step by step tutorial. This is an industrial strength tool to build Java applications.

Install Maven

Maven is a build, dependency management (i.e. jar files dependency) and deployment tool.

Step 1: Download and install Maven from:

http://maven.apache.org/download.html

Download the binary “zip” file “apache-maven-3.5.0-bin.zip” if in Windows or the “apache-maven-3.5.0-bin.tar.gz” if in Unix, and unpack them into the “tools” folder.

Install Maven

Step 2: Configure the environment variables “M3_HOME” and “path”. For example

On DOS

Previously in Java installation tutorial “environment-setup.cmd” was created under “c:\scripts”. Add Maven environment settings to that file.

Run the script:

On Unix

type the following in “.profile”

Verify installation

Once maven has been set up correctly, you should be able to verify it by

also, “where” in DOS and “which” in Unix.

Configure Maven

You can set up the repository location by opening the settings.xml file under conf folder inside your maven installation directory “c:\tools\apache-maven-3.5.0-bin\apache-maven-3.5.0\conf”. Repository is where the dependency Java libraries will be stored.

Rename the settings.xml to settings.xml_old, and create a new “settings.xml” as shown below with a basic config.

Local Repository

Are you behind a firewall?

If your internet access requires proxy settings, you need have that added to your settings.xml file.

Create a Maven project

A maven project is nothing but a Java project as we saw earlier with a predefined structure. Maven follows a certain convention in terms of the directory structure.

Step 1: Create a Java project using Maven. “com.mytutorial” is a package inside “src/main/java”.

Step 2: “pom.xml” file looks as shown below.

The pom.xml file is where you define the dependent libraries like “hibernate”, “spring”, “junit”, etc. Change the version to the latest possible. For example: “4.9” as per

http://repo1.maven.apache.org/maven2/junit/junit/4.9/“.

You can also search at

http://search.maven.org/

prefix it with “g:” for group as in “g:junit”, and “a:” for artifact id as in “a:junit”.

Step 3: Inspect the local repository location that we set up via the “settings.xml”.

Maven Local Repository

Step 4: Create a Java “HelloWorld.java” as shown below inside “C:\projects\simple-maven-project\src\main\java\com\mytutorial” where “src\main\java” is the mvn stipulated project structure and “com\mytutorial” is the package.

Step 5: Execute the following command from the project base folder “C:\projects\simple-maven-project”. This basically looks at the “pom.xml” file in that folder.

This would have created a jar file named “simple-maven-project-1.0-SNAPSHOT.jar” inside the “C:\projects\simple-maven-project\target”. This jar file contains the packages & class files. The maven default plugins have compiled the “.java” files and packaged it up as a jar file. This is the basic power of a build tool. It can do a lot more.

Step 6: Run “com.mytutorial.HelloWorld”

Much simpler to create Java projects as jar files.

Tips:

#1. Setting up the Maven environment

Instead of running the “c:\scripts>environment-setup.cmd” in Windows, you could set up the Windows environment variables as demonstrated in setting up Java tutorial.

set M3_HOME

set MAVEN_OPTS

set the path variable

#2. Installing the jar file to local maven repository

Instead of “mvn clean package”, you can install it to the repository in your “c:\tools\home\.m2\repository” folder.

Publish your project artifact(i.e jar file) to the local repository

Next Java Beginner Tutorial: Setting up Eclipse IDE step by step tutorial.


Java Interview FAQs

800+ Java Interview Q&As

Top