Maven

Maven tutorial provides basic and advanced concepts of apache maven technology. Our maven tutorial is developed for beginners and professionals.

Maven is a powerful project management tool that is based on POM (project object model). It is used for projects build, dependency and documentation.

It simplifies the build process like ANT. But it is too much advanced than ANT.

Docs
Maven
Maven is mostly used for the java projects to build web application packages. Maven provides different features which make it easy to build the web-application packages we manage complex projects easily.
  1. POM Files: Project Object Model(POM) Files are XML file that contains information related to the project and configuration information such as dependencies, source directory, plugin, goals etc. used by Maven to build the project. When you should execute a maven command you give maven a POM file to execute the commands. Maven reads pom.xml file to accomplish its configuration and operations.
  2. Dependencies and Repositories: Dependencies are external Java libraries required for Project and repositories are directories of packaged JAR files. The local repository is just a directory on your machine's hard drive. If the dependencies are not found in the local Maven repository, Maven downloads them from a central Maven repository and puts them in your local repository.
  3. Build Life Cycles, Phases, and Goals: A build life cycle consists of a sequence of build phases, and each build phase consists of a sequence of goals. Maven command is the name of a build lifecycle, phase, or goal. If a lifecycle is requested executed by giving the maven command, all build phases in that life cycle are executed also. If a build phase is requested executed, all build phases before it in the defined sequence are executed too.
  4. Build Profiles: Build profiles a set of configuration values that allows you to build your project using different configurations. For example, you may need to build your project for your local computer, for development and test. To enable different builds you can add different build profiles to your POM files using its profiles elements which are triggered in a variety of ways.
  5. Build Plugins: Build plugins are used to perform a specific goal. you can add a plugin to the POM file. Maven has some standard plugins you can use, and you can also implement your own in Java.
  1. check maven installation and version
                         
                            mvn -v
                            
    
  2. To generate the archetype (Non-interactive)
                         
                            mvn archetype:generate -DgroupId=com.soham.app -DartifactId=myapp -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DinteractiveMode=false
                            
    
  3. To generate the archetype (interactive)
                         
                            mvn archetype:generate
                            
    
  4. What is Build Tool ?
    What is Build Tool ?
    A build tool takes care of everything for building a process. It does following:

    1) Generates source code (if auto-generated code is used)
    2) Generates documentation from source code
    3) Compiles source code
    4) Packages compiled code into JAR of ZIP file
    5) Installs the packaged code in local repository, server repository, or central repository
  5. What it does?
    What it does?
    Maven simplifies the above mentioned problems. It does mainly following tasks.
    1. It makes a project easy to build
    2. It provides uniform build process (maven project can be shared by all the maven projects)
    3. It provides project information (log document, cross referenced sources, mailing list, dependency list, unit test reports etc.)
    4. It is easy to migrate for new features of Maven
    Apache Maven helps to manage
    • Builds
    • Documentation
    • Reporing
    • SCMs
    • Releases
    • Distribution
  6. Understanding the problem without Maven
    Understanding the problem without Maven

    There are many problems that we face during the project development. They are discussed below:

    1) Adding set of Jars in each project: In case of struts, spring, hibernate frameworks, we need to add set of jar files in each project. It must include all the dependencies of jars also.

    2) Creating the right project structure: We must create the right project structure in servlet, struts etc, otherwise it will not be executed.

    3) Building and Deploying the project: We must have to build and deploy the project so that it may work.

  7. Difference between Ant and Maven
    Difference between Ant and Maven
    AntMaven
    Ant doesn't has formal conventions, so we need to provide information of the project structure in build.xml file.Maven has a convention to place source code, compiled code etc. So we don't need to provide information about the project structure in pom.xml file.
    Ant is procedural, you need to provide information about what to do and when to do through code. You need to provide order.Maven is declarative, everything you define in the pom.xml file.
    There is no life cycle in Ant.There is life cycle in Maven.
    It is a tool box.It is a framework.
    It is mainly a build tool.It is mainly a project management tool.
    The ant scripts are not reusable.The maven plugins are reusable.
    It is less preferred than Maven.It is more preferred than Ant.
  8. Maven Repository
    Maven Repository
    A maven repository is a directory of packaged JAR file with pom.xml file. Maven searches for dependencies in the repositories. There are 3 types of maven repository:

    1. Local Repository
    2. Central Repository
    3. Remote Repository

    Maven searches for the dependencies in the following order:
    Local repository then Central repository then Remote repository. If dependency is not found in these repositories, maven stops processing and throws an error.
    • Maven Local Repository
      Maven Local Repository

      Maven local repository is located in your local system. It is created by the maven when you run any maven command.


      By default, maven local repository is %USER_HOME%/.m2 directory. For example: C:\Users\SSS IT\.m2.


      We can change the location of maven local repository by changing the settings.xml file. It is located in MAVEN_HOME/conf/settings.xml, for example: E:\apache-maven-3.1.1\conf\settings.xml.
    • Maven Central Repository
      Maven Central Repository

      Maven central repository is located on the web. It has been created by the apache maven community itself.


      The path of central repository is: http://repo1.maven.org/maven2/.


      The central repository contains a lot of common libraries that can be viewed by this url http://search.maven.org/#browse.

    • Maven Remote Repository
      Maven Remote Repository
      Maven remote repository is located on the web. Most of libraries can be missing from the central repository such as JBoss library etc, so we need to define remote repository in pom.xml file.
  9. Maven pom.xml file
    Maven pom.xml file

    POM is an acronym for Project Object Model. The pom.xml file contains information of project and configuration information for the maven to build the project such as dependencies, build directory, source directory, test source directory, plugin, goals etc.

    Maven reads the pom.xml file, then executes the goal.

    Before maven 2, it was named as project.xml file. But, since maven 2 (also in maven 3), it is renamed as pom.xml.

    Elements of maven pom.xml file
    ElementDescription
    project It is the root element of pom.xml file.
    modelVersion It is the sub element of project. It specifies the modelVersion. It should be set to 4.0.0.
    groupId It is the sub element of project. It specifies the id for the project group.
    artifactIdIt is the sub element of project. It specifies the id for the artifact (project). An artifact is something that is either produced or used by a project. Examples of artifacts produced by Maven for a project include: JARs, source and binary distributions, and WARs.
    versionIt is the sub element of project. It specifies the version of the artifact under given group.

    Maven pom.xml file with additional elements
    ElementDescription
    packaging defines packaging type such as jar, war etc.
    name defines name of the maven project.
    url defines url of the project.
    dependenciesdefines dependencies for this project.
    dependencydefines a dependency. It is used inside dependencies.
    scopedefines scope for this maven project. It can be compile, provided, runtime, test and system.
  10. How to build the maven project or how to package maven project?
    How to build the maven project or how to package maven project?

    The mvn package command completes the build life cycle of the maven project such as:

    1. validate
    2. compile
    3. test
    4. package
    5. integration-test
    6. verify
    7. install
    8. deploy

    Now you will see that a jar file is created inside the project/target directory.

                        
                            mvn package