Maven: Let’s Get Started with Single Module Project

Reading Time: 4 minutes

If you wants to manage the build, testing, and deployment processes. Maven can helps you out in this. It can separate the unit tests and integration tests.So you only run them when necessary and cut down on build time.

In this blog we’re going to learn for what reason do we need Maven, basics, and a single module project of Maven.

For what reason do we need Maven? 

When we are doing java projects then almost every time we need dependencies. Dependencies are only libraries or JAR files. We need to just download it and add it manually. Before Maven, the job of upgrading the software stack for our projects has been done manually. Therefore, there was a requirement for a better build tool that would deal with such issues. 

To overcome these tasks, Maven comes into the image. It can handle all the issues related to dependencies which automate the process.

What is Maven?

Maven is a build automation tool developed by Apache-based. It uses the concept of a global repository that provide artifacts and dependencies that requires to build the project. We simply need to determine the dependencies and the version of the software that we need in the pom.xml file and it will deal with the rest automatically. For every project, we need to deal with a pom.xml file, it is the most important file to build the project.

Project Object Model(Pom.xml) : 

This file contains all the information about the project, configuration of plugins, dependencies, goals, etc. to build the project. When we build the project, the compiler reads the pom.xml file to get the dependencies, its configuration, and plugins information.

Structure is like: 

<?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>{{organization_identifier}}</groupId>
    <artifactId>{{project_name}}</artifactId>
    <version>{{version_id}}</version>

    <properties>
        <maven.compiler.source>{{jdk_version}}</maven.compiler.source>
        <maven.compiler.target>{{jdk_version}}</maven.compiler.target>
    </properties>

</project>

Creating a Simple Maven Project 

 A maven project can be created by using an IDE as well as a terminal. Besides, if you want to learn how to create the project through the terminal, you can refer here else you can continue with IDE.

In this, I’ll explain through the IDE- IntelliJ.

1 . Click on the new project on the welcome screen.

Maven

2. Select the Maven which is showing in the left pane.

Maven

3. After that, specify the project’s JDK(SDK) or you can select the default one which is “Create from archetype” only if you want to use predefined templates. We can configure it accordingly to our preferences. Click Next.

4. Therefore, in the next window, specify the coordinates of the maven which has to add to the pom.xml file which is as follows:

  • GoupId: An identifier that starts with the organisation’s name in the reverse order that produced the module and after that, it moves into more specialised projects. For example, com.knoldus
  • Artifact: A module name within the group. For example, if the module name is “assign” so its artifact name is “assign”.
  • Version: It’s the identifier of the release. For example: “1.0-SNAPSHOT”. Click Next.

5. However, if you’re creating it by using archetype, IntelliJ provides the option to set the Maven home directory and its repositories. You can check the archetype properties also. Click Next

archtype

6. At last, Specify the name of the project and its location.  Click finish.

finish

Therefore, it will load the project with the pom.xml file automatically by IntelliJ. This file includes the compiler and target version, dependencies, artifacts, groupId, etc. i.e; all the necessary information of the project to start the work. 

Directory Structure of the project:

Subsequently, includes the code by creating the file. For example: As shown, I create the Hello.java file inside the com.knoldus package which is the main class file.

package com.knoldus;
public class Hello {
    public static void main(String[] args){
        System.out.println("Hello World");
    }
}

How to run the maven project?

Commands are:
  • mvn clean install: This will build the project. After that, it creates the target folder. It contains all the sources, resources, web files, plugin info.
  • mvn clean compile: It makes the project fresh and clean to compile.
  • To run the java file: Go to C:\{{Project_name}}\target\classes directory and the run the command:
    • java {{packages_name}}.{{file_name}}

As a result, Hello World will be the output. 

Our work is done!! We made our first maven project by learning a step-by-step process.

Conclusion:

We came to know that the maven surely helps us to understand the process of the automation tool without adding the dependencies manually. We learn that the pom.xml file is the most important file for any maven project in which we can add plugins that allow us to reuse common build logic across multiple projects. You can also build multi-module processes in maven. For that, you can refer here.

Here are my references: you can refer

knoldus-advt-sticker

Written by 

Alka Vats is a Software Consultant at Knoldus. She is passionate about web development. She is recognized as a good team player, a dedicated and responsible professional, and a technology enthusiast. She is a quick learner & curious to learn new technologies. Her hobbies include reading books, watching movies, and traveling.