Java

Creating Spring Maven Project

In this article, we will learn how to create a Maven-based spring application using the Maven tool to build and manage all the project dependencies.

Let’s start with opening the Eclipse that we just installed. Click on the File menu and select the Maven project, like this:


It will ask for several details, such as project location. Set the location and click next.


After that, provide the package name, app name, version, packaging type, etc. The packaging specifies the final build bundle type of the project. If the application is a web app, then it should be war (Web Archive).


Hit the Finish button, and it will take a couple of seconds to create a project. At first glance, the newly created project looks like this:


Initially, all the folders are empty, and no source file is present. The pom.xml file is the main Maven file containing all the configurations and dependencies of the project. We will use this file to add spring dependencies, as well.

Initially, the pom.xml file looks like this:

// pom.xml

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.linuxhint</groupId>
  <artifactId>springapp</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>springapp</name>
  <description>a simple spring app</description>
</project>

 

// Spring Dependencies

To make the spring application, we need to specify these dependencies to the pom.xml file and wait. It will download all the required files automatically to the project.

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
    </dependency>
</dependencies>
<properties>
        <spring.version>5.2.8.RELEASE</spring.version>
</properties>

 
After adding these dependencies, you will notice that the Maven dependency folder contains all the JAR files.

Notice: The Maven dependencies folder was not present in the initial crafted project before adding the dependencies. It was added by the Maven to collect all the dependencies into a single folder for better management.


To check whether all these JARs are working fine, you can create a simple class and then use any spring annotation, such as @Component or @Configuration, etc., in the source file.


Apply @Component annotation to the class and see it provides an auto suggestion that has linked all the JARs to the Eclipse. Checking the annotation:


Here, everything is working fine.

Conclusion

In this article, we have learned how to create successfully a Maven-based spring project. We used the pom.xml file and // Spring Dependencies in the example provided to create the new Spring Maven project.

About the author

Irfan Khan

I’m a software programmer having more than 5 years of development experience in Java and related technology. I love to write technical content as well and love to share the technical knowledge to make it available for all.