You are on page 1of 20

DevOps 506: Intermediate

Maven
Persistent University
Module 3: Core Concepts
Key learning points

• Maven Directory Structure

• Standard Maven LifeCycle

• Maven Plugins and Goals

• 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

- A stage in the lifecycle.

• Goal

- A plugin goal represents a specific task

6
Lifecycles in Maven

• default

- handles your project deployment

• clean

- handles project cleaning

• site

- handles the creation of your project's site documentation

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

• compile - compile the source code of the project

• 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

pre-clean execute processes needed prior to


the actual project cleaning
clean remove all files generated by the
previous build
post-clean execute processes needed to
finalize the project cleaning

9
Site Lifecycle

pre-site execute processes needed prior to


the actual project site generation
site generate the project's site
documentation
post-site execute processes needed to
finalize the site generation, and to
prepare for site deployment
site-deploy deploy the generated site
documentation to the specified web
server

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

• The Archetype plugin created a project with a file named pom.xml.

• This is the Project Object Model (POM), a declarative description of a project

• 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 group, company, team, organization, project, or other group

- The convention for group identifiers is that they begin with the reverse domain name of the organization

that creates the project

- Project groupid created by apache would be org.apache.maven

• artifactId

- A unique identifier under groupId that represents a single project

• version

- A specific release of a project.

- 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

• Ability to locate an artifact in a repository based on Maven coordinates

<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

compile This scope indicates that dependency is available in classpath of project. It


is default scope.

provided This scope indicates that dependency is to be provided by JDK or web-


Server/Container at runtime

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

- Maven Central http://repo.maven.apache.org/maven2/

- Company provided internal repo deployed on an HTTP server

• 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.

• Maven provides several Archetype


maven-archetype-plugin An archetype to generate a sample Maven plugin.
artifacts:
maven-archetype-plugin-site An archetype to generate a sample Maven plugin site.

maven-archetype-portlet An archetype to generate a sample JSR-268 Portlet.

maven-archetype-quickstart An archetype to generate a sample Maven project.

maven-archetype-simple An archetype to generate a simple Maven project.

maven-archetype-site An archetype to generate a sample Maven site which


demonstrates some of the supported document types like
APT, XDoc, and FML and demonstrates how to i18n your
site.

maven-archetype-site-simple An archetype to generate a sample Maven site.

maven-archetype-webapp An archetype to generate a sample Maven Webapp project.

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

You might also like