Upload
ankit-gubrani
View
1.677
Download
2
Tags:
Embed Size (px)
DESCRIPTION
Introduction to Maven
Citation preview
Build Automation With maven(By Ankit Gubrani)
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.
What is maven ?
Any Idea?
Ok but before that !What is a build tool?
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.
Now that you know What Build Tool is. Here is 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.
Maven provides developers ways to manage following:q Buildsq Documentationq Reportingq Dependenciesq SCMsq Releasesq Distributionq Mailing list
Why Maven if we have other Build tools available ???
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).
Maven Installation
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
Maven Build Lifecycle
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
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.
lPhase-Goal BindinglLifecycle-Phase Binding
POM
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.
lSome of the configuration that can be specified in thePOM are following:
q project dependenciesq pluginsq goalsq build profilesq project versionq developersq mailing list
Maven Repository
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.
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 .
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.
Build Profile
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)
Maven Plugins
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.
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 .
Archetype
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
Now that you know basics of maven,Let's create a maven project
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
Demo
Maven Snapshots
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.
Hey did you notice ???What we did till now automated the build
process. And this is called Build Automation using Maven
Auto-Generated Project DocumentationUsing 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
Any Questions ?
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;
}
Thank YouPlease contact me at : [email protected]