Do you think IIT Guwahati certified course can help you in your career?
No
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.
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.
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
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 archetype: generate
If everything goes well, you’ll see this-
Maven will start functioning and will create the complete maven project structure as follows
You'll see a java application project created, named coding (as specified in artifactId) as defined in the following snapshot.
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.
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-
To create a simple Maven web application, we will use this command- mvn archetype: generate. 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?