Slide 1

Slide 1 text

@CGuntur Apache Maven A Peek Under The Hood Chandra Guntur Oct. 2023 1

Slide 2

Slide 2 text

@CGuntur • a powerful build management tool • primarily used to build Java projects • developed in Java • an open source project • follows convention-over-configuration • allows for other language projects to be built What is Apache Maven? 2

Slide 3

Slide 3 text

@CGuntur What is “build management”? • compiling - compile code • packaging executables - create a .jar, .war, .zip etc. • testing - unit.functional/integration testing can be orchestrated • generating documentation - javadoc/site generation capabilities • generating metrics/reports - build metrics, test result reports • deploying - push to repository or to server 3

Slide 4

Slide 4 text

@CGuntur What is “convention-over-configuration”? • a software design paradigm • decreases the number of decisions needed from developer • satisfies the principle of least astonishment • provides sensible default “assumptions” • promotes common structures and flow in a project • an example: directory structure • not just limited to directory structure • instruction set for Apache Maven is via a POM 4

Slide 5

Slide 5 text

@CGuntur What is a POM? 5

Slide 6

Slide 6 text

@CGuntur “POM” is … A Pomeranian? Pomegranate Juice? One of the Pom-poms? 6

Slide 7

Slide 7 text

@CGuntur “POM” is … A pomeranian? Pomegranate Juice? One of the pom poms? • POM lists: • dependencies • plugins • properties • inheritance details • profiles • ... POM is Project Object Model 7

Slide 8

Slide 8 text

@CGuntur “POM” is … Appendix has POM structure details … 8

Slide 9

Slide 9 text

@CGuntur Benefits/usage of Apache Maven? 9

Slide 10

Slide 10 text

@CGuntur Benefits of using Apache Maven • visibility - build logs as evidence • reusability - builds can have composition & hierarchies • verifiability - builds produce test evidence • reproducibility - repeatable builds • maintainability - management of builds is possible • comprehensibility - ease of understanding the build process 10

Slide 11

Slide 11 text

@CGuntur Apache Maven usage (1) • create any auto-generated source code, if needed • generate any documentation from source code • compile source code, display any errors and warnings • test the project running existing tests in source code continued ... 11

Slide 12

Slide 12 text

@CGuntur Apache Maven usage (2) • package compiled code into a .jar or .war or .zip etc. • additionally, package source code into a .jar or .zip • install the packaged code into a repository/server • generate site reports and test evidence • report a build as success or failure 12

Slide 13

Slide 13 text

@CGuntur How does Apache Maven work? 13

Slide 14

Slide 14 text

@CGuntur Watch for notes here How does Apache Maven work? Internet Maven Repository Java (or other lang) Project App Source Code 14

Slide 15

Slide 15 text

@CGuntur Watch for notes here How does Apache Maven work? (1) Internet Maven Repository Java (or other lang) Project Project Object Model (POM) assemble dependencies plugins profiles lifecycle build phases App Source Code • Add a pom (typically a pom.xml file) • Point settings.xml to repository • Include dependencies & plugins • Add properties & override configuration Notes 15

Slide 16

Slide 16 text

@CGuntur Watch for notes here How does Apache Maven work? (2) Internet Maven Repository Java (or other lang) Project Project Object Model (POM) assemble dependencies plugins profiles lifecycle build phases App Source Code Apache Maven 1 16 • Use Terminal or IDE • Navigate to the project root • List the phases (or phases:goals) • Run Apache Maven with above Notes

Slide 17

Slide 17 text

@CGuntur Watch for notes here How does Apache Maven work? (3) Internet Maven Repository Java (or other lang) Project Project Object Model (POM) assemble dependencies plugins profiles lifecycle build phases App Source Code Apache Maven assemble 1 2 • Maven parses the POM • Maven combines the below ... • Various settings • Maven defaults • POM overrides to defaults • … to produce an “effective POM” • Effective POM is read-only • It is the full set of what Maven uses Notes 17

Slide 18

Slide 18 text

@CGuntur Watch for notes here How does Apache Maven work? (4) Internet Maven Repository {Local Repository (local cache) Java (or other lang) Project Project Object Model (POM) assemble dependencies plugins profiles lifecycle build phases App Source Code Apache Maven assemble 1 2 • If you previously ran a build • You may already have a local cache • Maven can use local cache • If you don’t have a local cache • Maven will use internet repository • Local cache will be created • Next run can depend on local cache Notes 18

Slide 19

Slide 19 text

@CGuntur Watch for notes here How does Apache Maven work? (5) Internet Maven Repository {Local Repository (local cache) Java (or other lang) Project Project Object Model (POM) assemble dependencies plugins profiles lifecycle build phases App Source Code Apache Maven assemble 1 2 3 • Maven fetches dependencies & plugins • Transitive dependencies are also fetched • Maven uses local cache if up-to-date • If not up-to-date (e.g. older SNAPSHOT) • Maven goes to non-local repository • Updates/adds to the cache • Next run can rely on local cache Notes 19

Slide 20

Slide 20 text

@CGuntur Watch for notes here How does Apache Maven work? (6) Internet Maven Repository {Local Repository (local cache) Java (or other lang) Project Project Object Model (POM) assemble dependencies plugins profiles lifecycle build phases App Source Code Apache Maven assemble execute 1 2 3 4 • Maven executes phases or phases:goals • Profiles allow any or all of • alternate executions • alternate configurations • environment specificity Notes 20

Slide 21

Slide 21 text

@CGuntur Watch for notes here How does Apache Maven work? (7) Internet Maven Repository {Local Repository (local cache) Java (or other lang) Project Project Object Model (POM) assemble dependencies plugins profiles lifecycle build phases App Source Code Apache Maven assemble 1 2 3 5 • Optional • Maven can push built “artifacts” • Artifacts can be pushed to: • local cache (local repository) • non-local repository Notes 21 execute 4

Slide 22

Slide 22 text

@CGuntur How does Apache Maven work? (FINAL) {Local Repository (local cache) Java (or other lang) Project Project Object Model (POM) assemble dependencies plugins profiles lifecycle build phases App Source Code Apache Maven assemble 22 execute 5 4 3 2 1 Internet Maven Repository

Slide 23

Slide 23 text

@CGuntur Effective POM? 23

Slide 24

Slide 24 text

@CGuntur What is an “effective POM”? • an assembly of execution steps, properties and profiles • content that Maven can execute for the project • exhaustive set of dependencies and plugins that can be used to build • lists all dependencies and their transitive dependencies • any potential dependency conflict resolution • ready for execution by Maven executable 24

Slide 25

Slide 25 text

@CGuntur How is the effective POM created? Maven Internal Defaults Each lower block overrides previous values Maven Base POM Default POM (in maven jar, cannot override) Maven Global Settings Settings located under maven installation Maven User Settings Settings located under user home .m2 directory Parent/Bill-of-Material POM(s) Parent or BOM specified in project POM Maven Project POM The project POM Effective POM Read-only generated Effective POM 25

Slide 26

Slide 26 text

@CGuntur POM Hierarchies 26

Slide 27

Slide 27 text

@CGuntur POM Hierarchies Parent POM Another Parent POM Project POM Dependencies
 Plugins
 Properties Dependencies
 Plugins
 Properties Parentage Aggregation Aggregate POM Module 1 POM Module 2 POM Module 3 POM Module 4 POM No inheritance 
 of content Project knows Parent
 Parent does not know Project Parent can aggregate Project Module does not know Aggregator Module can set Aggregator as Parent
 Aggregator knows Module Bill-Of-Materials Bill-Of-Material POM Project 1 POM Project 2 POM Project 3 POM Project 4 POM Project inherits/imports Bill-Of-Materials
 Bill-Of-Materials does not know Project Bill-Of-Materials can aggregate Project Dependencies
 Plugins
 Properties 27

Slide 28

Slide 28 text

@CGuntur POM Hierarchies - example pom excerpts Parentage Aggregation Bill-Of-Materials Project knows Parent
 Parent can aggregate Project Module can set Aggregator as Parent
 Aggregator knows Module Project inherits/imports Bill-Of-Materials
 Bill-Of-Materials does not know Project 28

Slide 29

Slide 29 text

@CGuntur Under the hood 29

Slide 30

Slide 30 text

@CGuntur Lifecycles in Maven Standard Lifecycles 
 pre-clean
 clean
 post-clean generate-resources 
 pre-site
 site
 post-site site-deploy 
 validate initialize generate-sources process-sources generate-resources process-resources compile process-classes generate-test-sources process-test-sources generate-test-resources process-test-resource 
 test-compile process-test-classes test prepare-package package pre-integration-test integration-test post-integration-test verify install deploy
 site build (default) Phases Phases Phases clean default phase Legend not commonly invoked 30

Slide 31

Slide 31 text

@CGuntur Lifecycles: Under the hood • command: mvn clean compile • first calls clean:clean (default phase of clean lifecycle is clean): pre-clean clean • then invokes the following phases of default lifecycle (no prefix): validate initialize generate-sources process-sources generate-resources process-resources compile 31

Slide 32

Slide 32 text

@CGuntur Plugins Plugins Goal name4 Plugin 2 . . . Plugin 1 Goal name1 Lifecycle phase 1
 
 phase 2 
 phase 3 ... Plugin 3 Goal name2 phase 1 Goal name5 phase 1 Goal name3 phase 3 Lifecycle 32

Slide 33

Slide 33 text

@CGuntur Plugins: Under the hood • a lifecycle phase by itself does not perform any task • tasks are performed by plugins • plugins allow phases to carry out responsibilities • plugins declare goals • a plugin goal defines a specific task towards building/managing the project • a plugin goal may be bound to zero or more lifecycle phases • invoking a phase invokes plugin goals linked to the phase • in addition to calling lifecycles or their phases, possible to invoke a plugin goal • default bindings of lifecycle phase to plugin goal may depend on packaging • package phase for ejb will call ejb goal from the maven-ejb-plugin • package phase for jar will call jar goal from the maven-jar-plugin • generate-resources phase for ear will call generate-application-xml goal from the maven-ear-plugin 33

Slide 34

Slide 34 text

@CGuntur Dependencies Exclusions Dependency G-A-V Scope Type Dependency G-A-V Scope Type Dependency G-A-V Scope Type Dependency G-A-V Scope Type Dependency G-A-V Scope Type Dependency G-A-V Scope Type Dependency G-A-V Scope Type Dependency G-A-V Scope Type Dependency G-A-V Scope Type Dependency G-A-V Scope Type Optional Exclusions Optional 1 2 3 4 5 34

Slide 35

Slide 35 text

@CGuntur Dependencies Dependency Graph Dependency D1 Dependency D2 Dependency D3 Dependency D4 Dependency D11 Dependency Dx Dependency Dx Dependency Dx Dependency D31 Dependency D311 Dependency Dx G1:A1:V1 G11:A11:V11 G34:A34:V34 Gx:Ax:V1.0.0 Gx:Ax:V1.2.0 G33:A33:V33 Gx:Ax:V1.3.0 Gx:Ax:V1.5.0 G2:A2:V2 G3:A3:V3 G4:A4:V4 Selection 35

Slide 36

Slide 36 text

@CGuntur Dependencies Dependency Graph Dependency D1 Dependency D2 Dependency D3 Dependency D4 Dependency D11 Dependency Dx Dependency Dx Dependency Dx Dependency D31 Dependency D311 Dependency Dx G1:A1:V1 G11:A11:V11 G34:A34:V34 Gx:Ax:V1.0.0 Gx:Ax:V1.2.0 G33:A33:V33 Gx:Ax:V1.3.0 Gx:Ax:V1.5.0 G2:A2:V2 G3:A3:V3 G4:A4:V4 Selected v1.2.0
 Nearest in depth First in resolution path Omitted v1.3.0
 Not nearest in depth Omitted v1.5.0
 Not first in resolution path Omitted v1.0.0
 Not nearest in depth Selection 36

Slide 37

Slide 37 text

@CGuntur There is a lot more … That’s all we have time for! 37 Questions?

Slide 38

Slide 38 text

@CGuntur Appendix 38

Slide 39

Slide 39 text

@CGuntur Maven POM Reference - project modelVersion parent groupId artifactId version name description url inceptionYear organization licenses developers contributors mailingLists scm issueManagement ciManagement distributionManagement repositories pluginRepositories modules properties build reporting profiles complex entity Legend simple entity expanded later dependencies dependencyManagement 39

Slide 40

Slide 40 text

@CGuntur Maven POM Reference - build sourceDirectory scriptSourceDirectory testSourceDirectory outputDirectory testOutputDirectory extensions testResources filters defaultGoal resources directory finalName complex entity Legend simple entity expanded later plugins pluginManagement 40

Slide 41

Slide 41 text

@CGuntur Maven POM Reference - [dependencyManagement/] dependencies/dependency complex entity Legend simple entity expanded later groupId artifactId version classifier type exclusions exclusion groupId artifactId scope systemPath optional 41

Slide 42

Slide 42 text

@CGuntur Maven POM Reference - [pluginManagement/] plugins/plugin complex entity Legend simple entity expanded later groupId artifactId version extensions inherited executions execution id phase inherited goals goal configuration dependencies configuration 42

Slide 43

Slide 43 text

@CGuntur Maven POM Reference - profiles/profile id activation testOutputDirectory distributionManagement modules repositories reporting dependencyManagement properties dependencies pluginRepositories complex entity Legend simple entity expanded later 43

Slide 44

Slide 44 text

@CGuntur Configuring Apache Maven JAVA_HOME Environment Variables .mvn Config Files jvm.config mvn.config XML Configurations /conf/settings.xml M2_HOME PATH MAVEN_OPTS /.m2/settings.xml /.mvn/extensions.xml /.m2/toolchains.xml /toolchains.xml 44

Slide 45

Slide 45 text

@CGuntur Understanding versioning strategies Development G:A:V-SNAPSHOT Testing, Bug Fixes
 
 Same Version Build, Continuous Integration
 
 Sharing during development Mutable Artifacts
 replaced every build Maven
 SNAPSHOT
 Repository Next Version
 
 Release Build, no SNAPSHOT
 
 Production-ready artifact Immutable Artifacts
 Should never be altered Maven
 RELEASE
 Repository Release G:A:V 1 2 3 4 45