Quick Introduction to Maven

Maven logo
Maven is a dependency management tool.

Now, the terms dependency management might be new. Nothing, new here, you know it all. dependency means, in general, a jar file (which is a collection of classes). This jar files relates to a particular framework or api and management in the sense, managing those jar files. I said, jar files because your project might contain multiple jar files, because it is most likely to use more than one framework. Even, if project is not using more than one framework, you might contain multiple jar files. This is because the framework you are using might depend on classes in other jar files. So, it is necessary to include those jar files for the framework to work.
For example, when writing a common Hello world program in Java, you need System.out.println() where System class resides in java.lang package. This class is present in rt.jar which is situated in your jre folder.
Without this rt.jar file, you cannot compile the program because there is no java.lang package.

Fine, what does maven do?

Maven manages these dependencies (jars) for you. When your project uses a particular framework, it downloads all the jar files that the particular framework uses. So, you don't have to. All you need to do is to just tell maven which framework your project uses, and maven looks after every resource (jar file) that the framework need to work. So, no matter how many frameworks you use, just tell them what you use, what those frameworks use internally is something you need not care about.

Where do i specify what frameworks my projects use?

There is a file called pom.xml where POM means Project Object model which defines the model of the project. We will be writing a <dependencies> tag in which we will specify each dependency. Note that dependency means a jar file. There are many other tags in pom.xml, however we are only going to work with a few which enables us to include dependencies.

Where are these jar files stored?

These jar files are stored in the Central repository (http://search.maven.org). This contains all the jar files from which the maven pulls. You can also download the jar files independently.

groupId, artifactId and version

Every dependency will have these properties. groupId refers to the id of the group which is unique to the project and next, artifactId is the name of the jar file and version refers to the version of the jar file (as you might have updates for the framework, so you have different versions).
You might have a doubt about the difference between groupId and artifactId. It is simple, groupId refers to the name of the project. Let me give you some examples, so that you will be able to understand.

groupId - org.springframework
artifactId - spring-core

groupId - org.springframework
artifactId - spring-beans

Writing your first 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.javademos.blogspot</groupId>
   <artifactId>maven-project</artifactId>
   <version>1.0</version>
 
</project>
 
See, <project> is the root element which contains several tags in it. Just don't worry about the schema locations and all, if you are not familiar with it. The .xsd files contains definitions for the tags like <groupId>, <artifactId> etc, i.e. if you don't have those xsd files, these tags don't work, the pom.xml will contain errors. Consider it like importing packages when we make use of a class in java.

When we create a project, it must have a name which is the groupId and there must be a name that is given to the jar file (artifcatId) and the version. The groupId must follow the package naming conventions (i.e. how  java packages are named).
Now, comes the thing. Including dependencies..
 
<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.javademos.blogspot</groupId>
   <artifactId>maven-project</artifactId>
   <version>1.0</version> 
 
   <dependencies>
          <dependency>
              <groupId>org.springframework</groupId>
              <artifactId>spring-core</artifactId>
              <version>4.1.4.RELEASE</version> 
          </dependency> 
   </dependencies> 
</project>
 
See, you can contain as many dependencies your project uses. All you need to do is to add a <dependency> tag in the <dependencies> tag. Note, that the version name can also contain alphabets as you can see.

You might ask me, how do I know all groupId's, artifactIds and versions which I want to use. In fact, most of the times you don't know the groupId, but you will be familiar with artifactId. Just type go to the http://search.maven.org and there search for artifactId. For example, spring-core. That's it. By seeing the groupId you will be able to understand which spring-core you need to use. Just click on the latest version (if you use) otherwise, you can go to all versions where you will be displayed a lot of versions and choose the version your project uses.

In the left side, you will be having Dependency information panel, where you can copy the <dependency> tag and paste it. You need not even write it too!
 


Posted by at on

Tags: Maven,

-->