Maven

What is Maven

    • Maven is Project Management tool

    • Used for build management and dependencies

Dependency Jar Problem

    • When building your Java Project, you may need additional JAR files e.g. Spring, Hibernate, Commons Logging, JSON etc

    • One approach is to download JAR files from each project web site

    • Manually add the JAR files to your build path/classpath

    • This is cumbersome manual approach to download JAR from each project site and then copy

Maven Solution

    • Tell Maven the dependencies (the projects you are working with) e.g. Spring, Hibernate

    • Maven will go out and download the JAR files for those projects for you

    • Maven will make those JAR files available during compile/run time

    • Maven is like a helping friend

    • Project Config file (list of dependencies i.e. shopping list :))

Maven Workflow

    • Maven will read the project config file and

    • Maven will check Maven local repository

    • Maven will check Maven central repository - remote for missing jars in local repo

    • Maven will copy JARs from Maven central to Maven local repo

    • Maven will pull all JARs to local project

    • Maven will build and run the project

Handling JAR Dependencies

    • When Maven retrieves a project dependency

    • It will also download supporting dependency, e.g. Spring depends on commons-logging

    • Maven will handle like auto-matically

Building and Running

    • When you build and run your app

    • Maven will handle class/build path for you

    • Based on config file, Maven will add JAR files accordingly

Maven Cheat Sheet

Standard Directory Structure

    • Maven provides standard directory structure

    • It makes easy to find the files

    • Most major IDEs have built-in support for Maven projects

    • Maven projects are portable across IDEs

Advantages

    • Dependency Management

    • Building and running the project. No more build\classpath issues

    • Standard directory structure

Maven Key Concepts

POM File - pom.xml

    • Project Object Model File (POM File)

    • Configuration file for the project (shopping list for Maven)

    • POM file is located in the root of Maven project

    • POM file has 3 sections

Project Coordinates

    • Project Coordinates uniquely identify a project

    • groupId: Name of organization, convention is to use reverse domain name

    • artifactId: Name of the project

    • version: A specific release version, 1.0-SNAPSHOT if project is under active development

    • Example

    • <groupId>org.springframework</groupId>

    • <artifactId>spring-context</artifactId>

    • <version>5.0.0.RELEASE</version>

Add Dependency

    • Under <dependency> tag, project co-ordinates are specified

    • Version is optional

    • Referred as GAV

    • Dependency co-ordinates could be found at https://search.maven.org

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context</artifactId>

<version>5.0.0.RELEASE</version>

</dependency>

Maven Archetypes

    • Archetypes can be used to create new Maven projects

    • Contains template file for a given Maven project (Java project, Web project)

    • Could be used from command line or IDE to create Maven project

    • Eclipse: File -> New Maven Project (Choose Archetype)

Create a Simple Project (Eclipse-Maven)

    • Open Eclipse and check if m2e plugin is installed

    • Help->Install New Software->Already installed->Filter(m2e) (Maven integration for Eclipse)

    • Create Maven Project

    • File->New->Other->Maven Project->Next->Filter (quickstart)->Next

    • Fill Group-id, Artifact-id, Version and finish

    • Project created with pom.xml and App.java

    • code is under src-main-java

    • Run App.java as java application

    • Populate java version, target and source

<properties>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<maven.compiler.target>1.8</maven.compiler.target>

<maven.compiler.source>1.8</maven.compiler.source>

</properties>

Repository

Local Repository

    • Located on developer's computer

    • Windows c:\Users\<user home directory>/.m2/repository

    • Mac\Linux ~/.m2/repository

    • Maven search local repository before going out to central repository

Central Repository

    • By default, Maven will search Maven's Central Repository (remote)

    • https://repo.maven.apache.org/maven2

    • Requires an Internet connection

    • Once files are downloaded, they are stored in local repository

References