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.
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.
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.
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.
Once you are done placing all the names of the JARs in the pom.xml file. You need to run a maven command,
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.
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
<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.
<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>
<modelVersion>4.0.0</modelVersion>
<groupId>com.learnerslesson.myexample</groupId>
So far we have used the basic version of pom.xml. Now, let us put dependency to it.
<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
<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.