How tomcat downloads the maven dependencies?

Viewed 2716

I have just started using Maven for one of my projects. I am packaging my artifact as war file which I'd deploy to tomcat. I have specified all my dependencies in the pom.xml file (For ex: jackson-databind jar)

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.9.7</version>
</dependency>

I could see all my dependecy jars in my eclipse classpath, sure there were no compilation errors or anything.

After packaging my code into a war file I tried to check it's contents. I could see all my source file, but there was no trace of my dependencies in the war file.

I deployed the war to a local tomcat server and was able to test with no error.

My question is: How did tomcat get the jars that my project was depending on? or does tomcat read the pom.xml of each war file and download all the dependencies?

2 Answers

Said easily, maven finds for you all libraries needed to let your web application work, following what is described in pom file. In fact, at the Good Old Times (!) all needed libraries had to be searched manually and put on /lib folder to let webapp work correctly.

So, when you run e.g. mvn clean install maven does this work for you: looks for dependencies, finds correct jar files from a maven repository, download them (if needed), then builds your war putting everything in the right place, so when .war is deployed on webapps folder on Tomcat (and then unzipped by tomcat itself), at webapp start Java classloader will not complain.

Sorry for the really simplified example, but I hope to have clarified the point.

Tomcat server is not responsible for resolving maven dependencies, It only use for running application on server.

When you building your project using mvn clean install then automatically dependencies jars will be attached to you war file. No need to worry about dependency resolution.

Tomcat maps this request to the appropriate Engine for processing. These Engines are contained within other elements, such as Hosts and Servers, which limit the scope of Tomcat's search for the correct Engine.

User calls the "archetype" plugin with the "generate" goal to generate a Maven project of the specified type from the standard model for this type of project.

Maven executes a series of steps associated with this goal, checking local repositories for the dependencies (both Java-standard JARs and Maven plugins) required to build the given archetype. Maven organizes these files into directories, using the same layout for every project. This "Standard Directory Layout" helps guarantee that all Maven projects will look familiar to all users, even if they are built with different frameworks.

The user modifies the package's POM.xml configuration file with configuration information appropriate for their project. Large pieces of this process can be automated via other plugins.

The user calls a Maven "phase" on the project, such as compile, test, or deploy. Maven automatically triggers all necessary preceding phases to reach the requested phase. Phases provide command abstraction based on information inherited from the POM. Thus, calling the "package" phase on a JAR project will execute different steps than calling the same command on a WAR project.

Hope this will help.

Related