45
Build Automation With maven (By Ankit Gubrani)

Build Automation using Maven

Embed Size (px)

DESCRIPTION

Introduction to Maven

Citation preview

Page 1: Build Automation using Maven

Build Automation With maven(By Ankit Gubrani)

Page 2: Build Automation using Maven

Agenda

q What is maven?q Why maven?q Maven Installation.q How maven works.q The build Lifecycle.q Basics of POM, Profiles, Repositories.q Maven Plugins.q Maven Dependency.q Creating custom maven Plugin.

Page 3: Build Automation using Maven

What is maven ?

Page 4: Build Automation using Maven

Any Idea?

Page 5: Build Automation using Maven

Ok but before that !What is a build tool?

Page 6: Build Automation using Maven

Build Tool

q A build tool is a programming utility that is used whenbuilding a new version of a program.

q Build tools are tools to manage and organise yourbuilds.

Page 7: Build Automation using Maven
Page 8: Build Automation using Maven

Now that you know What Build Tool is. Here is Maven -

Page 9: Build Automation using Maven

Maven

q Maven is a popular open source build tool used primarilyfor Java projects. It was designed to make buildprocess easier task.

q Apart from being a build tool Maven can also managereporting and documentation from a central piece ofinformation.

q It is based on the concept of a project object model(POM).

q Maven is more than just a "build tool" rather is aproject management tool.

Page 10: Build Automation using Maven

Maven provides developers ways to manage following:q Buildsq Documentationq Reportingq Dependenciesq SCMsq Releasesq Distributionq Mailing list

Page 11: Build Automation using Maven

Why Maven if we have other Build tools available ???

Page 12: Build Automation using Maven

Comparison

Here are some of the benefits of using Maven over anyother build (Ant, make etc) :

q Standardized project layout. (Convention overconfiguration unlike ANT)

q Automatic dependency handling.(Ant can't do thisunassisted)

q Large Existing repository.

q Reusability ( through Maven Plugins).

Page 13: Build Automation using Maven

Maven Installation

Page 14: Build Automation using Maven

Installation

q Maven is a Java tool, so you must have Java installed inorder to proceed.

q Follow the instructions on the link for intallation :

http://maven.apache.org/download.cgi#Installation

q Once installation is done type the following command ina terminal or in a command prompt:

mvn –versionIt should print out your installed version of Maven

Page 15: Build Automation using Maven
Page 16: Build Automation using Maven

Maven Build Lifecycle

Page 17: Build Automation using Maven

Lifecycle

q Build Lifecycle is a well defined sequence of phaseswhich define the order in which the goals are to beexecuted. Here phase represents a stage in life cycle.

q When Maven starts building a project, it steps througha defined sequence of phases and executes goals whichare registered with each phase. Maven has followingthree standard lifecycles:

q Cleanq default(or build)q Site

Page 18: Build Automation using Maven

q A Build Lifecycle is Made Up of Phases.

q A Build Phase is Made Up of Plugin Goals.

q A Plugin Goal represents a specific task (finer than abuild phase) which contributes to the building andmanaging of a project.

Page 19: Build Automation using Maven

lPhase-Goal BindinglLifecycle-Phase Binding

Page 20: Build Automation using Maven

POM

Page 21: Build Automation using Maven

lProject Object Model

q POM stands for Project Object Model. It isfundamental Unit of work in Maven.

q It is an XML file. It always resides in the basedirectory of the project as pom.xml.

q The POM contains information about the project andvarious configuration detail used by Maven to build theproject.

Page 22: Build Automation using Maven

lSome of the configuration that can be specified in thePOM are following:

q project dependenciesq pluginsq goalsq build profilesq project versionq developersq mailing list

Page 23: Build Automation using Maven

Maven Repository

Page 24: Build Automation using Maven

q A maven repository is a directory of packaged JAR file withpom.xml file. Maven searches for dependencies in therepositories. There are 3 types of maven repostory :§ Local Repository§ Central Repository§ Remote Repository

q Maven searches for the dependencies in the following order:q Local repository then Central repository then Remote

repository.

Page 25: Build Automation using Maven

q Local Repository :-§ Maven local repository is located in your local

system. It is created by the maven when you runany maven command.

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

q Central Repository :-§ Maven central repository is located on the web. It

has been created by the apache maven communityitself.

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

Page 26: Build Automation using Maven

q Remote Repository :-

§ Maven remote repository is located on the web.Most of libraries can be missing from the centralrepository such as JBoss library etc, so we need todefine remote repository in pom.xml file.

Page 27: Build Automation using Maven

Build Profile

Page 28: Build Automation using Maven

q A Build profile is a set of configuration values which can beused to set or override default values of Maven build. Usinga build profile, you can customize build for differentenvironments such as Production v/s Developmentenvironments.

q Build profiles are majorly of three types :

§ Per Project :- Defined in the project POM file, pom.xml§ Per User :- Defined in Maven settings xml file

(%USER_HOME%/.m2/settings.xml)§ Global :- Defined in Maven global settings xml file

(%M2_HOME%/conf/settings.xml)

Page 29: Build Automation using Maven

Maven Plugins

Page 30: Build Automation using Maven

q Plugins are the central feature of Maven that allow for thereuse of common build logic across multiple projects.

q Maven is actually a plugin execution framework where everytask is actually done by plugins.

q A plugin generally provides a set of goals and which can beexecuted using following syntax:ü mvn [plugin-name]:[goal-name]

q There are plugins for almost every task in maven like pluginsare used to: create jar files, create war files, compile code,unit test code, create project documentation, and on and on.

Page 31: Build Automation using Maven

q Maven provides following two types of Plugins:

1)Build plugins:- They execute during the build and should beconfigured in the <build/> element of pom.xml .2)Reporting plugins:-They execute during the sitegeneration and they should be configured in the<reporting/> element of the pom.xml .

Page 32: Build Automation using Maven

Archetype

Page 33: Build Automation using Maven

lAn archetype is defined as an original pattern or model fromwhich all other things of the same kind are made.

lMaven provides users,a very large list of different types ofproject templates using concept of Archetype. Maven helpsusers to quickly start a new java project using followingcommand :

mvn archetype:generate

lArchetype is a Maven plugin whose task is to create a projectstructure as per its template. Archetype Plugin allows the userto create a Maven project from an existing template called anarchetype.

Archetype

Page 34: Build Automation using Maven

Now that you know basics of maven,Let's create a maven project

Page 35: Build Automation using Maven

q For creating any project maven provides templates(Archetype),using which one can create by selecting any one archetype fromavailable archetypes.

q To start a new Maven project, use the Maven Archetype plugin fromthe command line. Run the following command :-

§ mvn archetype:generate -DarchetypeGroupId=[Group Id] -DarchetypeArtifactId= [Artifact ID]

q You can search for all archetypes provided by maven at therecentral repository (http://search.maven.org/) and all other 3rdparty archetypes at any remote repository like -http://mvnrepository.com

Page 36: Build Automation using Maven

Demo

Page 37: Build Automation using Maven

Maven Snapshots

Page 38: Build Automation using Maven

q SNAPSHOT is a special version that indicates a currentdevelopment copy.

q Unlike regular versions, Maven checks for a newSNAPSHOT version in a remote repository for every build.

q Snapshot vs Version :-q In case of Version, if Maven once downloaded the mentioned

version say data-service:1.0, it will never try to download anewer 1.0 available in repository. To download the updatedcode, artifact version is be upgraded to 1.1.

q In case of SNAPSHOT, Maven will automatically fetch thelatest SNAPSHOT (data-service:1.0-SNAPSHOT)everytime app-ui team build their project.

Page 39: Build Automation using Maven

Hey did you notice ???What we did till now automated the build

process. And this is called Build Automation using Maven

Page 40: Build Automation using Maven

Auto-Generated Project DocumentationUsing maven

Page 41: Build Automation using Maven

q Team communication is an essential part of anyproject. And wasting time looking for technical projectinformation can be costly and frustrating. Clearly, anyIT project will benefit from having its own dedicatedtechnical Website.

q That's where the Maven site generator steps in. Withlittle effort, you can have a professional-quality, lowmaintenance project Website up and running in no time.

q Documentation is generated by running§ mvn site

Page 42: Build Automation using Maven

Any Questions ?

Page 43: Build Automation using Maven
Page 44: Build Automation using Maven

lAbout Me

.about-me{name: Ankit Gubrani !Sr. AEM Developer;email-id: [email protected];LinkedIn: in.linkedin.com/in/ankitgubrani;twitter: @ankitgubrani90;blog: codebrains.blogspot.in;website : codebrains.co.in;

}

Page 45: Build Automation using Maven

Thank YouPlease contact me at : [email protected]