Training Guide ‐ Maven Fundamentals - vinhtbkit/bkit-kb GitHub Wiki
- Prerequisites
- There is some basic knowledge about Maven. If you are a beginner learning Maven, I recommend you go here: https://maven.apache.org/guides/getting-started/maven-in-five-minutes.html before going to this documentation.
- Goals:
- Have knowledge of how to use Maven.
- Have knowledge of how Maven operates in a project
- Have knowledge of how Maven builds an infrastructure for a project.
- Duration
- Installing Maven on your OS
- Maven Lifecycle
- Maven Plugins
- Dependency management and its working mechanism
As we start using Maven, we need to understand the Maven project lifecycle. Maven is implemented based around the concept of a
build lifecycle
. This means there is a clearly defined process to build and distribute artifacts with Maven.
What makes up a lifecycle? The stages of a lifecycle are called
phases
. In each phase, one or moregoals
can be executed.
-
Build Lifecycle
- Three built-in build lifecycles:
default
,clean
,site
- The purpose of those built-in build lifecycles
- Three built-in build lifecycles:
-
Phases
- The relationship between phases and a built-in build lifecycles.
- A Build Lifecycle is Made Up of Phases
- The relationship between goals and a phase
-
Goals (Plugin Goals)
- A Build Phase is Made Up of Plugin Goals
- How add execute goals?
- call a goal via command line
- add goals to phases (configuration in pom file)
-
Quizzes!
- Explain this command:
mvn clean dependency:copy-dependencies package
- In the Default lifecycle bindings, what will happen when I run the command
mvn package
?- What will it do for my project?
- Do the
install
andtest
phases run? - For the packaging type
jar
, whichplugin
andgoal
are used?
- Explain this command:
-
Reference
- structure POM file
- Inheritance, Aggregation in POM
- Quizzes!
- What happens when you add a parent POM to the POM file?
- What is the version of the dependency
org.springframework.cloud:spring-cloud-starter-openfeign
? Explain whyorg.springframework.cloud:spring-cloud-starter-openfeign
has that version.
<properties>
<spring-cloud.version>4.0.3</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
Your goal is to understand the structure of the POM file, the specific tasks of each tag element within it. Detailed sections related to it will be described in later parts.
- References
- Structure setting.xml file and the purpose of those elements.
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository/>
<interactiveMode/>
<offline/>
<pluginGroups/>
<servers/>
<mirrors/>
<proxies/>
<profiles/>
<activeProfiles/>
</settings>
-
Configuration
- Simple value
- Plugin Groups
- Proxies
- Mirros
- Servers
-
Profiles
- Activation
- Properties
- Repositories
- Plugin Repositories
- Active Profiles
-
Settings Level
- Global settings
- User Settings
-
Quizzes!
- If there is the same configuration in both global settings and user settings, which configuration takes precedence?
- What is the purpose of the server element? When should the server element be used?
- How to activate a profile?
-
References
- Purpose
- What are the different types of profile? Where is each defined?
- Adding a new profile
- Activating a profile
- Explicitly
- Implicitly
- Based on OS
- Based on system properties
- Based on presence of files
- Deactivating a profile
- Which areas of a POM can be customized by each type of profile? Why?
- Profile Order
- Quizzes!
- Please explain this command line?
mvn groupId:artifactId:goal -P profile-1,profile-2,?profile-3
- I have a settings.xml file with a profile named
demo-skip-test
, how to run this profile in a project
<profile> <id>demo-skip-test</id> <properties> <maven.test.skip>true</maven.test.skip> </properties> </profile>
- Please explain this command line?
- References
- The mechanism for downloading versions of artifacts.
- Understanding SNAPSHOT dependencies
- When we build an application, how does Maven search for dependencies?
- Let’s assume we have a project under development with a SNAPSHOT version:
<groupId>com.demo</groupId>
<artifactId>my-project</artifactId>
<version>3.0.0-SNAPSHOT</version>
We’re going to deploy the project to a self-hosted Nexus repository. When I run the command mvn deploy
what is the version of my artifact?
- Similarly, I have a project configured like this.
<groupId>com.demo</groupId>
<artifactId>my-project</artifactId>
<version>3.0.5</version>
When I run the command mvn deploy
, what is the version of my artifact? If I run this command twice, what will happen?
- Suppose I have a
A
dependency, and within thisA
dependency, there are other dependencies as shown below. Can I use theD
dependency in my project? If yes, which version of theD
dependency will be used?
- https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html
- https://www.baeldung.com/maven-snapshot-release-repository
- https://maven.apache.org/pom.html#Repositories
- What is Archetype?
- Using an Archetype
- Provided Archetypes
- What makes up an Archetype? (OPTIONAL)
- how to use the Archetype Plugin to create a project
- how to use the Archetype Plugin to create an archetype from an existing project
- Goals of archetype plugin
- The command
mvn archetype:generate
will perform what action?
- Build plugin
- Reporting plugin
- Plugins bound to
Default Lifecycles
- The Resources Plugin
- The Compiler Plugin
- The Surefire Plugin
- The Failsafe Plugin
- The Verifier Plugin
- The Install Plugin
- The Deploy Plugin
- Plugins bound to
Clean Lifecycles
- Plugins bound to
Site Lifecycles
- Maven JaCoCo plugin
- Maven SonarQube plugin
- Mojo
- When running the
mvn test
command, which plugin will be used?
- https://maven.apache.org/guides/mini/guide-configuring-plugins.html
- https://www.baeldung.com/core-maven-plugins
Create a project using the Spring Boot framework with Maven.
- Use Maven archetype to create a project structure with the following configuration:
com.training:my-demo:1.0.0-SNAPSHOT
- Add parent dependency
org.springframework.boot:spring-boot-starter-parent:3.1.0
- Add the following dependencies:
org.springframework.boot:spring-boot-starter-web
,org.springframework.boot:spring-boot-configuration-processor
,spring-boot-configuration-processor:spring-boot-starter-validation
. The version of these dependencies should inherit from the version of the parent dependency.