Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Apache Maven: Collaborative Mastery

Apache Maven: Collaborative Mastery

Maven is a power tool for Java developers. However, harnessing Maven's incredible capabilities efficiently is no small feat. In this session, we’ll embark on a journey to decipher the complexities of Maven, offering solutions for developers of all levels within a team.

We start by demystifying the inner workings of Maven lifecycles, ensuring that newcomers and seasoned developers alike can navigate its intricate processes. We’ll delve into the intricacies of dependency resolution, providing clarity on this critical aspect of Maven.

We’ll also explore how a Maven Project Object Model (POM) seamlessly integrates with IntelliJ IDEA, making life easier for developers. Discover the ins and outs of upgrading Java versions within a POM and learn how IntelliJ IDEA assists in tracking and managing changes to dependencies, ensuring you stay on top of any vulnerability concerns.

As a bonus, we’ll share valuable collaboration tips and tricks specifically tailored for teams using Maven in IntelliJ IDEA. Whether you're a Java novice or a seasoned pro, this session is your guide to a smoother and more productive journey with Maven in a team environment.

Chandra Guntur

October 26, 2023
Tweet

More Decks by Chandra Guntur

Other Decks in Technology

Transcript

  1. https://cguntur.me @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
  2. https://cguntur.me @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
  3. https://cguntur.me @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
  4. https://cguntur.me @CGuntur • POM lists: • dependencies • plugins •

    properties • inheritance details • profiles • ... “POM” is … A Pomeranian? Pomegranate Juice? One of the Pom-poms? 7 POM is Project Object Model
  5. https://cguntur.me @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
  6. https://cguntur.me @CGuntur Apache Maven usage • create any auto-generated source

    code, if needed • generate documentation from source code • compile source code, display any errors and warnings • test the project running existing tests in source code • 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 • in addition to reporting a build as success or failure 11
  7. https://cguntur.me @CGuntur Watch for notes here How does Apache Maven

    work? 13 Java (or other lang) Project App Source Code Internet Maven Repository
  8. https://cguntur.me @CGuntur How does Apache Maven work? (1) 14 Java

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

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

    (or other lang) Project App Source Code Project Object Model (POM) dependencies plugins profiles lifecycle build phases assemble Watch for notes here • 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 1 Apache Maven assemble 2 effective POM Internet Maven Repository
  11. https://cguntur.me @CGuntur How does Apache Maven work? (4) 17 Java

    (or other lang) Project App Source Code Internet Maven Repository Project Object Model (POM) dependencies plugins profiles lifecycle build phases assemble Watch for notes here • 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 1 Apache Maven assemble 2 effective POM {Local Repository (local cache)
  12. https://cguntur.me @CGuntur How does Apache Maven work? (5) 18 Java

    (or other lang) Project App Source Code Internet Maven Repository Project Object Model (POM) dependencies plugins profiles lifecycle build phases assemble Watch for notes here • 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 1 Apache Maven assemble 2 effective POM {Local Repository (local cache) 3
  13. https://cguntur.me @CGuntur How does Apache Maven work? (6) 19 Java

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

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

    Model (POM) assemble dependencies plugins profiles lifecycle build phases App Source Code Apache Maven assemble 21 execute 5 4 3 2 1 effective POM How does Apache Maven work? (FINAL) Internet Maven Repository Local Repository (local cache)
  16. https://cguntur.me @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 23
  17. https://cguntur.me @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 24
  18. https://cguntur.me @CGuntur POM Hierarchies Parent POM Another Parent POM Project

    POM Dependencies
 Plugins
 Properties Dependencies
 Plugins
 Properties Parentage Aggregation 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 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 26 Aggregate POM Bill-Of-Material POM
  19. https://cguntur.me @CGuntur Project inherits/imports Bill-Of-Materials
 Bill-Of-Materials does not know Project

    Bill-Of-Materials can aggregate Project 27 Project knows Parent
 Parent can aggregate Project Module can set Aggregator as Parent
 Aggregator knows Module POM Hierarchies - example pom excerpts Parentage Aggregation Bill-Of-Materials
  20. https://cguntur.me @CGuntur Lifecycles in Maven Standard Lifecycles 
 validate initialize

    generate-sources process-sources generate-resources process-resources compile process-classes generate-test-sources process-test-sources generate-test-resources process-test-resources 
 test-compile process-test-classes test prepare-package package pre-integration-test integration-test post-integration-test verify install deploy
 build (default) Phases 
 pre-clean
 clean
 post-clean Phases 
 pre-site
 site
 post-site site-deploy Phases 29 clean site
  21. https://cguntur.me @CGuntur Lifecycles in Maven - default phases Standard Lifecycles

    
 validate initialize generate-sources process-sources generate-resources process-resources compile process-classes generate-test-sources process-test-sources generate-test-resources process-test-resources 
 test-compile process-test-classes test prepare-package package pre-integration-test integration-test post-integration-test verify install deploy
 build (default) Phases 
 pre-clean
 clean
 post-clean Phases 
 pre-site
 site
 post-site site-deploy Phases 30 clean site default phase Legend
  22. https://cguntur.me @CGuntur Lifecycles in Maven - common phases used Standard

    Lifecycles 
 validate initialize generate-sources process-sources generate-resources process-resources compile process-classes generate-test-sources process-test-sources generate-test-resources process-test-resources 
 test-compile process-test-classes test prepare-package package pre-integration-test integration-test post-integration-test verify install deploy
 build (default) Phases 
 pre-clean
 clean
 post-clean Phases 
 pre-site
 site
 post-site site-deploy Phases 31 clean site Legend not commonly invoked commonly invoked
  23. https://cguntur.me @CGuntur Lifecycles in Maven - calling “mvn clean compile”

    Standard Lifecycles 
 validate initialize generate-sources process-sources generate-resources process-resources compile process-classes generate-test-sources process-test-sources generate-test-resources process-test-resources 
 test-compile process-test-classes test prepare-package package pre-integration-test integration-test post-integration-test verify install deploy
 build (default) Phases 
 pre-clean
 clean
 post-clean Phases 
 pre-site
 site
 post-site site-deploy Phases 32 clean site
  24. https://cguntur.me @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 33
  25. https://cguntur.me @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 34
  26. https://cguntur.me @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 35
  27. https://cguntur.me @CGuntur Dependencies - graph 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 1 2 3 4 5 36 Optional
  28. https://cguntur.me @CGuntur Dependencies - resolution ??? Dependency Graph Dependency D1

    G1:A1:V1 Dependency D11 G11:A11:V11 Dependency D311 G34:A34:V34 Dependency Dx Gx:Ax:V1.0.0 Dependency Dx Gx:Ax:V1.2.0 Dependency D31 G33:A33:V33 Dependency Dx Gx:Ax:V1.3.0 Dependency Dx Gx:Ax:V1.5.0 Dependency D2 G2:A2:V2 Dependency D3 G3:A3:V3 Dependency D4 G4:A4:V4 Selection ? Dx v1.2.0 ? Dx v1.3.0 ? Dx v1.5.0 ? Dx v1.0.0 ?
  29. https://cguntur.me @CGuntur Dependencies - resolution !!! Dependency Graph Dependency D1

    G1:A1:V1 Dependency D11 G11:A11:V11 Dependency D311 G34:A34:V34 Dependency Dx Gx:Ax:V1.0.0 Dependency Dx Gx:Ax:V1.2.0 Dependency D31 G33:A33:V33 Dependency Dx Gx:Ax:V1.3.0 Dependency Dx Gx:Ax:V1.5.0 Dependency D2 G2:A2:V2 Dependency D3 G3:A3:V3 Dependency D4 G4:A4:V4 Selection ? 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
  30. https://cguntur.me @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 dependencies dependencyManagement 41 Legend complex entity simple entity expanded later
  31. https://cguntur.me @CGuntur Maven POM Reference - build sourceDirectory scriptSourceDirectory testSourceDirectory

    outputDirectory testOutputDirectory extensions testResources filters defaultGoal resources directory finalName plugins pluginManagement 42 Legend complex entity simple entity expanded later
  32. https://cguntur.me @CGuntur Maven POM Reference - [dependencyManagement/] dependencies/dependency groupId artifactId

    version classifier type exclusions exclusion groupId artifactId scope systemPath optional 43 Legend complex entity simple entity expanded later
  33. https://cguntur.me @CGuntur Maven POM Reference - [pluginManagement/] plugins/plugin groupId artifactId

    version extensions inherited executions execution id phase inherited goals goal configuration configuration 44 dependencies Legend complex entity simple entity expanded later
  34. https://cguntur.me @CGuntur Maven POM Reference - profiles/profile id activation testOutputDirectory

    distributionManagement modules repositories reporting properties pluginRepositories 45 dependencies dependencyManagement Legend complex entity simple entity expanded later
  35. https://cguntur.me @CGuntur Configuring Apache Maven JAVA_HOME Environment Variables .mvn Config

    Files jvm.config mvn.config XML Configurations <Maven Installation>/conf/settings.xml M2_HOME PATH MAVEN_OPTS <User Home>/.m2/settings.xml <Project Basedir>/.mvn/extensions.xml <User Home>/.m2/toolchains.xml <Project Basedir>/toolchains.xml 46
  36. https://cguntur.me @CGuntur Testing, Bug Fixes
 
 Same Version 1 Understanding

    versioning strategies Development G:A:V-SNAPSHOT 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 2 3 4 47