Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: Mar 27, 2024
Difficulty: Easy

How To Create A Maven Project

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

Have you ever tried doing projects in java? How convenient is it to work with Java using Maven? Don’t worry! Coding Ninjas got your back, and in this article, we will learn How to create a maven project.

Introduction

But before that, let’s know about Maven.

What Is Maven

Maven is a technology used to build and manage any Java-based project. It has various functionalities like creating, compiling, deployment, source management, etc. 

The next question is, how does Maven do it? Every Maven project consists of a POM file, and it is used for configuring.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Prerequisite

First, ensure that you have installed Java and Maven in your system according to the Operating Systems requirements.

Check the following articles if you need to install Maven.

Type this command in the command prompt to verify if maven is installed correctly. This window should show up.

mvn –version
mvn --version

After ensuring the presence of the maven, we will begin.

Creating A Maven Project

To create a simple Maven Project, we will use this plugin.

mvn archetypegenerate

If everything goes well, you’ll see this-

project generate

Maven will start functioning and will create the complete maven project structure as follows 

maven structure

You'll see a java application project created, named coding (as specified in artifactId) as defined in the following snapshot.

structure of file

src

The source folder contains the main file. The main file consists of the sample jsp file.
 

pom.xml 

POM stands for Project Object Model. This file contains the details of the project and its configuration information. Maven uses this information to build a project.

The contents of the pom.xml file are as follows-

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.codingninjas.code</groupId>
  <artifactId>coding</artifactId>
  <version>1.0</version>
  <name>coding</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>
  
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>
  
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
        <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>


You will witness that Maven also created a sample JSP Source file.

app.java

Now you need to access the AppTest.java file in the src directory of our project. AppTest is a unit-test source to test that the App class works as expected. It will look like this-

apptest

This is how we create a Maven Project.

Must Read Apache Server

Frequently Asked Questions 

What is the command to create a Maven Project?

To create a simple Maven web application, we will use this command- mvn archetypegenerate. Maven will start the creation of the project if installed correctly.

What are groupId, artifactId, and version?

groupId, artifactId, and version are maven coordinates for identifying the projects. They are compulsory to define.

List the advantages of Maven.

A maven is a helpful tool for project management. It is based on POM (project object model). It is widely used for project build, dependency, and documentation. It simplifies the building process.

What is a POM File?

POM stands for Project Object Model. This file contains the details of the project and its configuration information. Maven uses this information to build a project.

What is the method to check the presence of Maven?

We use the mvn –version command to check whether Maven is present. If we get an error, we should download it from the official website. 

Conclusion

In this blog, we studied how to create a Maven Project. We hope this article helped you clear your concepts. Enjoyed reading about Maven?

Refer to these articles to learn more about it!

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses and refer to the mock test and problems available. Take a look at the interview experiences and interview bundle for placement preparations.

Happy Coding!

Topics covered
1.
Introduction
2.
What Is Maven
3.
Prerequisite
4.
Creating A Maven Project
5.
Frequently Asked Questions 
5.1.
What is the command to create a Maven Project?
5.2.
What are groupId, artifactId, and version?
5.3.
List the advantages of Maven.
5.4.
What is a POM File?
5.5.
What is the method to check the presence of Maven?
6.
Conclusion