As we are going to use Spring MVC APIs heavily in our project, we need to add the Spring jars in our project to make use of it in our development. As mentioned previously, Maven will take care of managing dependency jars and packaging the project.
Let's take a look at how we can add the Spring-related jars via the Maven configuration:
pom.xml
; you can find pom.xml
under the root directory of the project itself.pom.xml
file. Select the Dependencies tab.org.springframework
, Artifact Id as spring-webmvc
, and Version as 4.3.0.RELEASE
. Select Scope as compile and then click on the OK button, as shown in the following screenshot:javax.servlet
, Artifact Id as jstl
, Version as 1.2
, and select Scope as compile.javax.servlet
, Artifact Id as javax.servlet-api
, and Version as 3.1.0
, but this time, select Scope as provided and then click on the OK button.pom.xml
file.In the Maven world, pom.xml
(Project Object Model) is the configuration file that defines the required dependencies. While building our project, Maven will read that file and try to download the specified jars from the Maven central binary repository. You need Internet access in order to download jars from Maven's central repository. Maven uses an addressing system to locate a jar in the central repository, which consists of Group Id, Artifact Id, and Version.
Every time we add a dependency, an entry will be made within the <dependencies> </ dependencies>
tags in the pom.xml
file. For example, if you go to the pom.xml tab after finishing step 3, you will see an entry for spring-webmvc
as follows with in <dependencies> </ dependencies>
tag:
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.2.2.RELEASE</version> </dependency>
We added the dependency for spring-mvc in step 3, and in step 4, we added the dependency for JSTL. JSTL is a collection of useful JSP (JavaServer Pages) tags that can be used to write JSP pages easily. Finally, we need a servlet-api.jar
in order to use servlet-related code; this is what we added in step 5.
However, there is a little difference in the scope of the servlet-api dependency compared to the other two dependencies. We only need servlet-api while compiling our project. While packaging our project as war
, we don't want to ship the servlet-api.jar
as part of our project. This is because the Tomcat web server would provide the servlet-api.jar
while deploying our project. This is why we selected the Scope as provided for servlet-api.
After finishing step 6, you will see all the dependent jars configured in your project, as shown in the following screenshot, under the Maven Dependencies library:
We added only three jars as our dependencies, but if you look in our Maven dependency library list, you will see more than three jar entries. Can you guess why? What if our dependent jars have a dependency on other jars and so on?
For example, our spring-mvc.jar
is dependent on the spring-core, spring-context, and spring-aop jars, but we have not specified those jars in our pom.xml
file; this is called transitive dependencies in the Maven world. In other words, we can say that our project is transitively dependent on these jars. Maven will automatically download all these transitive dependent jars; this is the beauty of Maven. It will take care of all the dependency management automatically; we need to inform Maven only about the first-level dependencies.