Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What Is Maven?
3.
Creating a Maven Web Application
4.
Build Web Application
5.
Deploy Web Application
6.
Test the Maven Web Application
7.
Frequently Asked Questions 
7.1.
What is the command to create a Maven Web Application?
7.2.
What is a POM File?
7.3.
What are groupId, artifactId, and version?
7.4.
What is the use of Maven in building web applications?
7.5.
What is the method to check the presence of Maven?
8.
Conclusion
Last Updated: Mar 27, 2024
Easy

Maven Web Application

Author Geetika Dua
0 upvote
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? Do you want to create a Maven Web application? 

Don’t worry! Coding Ninjas got your back and in this article we will learn about Maven Web Application.

Introduction

But, before that let’s learn 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

Creating a Maven Web Application

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.

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

mvn archetype: generate


If everything goes well, you’ll see this-

mvn generate

Maven will start functioning and will create the complete web-based java application project structure as follows −

mvn identification

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

src

Files

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

pom.xml 

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.

sample file

 

Must Read Apache Server

Build Web Application

The next step is to open the command console, go to the ‘C:\Users\geeti\coding’ directory, and execute the following mvn command.

C:\Users\geeti\coding>mvn clean package


Maven will start building the project.

build
build

Deploy Web Application

Now copy the coding.jar created in the directory to your web server web app directory and restart the web server.

deploy

Test the Maven Web Application

You can test the application by invoking the web project's URL:

http:/localhost:8080/coding/index.jsp

Hello World output

Frequently Asked Questions 

What is the command to create a Maven Web Application?

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

What is a POM File?

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

What are groupId, artifactId, and version?

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

What is the use of Maven in building web applications?

Maven increases reusability and takes care of most of the build-related tasks. Maven was originally designed to simplify building processes.

What is the method to check the presence of Maven?

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

Conclusion

In this blog, we studied how to create a Maven Web Application. We also looked at the deployment and testing aspects of it. We hope this article helped you clear your concepts. Enjoyed reading about Maven?

Refer to these articles to know 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!

Live masterclass