Click here to read Part 1 of this post

Creating a Maven Web Project

Let’s now create a new web project in Eclipse.

  1. Start Eclipse -> File -> New -> Project
  2. From the list, select Maven -> Maven Project eclipse-maven-project
  3. Click Next. On the Select An Archetype, type web in the Filter text field. From the list, choose maven-archetype-webapp
  4. On the next page, enter following:

    Group Id   :    com.myproject
    Artifact Id :  MyTestProject
    Package     :  com.myproject
  5. Click Finish

An introduction to how Maven works

First of all, the problem. In earlier days, when we had to use some external JAR files, we had to put them in a lib folder and then add them in build path. Now this caused us lot of trouble in version changes and mismatch. Also, we  had to face dependency errors as well.

Maven solves all these problems. We have to write all our requirements (dependencies) in an XML file called Project Object Model (pom.xml). Maven reads this file and downloads all the matching JAR files for us. It also provides all these JAR files to our project automatically and hence we don’t have to put anything in the lib folder.

But from where does it downloads? Where does it download?

Maven provides repositories that contains various JARs of different Java technologies. The default repository of Maven is Maven Central Repository. There are many other repositories as well that can be added to pom.xml.

http://search.maven.org/   is the site for maven central repository.

Remote Repository <==> Local Repository <==> Application

Maven by looking at your pom.xml searches for JAR files in the remote repositories one by one (if multiple repositories are specified) starting from maven central repository. It downloads the JAR files in your local repository. In case of Windows, it is generally:  c:\users\<user_name>\.m2\repository

Now, it provides JAR files from this local repository to your application.

Structure of a basic pom.xml

pom.xml resides in the root of your application. A basic pom.xml looks like this:

<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/maven-v4_0_0.xsd">
<groupId>com.mycompany</groupId>
<artifactId>my-project</artifactId>
<packaging>war</packaging>
<version>0.0.1</version>
<name>My Project</name>
<properties>
</properties>
<dependencies>
</dependencies>
<repositories>
</repositories>
</project>

groupId     :     This is used to identify your organization

artifactId   :     This is the name of the project

version       :     Version of jar

For more information on POM, please click here

So, whatever you need in your project, just search in the Maven central repository and copy the dependency tag in your pom.xml

For example, let’s search mysql connector on maven central repository. Click here to open it.

Now enter mysql connector in the text box and press enter. Click the link under version that have artifact id as mysql-connector-java. Copy the dependency tag similar to this:

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.36</version>
</dependency>

and paste it under the <dependencies> </dependencies> tag of your pom.xml and save. That’s it. Maven will download this jar file and provide it to your project.

Click here to read Part 3 of this post