Professional Documents
Culture Documents
Maven
Persistent University
Module 3: Core Concepts
Key learning points
• Maven Coordinates
• Dependency Management
• Maven Repositories
• Maven Archetypes
Maven Directory Structure
Source: https://maven.apache.org/guides/getting-started/maven-in-five-minutes.html
4
Maven Plugins and Goals
Lifecycle
Phase 1 Phase 2
Phase 4 etc
Goal 1 Goal 1
Goal 2
Phase 3 etc
5
Broader picture
• Lifecycle
- broadest unit
• Phase
• Goal
6
Lifecycles in Maven
• default
• clean
• site
7
default Lifecycle phases (these are some phases but the lifecycle contains many more phases)
• validate - validate the project is correct and all necessary information is available
• test - test the compiled source code using a suitable unit testing framework. These tests should not require the code be
packaged or deployed
• package - take the compiled code and package it in its distributable format, such as a JAR.
• verify - run any checks on results of integration tests to ensure quality criteria are met
• install - install the package into the local repository, for use as a dependency in other projects locally
• deploy - done in the build environment, copies the final package to the remote repository for sharing with other developers
and projects.
8
Clean Lifecycle
Phases
9
Site Lifecycle
10
Default Lifecycle in greater depth
• Default Lifecycle
- 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
11
Maven Coordinates
• Maven coordinates define a set of identifiers which can be used to uniquely identify a project
- groupId
- artifactId
- Version (Snapshot/1.x/2.x)
- Packaging (jar/war/ear/pom)
12
Maven Coordinates 13
group-id artifact-id
version packaging
13
Maven Co-ordinates
• groupId
- The convention for group identifiers is that they begin with the reverse domain name of the organization
• artifactId
• version
- Projects that have been released have a fixed version identifier that refers to a specific version of the project.
- Projects undergoing active development can use a special identifier that marks a version as a SNAPSHOT
• Packaging
- Jar/war/ear/pom
14
Maven Dependency Management
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
• Maven avoids the need to discover and specify the libraries that your own dependencies require by including transitive
dependencies automatically.
• Ref: https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html
15
Dependency Scopes
Scope Description
runtime This scope indicates that dependency is not required for compilation, but is
required during execution.
test This scope indicates that the dependency is only available for the test
compilation and execution phases.
system This scope indicates that you have to provide the system path.
import This scope is only used when dependency is of type pom. This scopes
indicates that the specified POM should be replaced with the dependencies
in that POM's <dependencyManagement> section.
Ref: https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Scope
16
Maven Repositories
1. Local
2. Remote
• Ref https://maven.apache.org/guides/introduction/introduction-to-repositories.html
17
Maven Archetypes
Archetype ArtifactIds Description
• Archetype is a Maven project templating maven-archetype-archetype An archetype to generate a sample archetype project.
toolkit
maven-archetype-j2ee-simple An archetype to generate a simplifed sample J2EE
application.
• Provided Archetypes
maven-archetype-mojo An archetype to generate a sample a sample Maven plugin.
Ref: https://maven.apache.org/guides/introduction/introduction-to-archetypes.html
18
References
• https://maven.apache.org/guides/getting-started/maven-in-five-minutes.html
• https://maven.apache.org/guides/introduction/introduction-to-repositories.html
• https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html
• https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Scope
• https://maven.apache.org/guides/introduction/introduction-to-archetypes.html
• https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html
• https://javabrains.io/courses/buildsys_mavenintro/
19
Thank you!
Persistent University