How to create Java Project with Maven and convert it to Spring Boot Project

How to create Java Project with Maven and convert it to Spring Boot Project

·

5 min read

Intro:-

maven is a tool to help managing Builds/Documentation/Dependencies/software configuration management/distribution etc. In Maven, an archetype is a template of a project which is combined with some user input to produce a working customised user's Maven project. An archetype is like template. Maven project created from the archetype has a POM, a source tree for your application's sources and a source tree for your test sources. That is the standard layout for Maven projects (the application sources reside in ${basedir}/src/main/java and test sources reside in ${basedir}/src/test/java, where ${basedir} represents the directory containing pom.xml)

Task:-

You are going to create maven project and convert it to spring boot project

Software Requirements:-

Ubuntu-20.04 +jit [linux-x86_64]
Open Jdk 8
Spring Boot 2.2

Level:-

Beginner

Prerequisite:-

Knowledge of core java and text editor

(A) creating maven project

Step 1:-

Create a directory
$mkdir my-devnationapp

Step 2:-

start a shell in that directory. On your command line, execute the following Maven goal:
$cd my-devnationapp

$mvn archetype:generate -DgroupId=com.devnation.app -DartifactId=my-devnationapp -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DinteractiveMode=false

maven1.png

The prefix archetype is the plugin that provides the goal. If you are familiar with Ant, you may conceive of this as similar to a task. This archetype:generate goal created a simple project based upon a maven-archetype-quickstart archetype. plugin is a collection of goals with a general common purpose.

Step 3:-

Build the Project
$mvn package

maven3.png

maven2.png

Unlike the first command executed (archetype:generate), the second is simply a single word - package. Rather than a goal, this is a phase. A phase is a step in the build lifecycle, which is an ordered sequence of steps. When a phase is given, Maven executes every phase in the sequence up to the defined one.

Step 4:-

compiled and packaged JAR with the following command:
$java -cp target/my-devnationapp-1.0-SNAPSHOT.jar com.devnation.app.App

maven4.png

(B)Convert maven project into spring boot project

Step 5:-

The spring-boot-starter-parent project is a starter project – that provides default configurations for your spring boot application and a complete dependency tree to quickly build your Spring Boot project. It also inherits dependency management from spring-boot-dependencies.

add these in your pom.xml file :

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

Step 6:-

Managing Dependencies
Once, you've declared the starter parent in our project, you can pull any dependency from the parent by just declaring it in our dependencies tag.

Also, you don't need to define versions of the dependencies, Maven will download jar files based on the version defined for starter parent in the parent tag.

For example, if you're building a web project, you can add spring-boot-starter-web directly, and you don't need to specify the version.

adding dependencies in pom.xml

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
    </dependency>

Step 7:-

run
$cd spring-boot-parent
or
$cd my-devnationapp

$mvn spring-boot:run

springbootproject2.png

springbootproject3.png

Note:-

i) If you are forgetting to add spring boot parent in pom file

use these command

$mvn org.springframework.boot:spring-boot-maven-plugin:run

springbootproject4.png

ii) The Dependency Management Tag

To manage a different version of a dependency provided by the starter parent you can declare dependency and its version explicitly in the dependencyManagement section:

<dependencyManagement>    
<dependencies>         
   <dependency>       
<groupId>org.springframework.boot</groupId>     
 <artifactId>spring-boot-starter-data-jpa</artifactId>      
<version>2.4.0</version>    
</dependency>      
</dependencies>    
</dependencyManagement>

iii) pom file

<?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.devnation.app</groupId>
  <artifactId>spring-boot-parent</artifactId>
  <version>1.0-SNAPSHOT</version>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.2.7.RELEASE</version>
 </parent>

  <name>my-devnationapp</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.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>
  <dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.2.6.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

  <dependencies>
     <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
    </dependency>
    <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>

The POM is the basic unit of work in Maven. Maven is inherently project-centric, in which everything is based on standard project.POM contains every important piece of information about your project and is essentially a single file for finding anything related to your project. Understanding the POM is important.

project :- This is the top-level element in all Maven pom.xml files.

modelVersion :- This denotes the version of the object model of POM .

groupId :- This element indicates the unique identifier of the organization or group that created the project. The groupId is one of the key identifiers of a project and is typically based on the fully qualified domain name of your organization. For example org.apache.maven.plugins is the designated groupId for all Maven plugins.

artifactId :-This element indicates the unique base name of the primary artifact being generated by this project. The primary artifact for a project is typically a JAR file. Secondary artifacts like source bundles also use the artifactId as part of their final name. A typical artifact produced by Maven would have the form -. (for example, myapp-1.0.jar).

Conclusion:-

After performing above steps you made a spring boot nano app. First your created basic java project using maven then your converted this project into spring boot project by adding spring -boot dependency in pom file. After adding starter parent in your project, you can fetch any dependency from the parent by just declaring it in your dependencies tag.