Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




Spring Boot - Maven


If you are well versed in Maven, you can skip this tutorial and proceed to the next one.


In the previous tutorial we have said that we need dependencies for,


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

To understand dependencies, let us go back to java. So in java, if you need an external framework like Jackson(That deals with xml files) or supercsv(That deals with csv files), you need to download the JARs for Jackson or Supercsv and add it to your classpath.


And what does the JARs contain?


The JARs can be thought of as an actual jar in your kitchen, which contains java classes.


Now, does it makes sense, why the JARs are placed in the classpath?


If you still have confusion, just break classpath into two parts. Class and Path, i.e. the path or place where java looks for the classes.


And a JAR is loaded with classes. So, we take the JAR and place it in the classpath. And Java finds all the classes it needs from that JAR.


Now, let us take a step forward.


Just like Jackson(That deals with xml files) or supercsv(That deals with csv files), Spring is also a framework.


So, in the similar way we need to place all the JARs provided by Spring in the Classpath of Java.


Note : Do not forget, Spring is a framework for java.

Now, to make our life a little easier, we will not be downloading the JARs for Spring and put it in the classpath. Instead we would be using a tool called maven.


What is Maven?


Don't you think, it would be a lot easier if we could list out all the names of the JARs and put it into a file. And the file would scan the JAR names one by one and download them and puts it into the classpath all by itself.


And this is exactly what Maven does?


There is a file called pom.xml. In this file you put the list of all the JAR files needed and maven automatically downloads the JAR file and puts it into your classpath.


How does Maven work?


Once you are done placing all the names of the JARs in the pom.xml file. You need to run a maven command,

  1. Maven looks at all the names of the JARs.


  2. And it goes to the repository that contains all the JARs.


  3. Then it downloads all the JARs.


  4. And places those JARs in the classpath.


And your job is done.


Long story short, instead of downloading all the JARs and placing them in your classpath,you only write the names of the JARs in the pom.xml of Maven. And Maven takes care of the rest.


Now, what are dependencies?


In the above pom.xml you have listed the names of the JARs. In technical term, names of JARs is called dependency.


Below is a sample pom.xml


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
http://maven.apache.org/xsd/maven-4.0.0.xsd">

	<modelVersion>4.0.0</modelVersion>
	<groupId>com.learnerslesson.myexample</groupId>
	<artifactId>myapplication</artifactId>
	<version>1</version>

</project>

So, in the above pom.xml, we have 4 elements.

  1. <project>



    <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">
    ...
    
    </project>


    <project> is the root element of pom.xml. Which says, all other elements should be inside it. Rest of the things in <project> element like xmls, xmlns:xsi can be ignored for now. You can just write it as is.

  2. <modelVersion>



    Next is <modelVersion>, the element inside the <project> element.

    <modelVersion>4.0.0</modelVersion>


    It should always be set to 4.0.0.

  3. <groupId>



    The <groupId> element is used to specify the name of the company for which you are making this project.

    <groupId>com.learnerslesson.myexample</groupId>


    In the above case, say we are creating a project for learnerslesson.com and the name of our project is myexample.

    In that case, we are creating the groupId as com.learnerslesson.myexample.

  4. <artifactId>



    The <artifactId> element is used to specify the actual name of the application you are going to build.

So far we have used the basic version of pom.xml. Now, let us put dependency to it.


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
http://maven.apache.org/xsd/maven-4.0.0.xsd">

	<modelVersion>4.0.0</modelVersion>
	<groupId>com.learnerslesson.myexample</groupId>
	<artifactId>myapplication</artifactId>
	<version>1</version>
	<packaging>jar</packaging>

	<name>Maven Quick Start Archetype</name>
	<url>http://maven.apache.org</url>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.8.2</version>
			<scope>test</scope>
		</dependency>
	</dependencies>

</project>

So, in the above pom.xml, we need to include the dependency for junit. And we have added the dependency for it.


<dependency>
	<groupId>junit</groupId>
	<artifactId>junit</artifactId>
	<version>4.8.2</version>
	<scope>test</scope>
</dependency>

So, the above dependency downloads the jar for junit of version 4.8.2 from the maven repository.


And if you want to specify many dependencies, you can specify inside the element.


<dependencies>
	<dependency>
		<groupId>junit</groupId>
		<artifactId>junit</artifactId>
		<version>4.8.2</version>
		<scope>test</scope>
	</dependency>
</dependencies>

So, that was a brief description of Maven.