Understanding Maven Basic and Building a SpringBoot app from pom.xml File

In this article I will be explaining the basics of maven, pom.xml file and we can look at how to build a spring boot application from ground up.

What is Maven ?

Maven is a build and management tool for java application. It helps to simplify the build process by providing a uniform build system for any Java Application.

In the above explanation,I mentioned that Maven is a management tool that provide a uniform build system and you may be wondering how Maven does that.

Maven makes use of a file called "pom.xml" .This file is located as the root directory of the java base application or in multiple location for project inheritane base app.

What is POM ?

This is an acronym that stands for Project Object Model. I like to think of this as a requirement list that contains all the necessary component and configuration that our java base application will need for it to build and carry out any functionality we want.

The pom.xml file is similar to the package.json file in a Nodejs base project or composer.json in a Php application.

How does the pom.xml fulfill the requirement in the list?

The pom.xml basically connect to the maven directory and help to pull each specified requirement in other to build our java application.

Building A Spring Boot App.

In order for us to build a spring boot application using maven as our build tool we need to first install maven using the below command in our terminal.

sudo apt-get install maven

then we verify if it has been installed by running

mvn --v

Screenshot from 2022-02-21 17-17-05.png

Create a new directory where you want to build our java/springboot application and then create a pom.xml in the direction

 mkdir springbootApp && cd springbootApp
 touch pom.xml

In our pom.xml file we will define the dependencies we need in other for us to build a spring application. paste the below scripting your pom.xml .

<?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.example</groupId>   
    <artifactId>springbootApp</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.6</version>
    </parent>

    <!-- Additional lines to be added here... -->

</project>

After we have defined the initial pom file we build it by typing .

mvn package

Our application now has a target folder which contains a build.
In other for us to build a spring application we need to add spring starter files that is required for a web base application.

<?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.example</groupId>
    <artifactId>springBootApp</artifactId> <!-- change this lie to your app name -->
    <version>0.0.1-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.3</version>
    </parent>

    <dependencies>   <!-- Inclusion start here -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies> <!-- End of inclusion -->

</project>

We can now try to package our build which will instruct maven to use our requirement list that is our pom.xml file to fetch us our dependency inour case the spring-boot-starter-web.

mvn package

Now we can begin writing our Java Application and get to include any dependency we will need in the future to this pom.xml. Create your Application entry using our standard way of building java project.

mkdir src && cd src
mkdir main && cd main
mkdir java && cd java
touch SpringbootApp.java

In the Java file you created using the format above update it with the below code.

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@EnableAutoConfiguration
public class SpringBootApp {
    @RequestMapping("/")
    String home() {
        return "Hello World!";
    }

    public static void main(String[] args) {
        SpringApplication.run(SpringBootApp.class, args);
    }
}

we can now run the package command to package our app and then run our spring boot application on our browser. using the below command

mvn spring-boot:run

now that's how to run a spring app from ground up. ):

Did you find this article valuable?

Support slycreator by becoming a sponsor. Any amount is appreciated!