Maven

Maven

Apache Maven, is an innovative software project management tool, provides new concept of a project object model (POM) file to manage project’s build, dependency and documentation. The most powerful feature is able to download the project dependency libraries automatically.

Maven Local Repository is a local folder that is used to store all your project’s dependencies (plugin jars and other files which are downloaded by Maven). In simple, when you build a Maven project, all dependency files will be stored in your Maven local repository.

By default, Maven local repository is default to .m2 folder.

When you build a Maven’s project, Maven will check your pom.xml file, to identify which dependency to download. First, Maven will get the dependency from your Maven local repository, if not found, then get it from the default Maven central  repository http://search.maven.org/

By default, Maven will download from the central repository. In Maven, when you’re declared library does not exist either in local repository nor Maven center repository, the process will stop and output error messages to your Maven console.

Maven

Maven

The org.xxx.xxxx is only available at xxxx.net repository. When you build this Maven project, it will fail and output dependency not found error message.

To tell Maven to get the dependency from Java.net, you need to declared a remote repository in your pom.xml file like this

<repositories>

<repository>

<id>java.net</id>

<url>https://maven.xxx.net/content/repositories/public/</url>

</repository>

</repositories>

POM:

POM stands for Project Object Model. It is fundamental Unit of Work in Maven. It is an XML file. It always resides in the base directory of the project as pom.xml.

The POM contains information about the project and various configuration detail used by Maven to build the project(s).

POM also contains the following:

◦project dependencies

◦plugins

◦goals

◦build profiles

◦project version

◦developers

mailing list

POM Example:

<project xmlns=”http://maven.apache.org/POM/4.0.0″ mlns: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.companyname.project-group</groupId>

<artifactId>project</artifactId>

<version>1.0</version> </project>

groupId: This is an Id of project’s group. This is generally unique amongst an organization or a project. For example, a banking group com.test.testbank has all bank related projects.

artifactId :This is an Id of the project. This is generally name of the project. For example, online Banking. Along with the groupId, the artifactId defines the artifact’s location within the repository.

Version: This is the version of the project. Along with the groupId, It is used within an artifact’s repository to separate versions from each other. For example:
com.test.testbank:online-banking:1.0 

com.test.testbank:online-banking:1.1

 Maven’s dependency mechanism::

Maven’s dependency mechanism help to download all the necessary dependency libraries automatically, and maintain the version upgrade as well.

In traditional way

1.Visit http://logging.apache.org/log4j/

2.Download the Log4j jar library

3.Copy jar to project classpath

4.Include it into your project dependency manually

5.All manages by yourself, you need to do everything

6.If there is a Log4j version upgrade, you need to repeat above steps again.

In Maven way

◦You need to know the log4j Maven coordinates, for example   <groupId>log4j</groupId>

            <artifactId>log4j</artifactId>

  <version>1.2.14</version>

◦It will download the log4j version 1.2.14 library automatically. If the “version” tag is ignored, it will upgrade the library automatically when there is a newer version.

◦Declares Maven coordinates into pom.xml file.

  <dependencies>

   <dependency>

     <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.14</version> </dependency> </dependencies>

◦When Maven is compiling or building, the log4j jar will be downloaded automatically and put it into your Maven local repository.

◦All manages by Maven.

When you build a Maven’s project, the pom.xml file will be parsed, if it see the log4j Maven coordinate, then Maven search the log4j library in this order

1.Search log4j in Maven local repository.

2.Search log4j in Maven central repository.

3.Search log4j in Maven remote repository (if defined in pom.xml).

This Maven dependency library management is a very nice tool, and save you a lot of work.

How to find the Maven coordinates?
Visit this Maven center repository(http://search.maven.org/), search the jar you want to download.

Build Life Cycle:

The default lifecycle has the following build phases:

validate – validate the project is correct and all necessary information is available

compile – compile the source code of the project

test – test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed

package – take the compiled code and package it in its distributable format, such as a JAR.

integration-test – process and deploy the package if necessary into an environment where integration tests can be run

verify – run any checks to verify the package is valid and meets quality criteria

install – install the package into the local repository, for use as a dependency in other projects locally

deploy – done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.

Maven Plugins:

Maven is actually a plugin execution framework where every task is actually done by plugins.

Clean: Clean up target after the build. Deletes the target directory.

Compiler: Compiles Java source files.

Surefile: Run the JUnit unit tests. Creates test reports.

Jar: Builds a JAR file from the current project.

War: Builds a WAR file from the current project.

Javadoc: Generates Javadoc for the project.

Maven uses archetype plugins to create projects

Eclipse provides an excellent plugin m2eclipse which seamlessly integrates Maven and Eclipse together.

Use maven-archetype-webapp

 

Leave a Reply

Your email address will not be published. Required fields are marked *