6
Maven What is Maven ? Common usage of Maven 1) as a Build Tool (Similar to Ant) 2) as a Projet Management Tool Common problems and activities 1) Multiple Jars : !or e"am#le if $e are using %i&ernate and S#ring ' $e $ill need to inlude all the ars  Sometimes $e don*t +no$ the name of the ars or if all the ars are inluded et  Maven hel#s us in this 2) Dependencies and Versions :  Su##ose a jar has a d e#enden, on another jar $e need to ma+e sure to lose all the de#endenies (ie su##l, all de#endant jars ) -ue to di.erent versions ' $e need to math the right version of de#endent jars /) Project Structure : We reate a diretor, Struture for our #rojet !or e"am#le ' for a $e& #rojet $e need W0B3!' li&raries 4) Build Publish and Deploy. Maven Setup : 1 5o to mavenaa#aheorg and do$nload the &inar, 6i# 7le and e"trat it 2 3o$ $e need to setu# t$o environment varia&les8 1) M29%:M0 ; Path $here ,ou have e"trated Maven C8<=sers<sravi<-ouments<ava<a#ahemaven/>4&in<a#ahe maven/>4 2) Add the #ath $here Maven*s &in is loated to the #ath varia&le set #ath;C8<=sers<sravi<-ouments<ava<a#ahemaven/>4 &in<a#ahemaven/>4<&in@#ath@ / To test if maven is $or+ing 7le ' e"eute a sim#le maven ommand for e"am#le mvn --version   ou should see the &elo$ o ut#ut C8<=sers<sravimvn version A#ahe Maven />4 (r12/2// 2>12>11 >/8448DE>D>>) Maven home8 C8<=sers<sravi<-ouments<ava<a#ahemaven/>4&in<a#ahe maven/ >4<&in<  ava vers ion8 1>9>' v endor8 :r ale Cor#or ation 1 F Page

Maven Rc Notes

  • Upload
    sxurdc

  • View
    214

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Maven Rc Notes

7/26/2019 Maven Rc Notes

http://slidepdf.com/reader/full/maven-rc-notes 1/6

MavenWhat is Maven ?

Common usage of Maven

1) as a Build Tool (Similar to Ant)2) as a Proje t Management Tool

Common problems and activities

1) Multiple Jars : !or e"am#le if $e are using %i&ernate and S#ring ' $e $ill needto in lude all the ars Sometimes $e don*t +no$ the name of the ars or if all the arsare in luded et Maven hel#s us in this

2) Dependencies and Versions : Su##ose a jar has a de#enden , on another jar

$e need to ma+e sure to lose all the de#enden ies (i e su##l, all de#endant jars )-ue to di.erent versions ' $e need to mat h the right version of de#endent jars

/) Project Structure : We reate a dire tor, Stru ture for our #roje t !or e"am#le' for a $e& #roje t $e need W0B 3!' li&raries

4) Build Publish and Deploy.

Maven Setup :

1 5o to maven aa#a he org and do$nload the &inar, 6i# 7le and e"tra t it

2 3o$ $e need to setu# t$o environment varia&les8

1) M29%:M0 ; Path $here ,ou have e"tra ted MavenC8<=sers<sravi<-o uments< ava<a#a he maven / > 4 &in<a#a he

maven / > 42) Add the #ath $here Maven*s &in is lo ated to the #ath varia&le

set #ath;C8<=sers<sravi<-o uments< ava<a#a he maven / > 4&in<a#a he maven / > 4<&in @#ath@

/ To test if maven is $or+ing 7le ' e"e ute a sim#le maven ommandfor e"am#le mvn --version

ou should see the &elo$ out#ut

C8<=sers<sravi mvn versionA#a he Maven / > 4 (r12/2// 2>12 >1 1 >/8448DE >D>>)Maven home8 C8<=sers<sravi<-o uments< ava<a#a he maven / > 4 &in<a#a hemaven /> 4<&in<

ava version8 1 >9> ' vendor8 :ra le Cor#oration

1 F P a g e

Page 2: Maven Rc Notes

7/26/2019 Maven Rc Notes

http://slidepdf.com/reader/full/maven-rc-notes 2/6

ava home8 C8<Program !iles< ava<jd+1 >9> <jre-efault lo ale8 en9=S' #latform en oding8 C#12D2:S name8 G$indo$s G' version8 GE 1G' ar h8 GamdE4G' famil,8 G$indo$sG

Maven uses re#ositories(for &uild tas+s) There are re#ositories availa&leonline(internet) and maven tal+ to these re#ositories ' so $e need to &e onne tedto nternet $hile $or+ing $ith Maven

sin! Maven

!irst advantage Maven #rovides regarding Proje t Stru ture

1) reate a dire tor, m+dir maventest2) d maventest

/) mvn ar het,#e8generate

Maven $ill do$nload all the reHuired #lugin*s and a #rom#t is dis#la,ed tosele t the ar he t,#e1>E is default

4) sele t the default and enterD) sele t the default version and enterE) -e7ne value for #ro#ert, * !roup"d *8 8

This $ould &e similar to #a +age name that ,ou $ould reate give org rajesh java&rains

) -e7ne value for #ro#ert, #arti$act"d#: 8 this is analogous to a lass name $hile grou# id is analogous to a#a +age name

give MavenTestA## A dire tor, $ith the Artifa t d name s#e i7ed $ill&e reated

ArtifactId will be the name of the Jar or War le .

%he !roup "d and arti$act "d are de&ned $or the complete application .

I) -e7ne value for #ro#ert, *version*8 1 > S3APS%:T8 8sele t default

J) -e7ne value for #ro#ert, *#a +age*8 org rajesh java&rains8 8 0nter 'ou can speci$y your pac(a!e name here ) the de$ault is the !roup id1>) Con7rm

So *hat has Maven Done

1) t has reated a dire tor, $ith the Artifa t d ,ou s#e i7ed 8 MavenTestA##2) d MavenTestA##/) dir

2 F P a g e

Page 3: Maven Rc Notes

7/26/2019 Maven Rc Notes

http://slidepdf.com/reader/full/maven-rc-notes 3/6

4) ou $ill see a folder alled sr and a 7le #om "ml (#om stands for #roje t o&je tmodel)D) d sr ,ou $ill see t$o folders main and test

sr KmainKjavaKor!+rajesh+javabrainsor!+rajesh+javabrains is the pac(a!e name *e speci&ed in step , .

ets loo( at pom. ml

L#roje t "mlns;Ghtt#8KKmaven a#a he orgKP:MK4 > >G"mlns8"si;Ghtt#8KK$$$ $/ orgK2>>1K MNS hema instan eG "si8s hemaNo ation;Ghtt#8KKmaven a#a he orgKP:MK4 > >htt#8KKmaven a#a he orgK"sdKmaven 4 > > "sdG LmodelOersion 4 > >LKmodelOersion

Lgrou# d org rajesh java&rainsLKgrou# d Lartifa t d MavenTestA##LKartifa t d Maven uses it to refer to this a##li ation Lversion 1 > S3APS%:TLKversion L#a +aging jarLK#a +aging --- means that application *ill be a jar &le/it is de$ault0

Lname MavenTestA##LKname 3ame $e use to refer to a##li ation ' Can &edi.erent than Artifa t Lurl htt#8KKmaven a#a he orgLKurl

L#ro#erties L#roje t &uild sour e0n oding =T! ILK#roje t &uild sour e0n oding LK#ro#erties

Lde#enden ies Lde#enden , Lgrou# d junitLKgrou# d if junit is de#endent on other jars $e dont have tomention Maven $ill

Lartifa t d junitLKartifa t d ta+e are of it Lversion / I 1LKversion Ls o#e testLKs o#e LKde#enden , LKde#enden iesLK#roje t

Compilin! usin! Maven

1. "norder to compile usin! Maven) *e need to be in the directory *herepom. ml is there

mvn compile

2 Maven ta+es are of do$nloading all the related de#enden ies that $e mentionedin the #om "ml

/ F P a g e

Page 4: Maven Rc Notes

7/26/2019 Maven Rc Notes

http://slidepdf.com/reader/full/maven-rc-notes 4/6

/ :n e the om#ilation is done $e need to #a +age it &, using the ommand mvnpac(a!e "t *ill create a jar &le /in this case 0 under the tar!et directory/Maven%est2pp-1.3-S42PS56%.jar0 "t *ill create a tar!et directory .

Maven *ould also run the junit %estcases .

7 ecute the class java # targetK MavenTestA## 1 > S3APS%:T jar org rajesh java&rains A##

Maven Basics

A repository in Maven is used to hold build artifacts and dependencies of varying types.

Maven installed on our machine tal(s tothe repository.

when we excuted mvn archetype:genratemaven connects to the Maven repository and get sthe arche type info and generates the dir structure.

When $e om#ile ' using mvn om#ile ' maven reads the #om "ml to get thede#enden , information t then onne ts to the G-e#enden , nfoG in the Maven

e#ositor, and do$nloads all the ar*s reHuired and then om#iles all ava Classes

When $e reHuest something from Maven ' it 7rst he +s the lo al re#ositor, to see 'if it has $hat it needed ' if it does not 7nd then it onne ts to online re#ositor, anddo$nload the needed artifa ts to the lo al re#ositor, and then uses them

4 F P a g e

Maven 8epository/on"nternet0

-e#enden, nfo

Ar he T,#enfo

Maven

Development 7nvironment/our Machine0

#om "ml jars

-o ument Stru ture

Page 5: Maven Rc Notes

7/26/2019 Maven Rc Notes

http://slidepdf.com/reader/full/maven-rc-notes 5/6

mvn ar het,#e8generate reates the follo$ing

1) !older Stru ture 2) #om "ml

When ,ou e"e ute mvn ar het,#e8generate ,ou $ill &e as+ed to sele t the follo$ingo#tions

1 ar het,#e 8 This re#resents the t,#e of a##li ation ,ou $ant to reate (fore"amle s#ring jsf hi&ernate) 0ver, ar het,#e has anasso iated num&er ,ou need to enter the num&er 2 5rou# d 8

/ Artifa t d 8 this is the a##li ation name for $e&a##li ation ,ou $ill get aartifa t d $ar for enter#rise a##li ation ear

for sim#le a##li ation jar

4 Oersion 8

D Pa +age 8 3ame of the #a +age to #la e ,our sour e ode What #a +ages thatlass should &elong to

:ne grou# id an ontain more than one Artifa t d A om&ination of grou#id 'artifa tid and version an &e used to identif, an artifa t

#om "ml has the details a&out the artifa t that is &eing #rodu ed and it has list ofde#enden ies sin e an artifa t (li+e a jar or $ar 7le) is uniHuel, identi7ed &, a om&ination of

• grou# d• Atrifa t d• Oersion d

n the de#enden , se tion for ea h de#enden , $e have to #rovide the a&ove /information

:n e $e reate our #roje t dire tor, stru ture ' $e $rite out ode and also u#date

the #om "ml $ith the de#enden , information n order to om#ile the ode or to#a +age the ode (i e to a jar or a $ar 7le ) ,ou go to the dire tor, $here #om "mlis stored and e"e ute the mvn om#ile or mvn #a +age ommands

Maven Build Phases1 validate2 om#ile/ test runs the junit test ases4 #a +age reates the jar or $ar 7les

D FP a g e

Maven Co ordinates

Page 6: Maven Rc Notes

7/26/2019 Maven Rc Notes

http://slidepdf.com/reader/full/maven-rc-notes 6/6

D install (installs the artifa t in lo al re#ositor,)E de#lo, (installsK#u&lishes the artifa t in online re#ositor,)

$hen ,ou e"e ute a #hase su h as mvn #a +age ' all the #re eding #hases aree"e uted automati all, So in this ase validate om#ile and test #hases are run automati all,

$hen ,ou #a +age ,ou a##li ation ' maven reates a dire tor, named target and#la es the jar or $ar 7le in it

5o* to add a dependency 9

mvn lean $ill delete the target folder that mvn reated

:n e ,ou

E F P a g e