-
Notifications
You must be signed in to change notification settings - Fork 16
/
chap-maven.lyx
1171 lines (853 loc) · 22.1 KB
/
chap-maven.lyx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#LyX 1.6.1 created this file. For more info see http://www.lyx.org/
\lyxformat 345
\begin_document
\begin_header
\textclass book
\use_default_options false
\language english
\inputencoding auto
\font_roman default
\font_sans default
\font_typewriter default
\font_default_family default
\font_sc false
\font_osf false
\font_sf_scale 100
\font_tt_scale 100
\graphics default
\paperfontsize default
\spacing single
\use_hyperref false
\papersize default
\use_geometry false
\use_amsmath 1
\use_esint 1
\cite_engine basic
\use_bibtopic false
\paperorientation portrait
\secnumdepth 3
\tocdepth 3
\paragraph_separation indent
\defskip medskip
\quotes_language english
\papercolumns 1
\papersides 1
\paperpagestyle default
\bullet 0 0 6 -1
\tracking_changes false
\output_changes false
\author ""
\author ""
\end_header
\begin_body
\begin_layout Chapter
A Brief Tour of Maven
\begin_inset Index
status collapsed
\begin_layout Plain Layout
Maven
\end_layout
\end_inset
\begin_inset CommandInset label
LatexCommand label
name "cha:Maven"
\end_inset
\end_layout
\begin_layout Standard
In this chapter we'll discuss the Maven build tool and some of the basics
of configuration and usage.
Maven is what Lift uses for build management, so becoming acquainted with
Maven is important to getting the most out of Lift.
If you're already familiar with Maven you can safely skip this chapter.
\end_layout
\begin_layout Section
What is Maven?
\end_layout
\begin_layout Standard
Maven is a project management tool, as opposed to simply a build tool.
The Maven site
\begin_inset Foot
status open
\begin_layout Plain Layout
\begin_inset CommandInset href
LatexCommand href
target "http://maven.apache.org/"
\end_inset
\end_layout
\end_inset
describes the goals of Maven as:
\end_layout
\begin_layout Itemize
Make the build process easy
\end_layout
\begin_layout Itemize
Provide a uniform build system
\end_layout
\begin_layout Itemize
Provide quality project information
\end_layout
\begin_layout Itemize
Provide guidelines for best practices
\end_layout
\begin_layout Itemize
Allow transparent migration to new features
\end_layout
\begin_layout Standard
As a project management tool, Maven goes beyond just controlling compilation
of your code.
By default, Maven comes equipped not only to perform development-centric
tasks, but it can generate documentation from your code and for your project
website.
Everything in Maven is controlled via the pom.xml (Project Object Model)
file, which contains both information and configuration details on the
project.
We'll be covering some of the basic aspects of the POM through the rest
of this chapter
\begin_inset Foot
status open
\begin_layout Plain Layout
A complete POM reference is available at
\begin_inset CommandInset href
LatexCommand href
target "http://maven.apache.org/pom.html"
\end_inset
\end_layout
\end_inset
.
\end_layout
\begin_layout Section
Lifecycles, Phases and Goals
\end_layout
\begin_layout Standard
Maven is designed around the concept of project lifecycles.
While you can define your own, there are three built-in lifecycles:
\family typewriter
default, clean
\family default
and
\family typewriter
site.
\family default
The
\family typewriter
default
\family default
lifecycle builds and deploys your project.
The
\family typewriter
clean
\family default
lifecycle cleans (deletes) compiled objects or anything else that needs
to be removed or reset to get the project to a pristine pre-build state.
Finally, the
\family typewriter
site
\family default
lifecycle generates the project documentation.
\end_layout
\begin_layout Standard
Within each lifecycle there are a number of phases that define various points
in the development process.
The most interesting lifecycle (from the perspective of writing code) is
\family typewriter
default
\family default
.
The most commonly used phases in the default lifecycle are
\begin_inset Foot
status open
\begin_layout Plain Layout
A full listing of lifecycles and their phases is at
\begin_inset CommandInset href
LatexCommand href
target "http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html"
\end_inset
\end_layout
\end_inset
:
\end_layout
\begin_layout Itemize
\family typewriter
compile
\family default
- compiles the main source code of the project
\end_layout
\begin_layout Itemize
\family typewriter
test
\family default
- tests the main code using a suitable unit testing framework.
These tests should not require that the code is packaged or deployed.
This phase implicitly calls the testCompile goal to compile the test case
source code
\end_layout
\begin_layout Itemize
\family typewriter
package
\family default
- packages the compiled code into its distributable format, such as a JAR.
The POM controls how a project is packaged through the
\family typewriter
<packaging/>
\family default
element
\end_layout
\begin_layout Itemize
\family typewriter
install
\family default
- installs the package into the local repository (see section
\begin_inset CommandInset ref
LatexCommand ref
reference "sec:Repositories"
\end_inset
), for use as a dependency in other projects locally
\end_layout
\begin_layout Itemize
\family typewriter
deploy
\family default
- used in an integration or release environment.
Copies the final package to the remote repository for sharing with other
developers and projects.
\end_layout
\begin_layout Standard
Maven is typically run from the command line
\begin_inset Foot
status open
\begin_layout Plain Layout
There are IDE plugins for Maven for most major IDEs as well
\end_layout
\end_inset
by executing command
\begin_inset Quotes eld
\end_inset
\family typewriter
mvn <phase>
\family default
\begin_inset Quotes erd
\end_inset
, where
\family typewriter
<phase>
\family default
is one of the phases listed above.
Since phases are defined in order, all phases up to the one you specify
will be run.
For example, if you want to package your code, simply run
\begin_inset Quotes eld
\end_inset
\family typewriter
mvn package
\family default
\begin_inset Quotes erd
\end_inset
and the
\family typewriter
compile
\family default
and
\family typewriter
test
\family default
phases will automatically be run.
You can also execute specific goals for the various plugins that Maven
uses.
Execution of a specific goal is done with the command
\begin_inset Quotes eld
\end_inset
\family typewriter
mvn <plugin>:<goal>
\family default
\begin_inset Quotes erd
\end_inset
.
For instance, the compile phase actually calls the
\family typewriter
compiler:compile
\family default
goal by default.
A common usage of executing a goal for Lift is the
\family typewriter
jetty:run
\family default
goal, which compiles all of your code and then runs an instance of the
Jetty
\begin_inset Foot
status open
\begin_layout Plain Layout
\begin_inset CommandInset href
LatexCommand href
target "http://www.mortbay.org/jetty/"
\end_inset
\end_layout
\end_inset
web server so that you can exercise your app.
The jetty plugin is not directly bound to any lifecycle or phase, so we
have to execute the goal directly.
\end_layout
\begin_layout Standard
One final note is that you can specify multiple phases/goals in one command
line, and Maven will execute them in order.
This is useful, for instance, if you want to do a clean build of your project.
Simply run
\begin_inset Quotes eld
\end_inset
\family typewriter
mvn clean jetty:run
\family default
\begin_inset Quotes erd
\end_inset
and the
\family typewriter
clean
\family default
lifecycle will run, followed by the
\family typewriter
jetty:run
\family default
goal (and all of the prerequisites for jetty:run, such as compile).
\end_layout
\begin_layout Section
Repositories
\begin_inset CommandInset label
LatexCommand label
name "sec:Repositories"
\end_inset
\end_layout
\begin_layout Standard
Repositories are one of the key features of Maven.
A repository is a location that contains plugins and packages for your
project to use.
There are two types of repository: local and remote.
Your local repository is, as the name suggests, local to your machine,
and represents a cache of artifacts downloaded from remote repositories
as well as packages that you've installed from your own projects.
The default locations of your local repo will be:
\end_layout
\begin_layout Itemize
Unix:
\family typewriter
~/.m2/repository
\end_layout
\begin_layout Itemize
Windows:
\family typewriter
C:
\backslash
Documents and Settings
\backslash
<user>
\backslash
.m2
\backslash
repository
\end_layout
\begin_layout Standard
You can override the local repository location by setting the M2_REPO environmen
t variable, or by editing the <home>/.m2/settings.xml file
\begin_inset Foot
status open
\begin_layout Plain Layout
Details on customizing your Maven installation are available at
\begin_inset CommandInset href
LatexCommand href
target "http://maven.apache.org/settings.html"
\end_inset
\end_layout
\end_inset
.
\end_layout
\begin_layout Standard
Remote repositories are repositories that are reachable via protocols like
http and ftp and are generally where you will find the dependencies needed
for your projects.
Repositories are defined in the POM; listing
\begin_inset CommandInset ref
LatexCommand ref
reference "sec:Repositories"
\end_inset
shows the definition of the scala-tools.org release repository where Lift
is found
\begin_inset Foot
status open
\begin_layout Plain Layout
scala-tools.org also has a snapshots repository where nightly builds of the
scala-tools projects are kept
\end_layout
\end_inset
.
Maven has an internal default set of repositories so usually you don't
need to define too many extra repos.
\end_layout
\begin_layout Standard
\begin_inset listings
lstparams "language=XML"
inline false
status open
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
Defining a repository
\begin_inset CommandInset label
LatexCommand label
name "lst:Defining-a-repository"
\end_inset
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
<repositories>
\end_layout
\begin_layout Plain Layout
<repository>
\end_layout
\begin_layout Plain Layout
<id>scala-tools.org</id>
\end_layout
\begin_layout Plain Layout
<name>Scala Tools Maven2 Repository</name>
\end_layout
\begin_layout Plain Layout
<url>http://scala-tools.org/repo-releases</url>
\end_layout
\begin_layout Plain Layout
</repository>
\end_layout
\begin_layout Plain Layout
</repositories>
\end_layout
\end_inset
\end_layout
\begin_layout Standard
As a final note, sometimes you may not have net access or the remote repos
will be offline for some reason.
In this case, make sure to specify the
\begin_inset Quotes eld
\end_inset
-o
\begin_inset Quotes erd
\end_inset
(offline) flag so that Maven skips checking the remote repos.
\end_layout
\begin_layout Section
Plugins
\end_layout
\begin_layout Standard
Plugins add functionality to the Maven build system.
Lift is written in Scala, so the first plugin that we need to add is the
Maven Scala Plugin; this adds the ability to compile Scala code in your
project.
Listing
\begin_inset CommandInset ref
LatexCommand ref
reference "lst:Configuring-the-Maven-scala"
\end_inset
shows how we configure the plugin in the pom.xml file for a Lift application.
You can see the Scala plugin adds a
\family typewriter
compile
\family default
and
\family typewriter
testCompile
\family default
goal for the build phase, which makes Maven execute this plugin when those
goals are called (explicitly or implicitly).
In addition, the configuration element allows you to set properties of
the plugin executions; in this case, we're explicitly specifying the version
of Scala that should be used for compilation.
\end_layout
\begin_layout Standard
\begin_inset listings
lstparams "language=XML"
inline false
status open
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
Configuring the Maven Scala Plugin
\begin_inset CommandInset label
LatexCommand label
name "lst:Configuring-the-Maven-scala"
\end_inset
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
<plugin>
\end_layout
\begin_layout Plain Layout
<groupId>org.scala-tools</groupId>
\end_layout
\begin_layout Plain Layout
<artifactId>maven-scala-plugin</artifactId>
\end_layout
\begin_layout Plain Layout
<executions>
\end_layout
\begin_layout Plain Layout
<execution>
\end_layout
\begin_layout Plain Layout
<goals>
\end_layout
\begin_layout Plain Layout
<goal>compile</goal>
\end_layout
\begin_layout Plain Layout
<goal>testCompile</goal>
\end_layout
\begin_layout Plain Layout
</goals>
\end_layout
\begin_layout Plain Layout
</execution>
\end_layout
\begin_layout Plain Layout
</executions>
\end_layout
\begin_layout Plain Layout
<configuration>
\end_layout
\begin_layout Plain Layout
<scalaVersion>${scala.version}</scalaVersion>
\end_layout
\begin_layout Plain Layout
</configuration>
\end_layout
\begin_layout Plain Layout
</plugin>
\end_layout
\end_inset
\end_layout
\begin_layout Section
Dependencies
\end_layout
\begin_layout Standard
Dependency management is one of the more useful features of Maven.
Listing
\begin_inset CommandInset ref
LatexCommand ref
reference "lst:Adding-a-Dependency"
\end_inset
shows a declaration of the Jetty dependency for the default Lift application.
The details of the specification are straightforward:
\end_layout
\begin_layout Itemize
The groupId and artifactId specify the artifact.
A given group may have many artifacts under it; for instance, Lift uses
net.liftweb for its groupId and the core artifacts are lift-core and life-util
\end_layout
\begin_layout Itemize
The version is specified either directly or with a range, as we've used
in this example.
A range is defined as <left>min,max<right> where left and right indicate
an inclusive or exclusive range: [ and ] are inclusive, ( and ) are exclusive.
Omitting a version in a range leaves that portion of the range unbounded.
Here we configure the pom so that Jetty 6.1.6 or higher is used
\end_layout
\begin_layout Itemize
The scope of the dependency is optional
\begin_inset Foot
status open
\begin_layout Plain Layout
Scope is discussed in detail at
\begin_inset CommandInset href
LatexCommand href
target "http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html"
\end_inset
\end_layout
\end_inset
, and controls exactly where the dependency is used.
In this case we specify a test scope which means that the package will
only be available to test phases
\end_layout
\begin_layout Standard
\begin_inset listings
lstparams "language=XML"
inline false
status open
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
Adding a Dependency
\begin_inset CommandInset label
LatexCommand label
name "lst:Adding-a-Dependency"
\end_inset
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
<dependency>
\end_layout
\begin_layout Plain Layout
<groupId>org.mortbay.jetty</groupId>
\end_layout
\begin_layout Plain Layout
<artifactId>jetty</artifactId>
\end_layout
\begin_layout Plain Layout
<version>[6.1.6,)</version>
\end_layout
\begin_layout Plain Layout
<scope>test</scope>
\end_layout
\begin_layout Plain Layout
</dependency>
\end_layout
\end_inset
\end_layout
\begin_layout Subsection
Adding a Dependency
\begin_inset CommandInset label
LatexCommand label
name "sub:Adding-a-Dependency"
\end_inset
\end_layout
\begin_layout Standard
As an example, let's say that you'd like to add a new library and you want
Maven to make sure you've got the most up-to-date version.
We're going to add Configgy
\begin_inset Foot
status open
\begin_layout Plain Layout
Configgy's home is
\begin_inset CommandInset href
LatexCommand href
target "http://www.lag.net/configgy/"
\end_inset
\end_layout
\end_inset
as a dependency.
Configgy is
\begin_inset Quotes eld
\end_inset
a library for handling config files and logging for a scala daemon.
The idea is that it should be simple and straightforward, allowing you
to plug it in and get started quickly, writing small useful daemons without
entering the shadowy world of java frameworks.
\begin_inset Quotes erd
\end_inset
\end_layout
\begin_layout Standard
First we need to tell Maven where we can get Configgy, so in the
\family typewriter
<repositories>
\family default
section add the following:
\end_layout
\begin_layout Standard
\begin_inset listings
lstparams "language=XML"
inline false
status open
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
Adding the Configgy repo
\begin_inset CommandInset label
LatexCommand label
name "lst:Adding-the-Configgy-repo"
\end_inset
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
<repository>
\end_layout
\begin_layout Plain Layout
<id>http://www.lag.net/repo/</id>
\end_layout
\begin_layout Plain Layout
<name>http://www.lag.net/repo/</name>
\end_layout
\begin_layout Plain Layout
<url>http://www.lag.net/repo/</url>
\end_layout
\begin_layout Plain Layout
</repository>
\end_layout
\end_inset
\end_layout
\begin_layout Standard
Then in the
\family typewriter
<dependencies>
\family default
section add:
\end_layout
\begin_layout Standard
\begin_inset listings
lstparams "language=XML"
inline false
status open
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
Adding the Configgy dependency
\begin_inset CommandInset label
LatexCommand label
name "lst:Adding-the-Configgy-dep"
\end_inset
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
<dependency>
\end_layout
\begin_layout Plain Layout
<groupid>net.lag</groupid>
\end_layout
\begin_layout Plain Layout
<artifactid>configgy</artifactid>
\end_layout
\begin_layout Plain Layout
<version>[1.2,)</version>
\end_layout
\begin_layout Plain Layout
</dependency>
\end_layout
\end_inset
\end_layout
\begin_layout Standard
That's it, you're done.
The next time you run Maven for your project, it will pull down the Configgy
jars into your local repository.
Maven will periodically check for new versions of dependencies when you
build, but you can always force a check with the