JVA-016-java代写
时间:2023-08-10
Luxoft, A DXC Technology Company. All rights reserved. © 2023
Apache Maven
Introduction
JVA-016
2Prerequisites
Apache Maven Introduction
Trainees should have experience in:
• Java programming
• Working with XML
• Working with the terminal
3Course Objectives
Apache Maven Introduction
Trainees will learn about:
• The purpose of the project builder
• Maven installation process
• POM file structure
• Profiles
• Configuration
4Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic: Course Objectives
What is the purpose of Apache Maven project builder?
a) To compile Java code
b) To manage project dependencies
c) To create Java applications
d) To debug Java applications
5Why Do We Need a Project Builder?
javac -classpath
~/work/projects/AntProject/lib/mysql.jar
~/work/projects/AntProject/src/*
• Only you know what is their in your files
• Compile only changes
• Use continuous integration
• Binding to a concrete IDE
Apache Maven Introduction
6Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic: Project Builder
Why is Project Builder important for continuous integration?
a) It helps organize and manage software development projects
b) It ensures that the project is up-to-date and working properly
c) It helps identify and fix issues with the project
d) All of the above
7Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic: Why Do We Need a Project Builder?
What is Apache Maven?
a) A type of database management system
b) A project builder that automates the build process
c) A programming language used for web development
d) An integrated development environment (IDE)
8Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic: Why Do We Need a Project Builder?
What are the benefits of using Apache Maven?
a) Automatically downloads and manages project dependencies
b) Provides the ability to run unit tests
c) Supports multiple programming languages
d) Offers plugins for integration with other software
9Why Do We Need a Project Builder?
Apache Maven Introduction
While developing SW, developers have to solve some routine tasks, such as:
• Compile source modules
• Conduct unit tests
• Put compiled modules into certain folders
• Pack them
• Copy necessary resources for modules
• Upload the application to the server
• etc.
10
Project Builders
Apache Maven Introduction
Builders — software to automate project build.
Build stages are referred to as build
As builds may be repeated many times, doing
them manually is too expensive!
11
Requirements
Apache Maven Introduction
The builder should be flexibly configurable:
• We need to have an external file with instructions for the builder what and where should be
done.
The builder should be expandable:
• There should be a capability of adding new operations to be performed by the builder.
The builder should be platform-independent:
• You should be able to build in any operating system.
• It should not be bound to IDE.
12
Conclusions
Apache Maven Introduction
• Project builders are extremely important.
• They are configurable.
• Builder can be expanded.
• Builder can be used in continuous integration.
• Simple projects can be built from the command line (or done without building at all)
13
Examples of Builders
Apache Maven Introduction
MAKE - C/C++
NANT - .NET
MAVEN - Java
SBT - Scala
GRADLE - Java
14
Reading on MAVEN
Apache Maven Introduction
www.maven.apache.org - The official web site:
15
MAVEN Background
Apache Maven Introduction
• Maven is a powerful tool for project management.
• It’s a byproduct of an open source project.
Jakarta Alexandria.
• The project was born in 2001.
• In 2005, it was “deleted” and became an independent
project.
• In American English the word “maven” means
connoisseur or expert.
As of the date of this presentation, the latest version was 3.9.1
The author of the project:
Jason van Zyl
16
MAVEN Installation
Apache Maven Introduction
Before installation, check if you have Java:
~$ java –version
java version "openjdk version "18.0.2"
OpenJDK Runtime Environment Corretto-
18.0.2.9.1 (build 18.0.2+9-FR)
Installation:
Download and unpack the distributive to any directory.
http://maven.apache.org/download.html
17
Installing MAVEN in Windows
Apache Maven Introduction
Don’t forget to set PATH and M2_HOME variables
PATH=...;%M2_HOME%\bin
18
Installing MAVEN in Ubuntu
Apache Maven Introduction
Installing Maven in Ubuntu:
~$ sudo add-apt-repository ppa:natecarlson/maven3
~$ sudo apt-get update
~$ sudo apt-get install maven3
Check the correctness of installation:
~$ mvn -version (-v)
Apache Maven 3.9.0
Java version: 18.0.2
19Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic: Installing MAVEN in Windows
How can you install MAVEN on Windows?
a) Through the Windows Store
b) By downloading it from a website and configuring environment
c) Using the command line
d) It comes pre-installed on Windows
20Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic: Installing MAVEN
What variables do you need to set after installing MAVEN?
a) M2_PATH
b) PATH
c) M2_HOME
d) HOME_PATH
21Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic: Installing MAVEN
What command can you use to check the correctness of the installation?
a) sudo mvn install
b) sudo apt-get install maven
c) mvn --version
d) sudo upgrade maven
22
MAVEN Installation
Apache Maven Introduction
~$ mvn -help
Syntax: mvn [options]
[] []
Options:
-P,--activate-profiles list of parameters for activation
-ff,--fail-fast stops building at first failure
-B,--batch-mode performed in non-interactive (batch) mode
-N,--non-recursive non recursive calling in subprojects
-npr,--no-plugin-registry don't use ~/.m2/plugin-registry.xml
-U,--update-snapshots update all memory states
-cpu,--check-plugin-updates perform force check for updates
-npu,--no-plugin-updates stop checking for updates
-D,--define define a property
-X,--debug display debug information
-e,--errors display an error message
-f,--file forced use of an alternative POM file
-h,--help display help info
-o,--offline work offline
-r,--reactor perform project tasks found in reactor
-s,--settings alternative path for the settings file
-v,--version enter information about version
-gs alternative path to global settings
23
MAVEN Configuration
Apache Maven Introduction
project/pom.xml
~/.m2/settings.xml
{M2_HOME}/conf/settings.xml
Level
Specific project configuration
Configuration of all projects of the user
Common configuration of all users
Description
3 configuration levels
Maven global conventions
24Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic: MAVEN Configuration
What are the 3 configuration levels in Apache Maven?
a) Project level, user level, and system level
b) Global level, environment level, and user level
c) Local level, network level, and server level
d) Version level, plugin level, and dependency level
25Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic: MAVEN Configuration
Where can you configure the Maven version in a project?
a) pom.xml
b) project.properties
c) settings.xml
d) command line
26Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic: MAVEN Configuration
Why do we need 3 levels of configuration in Maven?
a) To make the configuration process more complex
b) To separate concerns and provide flexibility in configuration
c) To make sure that only developers can configure the project
d) To generate documentation about the project
27Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic: MAVEN Configuration
What is the proper place to configure a network proxy?
a) ~./m2/settings.xml
b) pom.xml
c) Command line
d) Environment variables
28
Configuring Proxy
Apache Maven Introduction

...


optional
true
http
proxyuser
proxypass
proxy.host.net
80
local.net|some.host.


...

To configure proxy add the following to settings.xml:
29
MAVEN Folder
Apache Maven Introduction
apache-maven-3.9.1
bin
boot
conf
lib
Running files, including mvn
Default configuration
Launcher
README
LICENSE.txt
Additional libraries
30
MAVEN Components
Apache Maven Introduction
Maven has three main components:
• Core – describes basic principles and
project devices.
• Plugins – contain code and execute goals.
• Repository (local or remote).
31
Maven Folder Structure
Apache Maven Introduction
*every default path can be changed by configuring the respective plugin
pom.xml Configuration in the root folder of the project
README.txt
target/
src/main/java
src/test/java
File containing any useful information
Folder for output artifacts
Standard folder for the application source code
Standard folder for the source code of tests
src/test/resources Standard folder for test resources
src/main/config Standard folder for configuration files
src/main/filters Standard folder for filters
src/main/resources Standard folder for project resources
32
Project Build Artifacts
Apache Maven Introduction
target
classes
ua
luxoft
App.class
maven-archiver
pom.properties
test-classes
surefire-reports
maven-research-1.0-SNAPSHOT.jar
Test reports
A key folder for all build artifacts
Project properties
JAR
33Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic: Maven Folder Structure
Which folder contains the compiled source code of the project?
a) src/main/java
b) src/main/resources
c) src/test/java
d) target
34Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic: Maven Folder Structure
Which folder contains the unit tests for the project?
a) src/main/java
b) src/main/resources
c) src/test/java
d) src/test/resources
35Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic: Maven Folder Structure
Which file specifies the project's dependencies?
a) pom.xml
b) build.xml
c) dependencies.xml
d) project.xml
April 23, 2023 36
POM File Structure
37
POM - Project Object Model
POM File Structure

4.0.0





POM file contains the description of your project (declarative style!) and all its specific
configuration settings.
http://maven.apache.org/xsd/maven-4.0.0.xsdXSD-schema:
38Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic: POM
What does POM stand for?
a) Program Object Model
b) Product Object Model
c) Project Object Model
d) Prototype Object Model
39Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic: POM
What is the main purpose of the POM file?
a) To describe the project’s configuration settings
b) To execute code within the project
c) To store project assets such as images and videos
d) To create a user interface for the project
40
POM File Structure
POM File Structure
POM
POM Relationships Build Settings
Coordinate
groupId
artifactId
version
Multi-Module
Inheritance
Dependencies
General Project Information Build Environment
General
Contributors
Licenses
Environment Information
Maven Environment
Profiles
directories
extensions
resources
plugins
reporting
build


























Coordinates Build
Description Environment
41
POM File Structure
POM File Structure

4.0.0
com.luxoft
minimum-pom
1.0-SNAPSHOT


“Minimal POM file” should contain only model version and coordinates of the project.
Possible values for :
pom, jar, maven-plugin, ejb, war, ear, rar, par.
*By default packaging equals to jar
42Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic: POM File Structure
Which of the following is a valid coordinate format for the POM?
a) Group Id, Artifact Id, Version, Dependency
b) Group Id, Artifact Id, Version, License
c) Group Id, Artifact Id, Version, Packaging
d) Group Id, Artifact Id, Version, Repository
43Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic: POM File Structure
What is the default value for the element if not specified in the POM file?
a) war
b) pom
c) jar
d) ear
44

org.springframework
spring-core
6.0.8

Dependencies
Maven automatically downloads and adds necessary library versions to the classpath.
Example of dependency declaration:
org.springframework:spring-core:6.0.8
45Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic:
What is the purpose of declaring dependencies in Maven?
a) To manually include necessary libraries in the code
b) To automatically download and add necessary libraries to the classpath
c) To reduce the size of the project by removing unnecessary libraries
d) To increase the complexity of the project
46Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic:
Which elements are included in a declaration of dependency in Maven?
a) Group ID
b) Artifact ID
c) Version
d) Name
47Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic:
Where can necessary libraries be found in Maven?
a) They are automatically downloaded by Maven.
b) They are stored in the local Maven repository.
c) They can be downloaded from external sources.
d) They must be provided by the developer
48Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic:
What is a groupId in ?
a) Unique identifier of the organization which created the library
b) The name of the project
c) The name of the package within the project
d) The name of the class within the package
49Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic:
What is artifactId in ?
a) Unique identifier of the organization
b) The name of the project
c) The name of the package within the project
d) The name of the class within the package
50Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic:
What is a version in dependency?
a) The version of the developer's organization
b) The version of the project
c) The version of the dependency
d) The version of the class within the package
51
Maven repositories
Maven repositories are centralized locations where project dependencies and artifacts are stored and managed.
There are three types of Maven repositories:
• Local repository: The local repository is created on a developer's machine when Maven is first run. The local
repository helps reduce network traffic by caching remote artifacts.
• By default, the local repository is located in the .m2/repository directory within the user's home directory.
• Central repository: The central repository is a public repository provided by the Maven community, which hosts
a wide range of common open-source libraries, plugins, and other artifacts.
• When a required dependency is not found in the local repository, Maven searches for it in the central repository.
• If the dependency is found, it is downloaded and cached in the local repository for future use.
• The default central repository is maintained by the Apache Maven project at
https://repo.maven.apache.org/maven2/.
• Remote repository (also known as third-party or custom repository): Remote repositories are external
repositories hosted by organizations or developers to store and share their project artifacts. They can be public or
private, depending on the access requirements. Remote repositories can be added to a project's pom.xml file or
the global settings.xml file in the Maven installation directory. depen-
dency
LOCAL
CENTRAL
REMOTE
resolving
dependency:
download
52Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic: Maven repositories
What is the purpose of the Maven repository?
a) To store project source code
b) To store project binary files
c) To store project documentation
d) To store project test results
53Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic: Maven repositories
What are the three types of Maven repositories?
a) Local, global, external
b) Local, central, remote
c) Internal, external, shared
d) Primary, secondary, tertiary
54Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic: Maven repositories
Where is the local repository located by default?
a) In the project directory
b) In the Maven installation directory
c) In the user's home directory
d) In the root directory of the C drive
55Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic: Maven repositories
What is the purpose of the central repository?
a) To store project artifacts
b) To reduce network traffic
c) To cache downloaded artifacts
d) To host common open-source libraries and plugins
56Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic: Maven repositories
If a required dependency is not found in the local repository, where does Maven search for it?
a) In the central repository
b) In the remote repository
c) In the project directory
d) In the Maven installation directory
57Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic: Maven repositories
What are the remote repositories?
a) Repositories that are not accessible
b) Repositories that are hosted by Maven
c) Repositories that are created on a developers machine
d) Repositories that are external and hosted by organizations or developers
58Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic: Maven repositories
Where can remote repositories be added?
a) In the project directory
b) In the Maven installation directory
c) In the global settings.xml file
d) In the local repository
59Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic: Maven repositories
What is dependency resolution in Maven?
a) The process of adding dependencies to a project
b) The process of executing a project's build process
c) The process of caching downloaded artifacts
d) The process of finding and downloading a project's required dependencies
60Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic: Maven repositories
What is the default order in which Maven accesses repositories when downloading dependencies?
a) Local -> Remote -> Maven Central
b) Remote -> Local -> Maven Central
c) Remote->Maven Central->Local
d) Maven Central -> Local -> Remote
e) Maven Central -> Remote -> Local
f) Local -> Maven Central -> Remote
61

junit
junit
3.8.1
test

runtime
compile
provided
system
test
Dependency scopes
Dependency scopes define the visibility and usage of a
dependency in different parts of the build lifecycle.
They help to control which dependencies are included
during specific stages, such as compilation, testing, or
deployment.
Scope:
• compile (default scope): The dependency is required during the
compilation and runtime of the project. These dependencies are
included in the final packaged artifact (e.g., JAR, WAR, or EAR files).
Example: A library that your project uses in its core functionality.
• provided: The dependency is expected to be provided by the runtime
environment or container where the project is deployed. It is used
during compilation and testing, but not included in the packaged
artifact.
• runtime: The dependency is not required during compilation but is
needed during runtime. It is not used for compilation but is included in
the packaged artifact.
• test: The dependency is only needed during the test phase of the build
lifecycle. It is used for the compilation and execution of tests but is not
included in the packaged artifact. Example: A testing framework like
JUnit or Mockito.
Scopes:
compile runtime
compile with
import
javax.servlet.*
TOMCAT+servlet
JAR
provided:
Servlet API is provided
by Tomcat and
shouldn't be included
62Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic: dependency scopes
What is the purpose of Maven dependency scopes?
a) To list all dependencies used in the project
b) To determine the order in which dependencies are loaded
c) To specify which dependencies are required during compilation and runtime
d) To add additional features to the project
63Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic: dependency scopes
What is the default scope of a Maven dependency?
a) compile
b) test
c) provided
d) runtime
64Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic: dependency scopes
Which scope is used for frameworks like JUnit or Mockito?
a) compile
b) provided
c) runtime
d) test
65Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic: dependency scopes
Which of these scopes indicates that a dependency is not needed to be packaged in JAR?
b) compile
c) provided
d) runtime
66
Transitive dependencies
Transitive dependencies are a project's dependencies that are indirectly required by another
dependency.
Maven manages transitive dependencies using a dependency tree, which helps in identifying and
resolving conflicts that may arise due to different versions of the same dependency. This ensures
that your project uses consistent and compatible versions of all required libraries.
For example, let's say your project depends on
Library A, and Library A depends on Library B.
In this case, Library B is a transitive
dependency of your project.
When you include Library A in your project's
Maven configuration (pom.xml), Maven
automatically resolves and includes Library B
as well, since it's required by Library A.
POM.xml
Library A
POM.xml
Library B
transitive
dependency
67Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic: Transitive dependencies
What are transitive dependencies in Maven?
a) Direct dependencies of a project
b) Dependencies required by dependencies of your project
c) Dependencies not required by a project
d) Dependencies that are optional
68Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic: Transitive dependencies
What is the purpose of a dependency tree in Maven?
a) To list all direct dependencies of a project
b) To list all transitive dependencies of a project
c) To resolve conflicts between different versions of the same dependency
d) To exclude certain dependencies from a project
69Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic: Transitive dependencies
If Library A depends on Library B, and Library B depends on Library C, which of the following is a transitive
dependency of your project?
a) Library A
b) Library B
c) Library C
d) None of the above
70
Transitive dependencies conflicts
Conflicting transitive dependencies: When a project has multiple direct dependencies, each with its own set of
transitive dependencies, conflicts can arise if the transitive dependencies have overlapping or incompatible
requirements.
This creates a dependency conflict because two
different versions of Library B are required by your
project's dependencies.
This conflict can lead to various issues, such as:
• Build errors: If the two versions of Library B have
incompatible APIs or features, the build process
may fail due to missing classes or methods.
• Runtime errors: If the build succeeds, your
application may still encounter runtime errors or
unexpected behavior, as the class loader might
load classes from different versions of Library B.
POM.xml
Library A 2.0
Library C
1.0
transitive
dependencies
conflict
Library B 3.0 Library C
2.0
Maven can resolve conflicts using:
• Nearest definition: Maven selects the version of Library B that is closest to your project in the dependency tree
• Exclude one of the conflicting versions of Library B from either Library A or Library C in your project's pom.xml.
71
Transitive dependencies conflicts
Conflicting transitive dependencies: When a project has multiple direct dependencies, each with its own set of
transitive dependencies, conflicts can arise if the transitive dependencies have overlapping or incompatible
requirements.
POM.xml
Library A 2.0
Library C
1.0
Library B 3.0 Library C
2.0
Maven can resolve conflicts using:
• Nearest definition: Maven selects the version of Library B that is closest to your project in the dependency tree
• Exclude one of the conflicting versions of Library B from either Library A or Library C in your project's pom.xml.
com.luxoft.libraryC.ServiceClass {
public void callMe(String param);
}
com.luxoft.libraryC.ServiceClass {
@Deprecated
public void callMe(String param);
public void callMe(int param);
}
transitive
dependencies
conflict
72
Transitive dependencies conflicts: DEMO
first-project
POM.xml
second-project
common-
dependency 1.0
third-project common-
dependency 2.0
Maven can resolve conflicts using:
• Nearest definition: Maven selects the version of Library B that is closest to your project in the dependency tree
• Exclude one of the conflicting versions of Library B from either Library A or Library C in your project's pom.xml.
com.luxoft.libraryC.ServiceClass {
public void callMe(String param);
}
com.luxoft.libraryC.ServiceClass {
@Deprecated
public void callMe(String param);
public void callMe(int param);
}
transitive
dependencies
conflict
Conflicting transitive dependencies: When a project has multiple direct dependencies, each with its own set of
transitive dependencies, conflicts can arise if the transitive dependencies have overlapping or incompatible
requirements.
73
Transitive dependencies conflicts: nearest definition
Conflicting transitive dependencies: When a project has multiple direct dependencies, each with its own set of
transitive dependencies, conflicts can arise if the transitive dependencies have overlapping or incompatible
requirements.
first-project
POM.xml
second-project
common-
dependency 1.0
transitive
dependencies
third-project common-
dependency 2.0
com.luxoft.libraryC.ServiceClass {
public void callMe(String param);
}
com.luxoft.libraryC.ServiceClass {
@Deprecated
public void callMe(String param);
public void callMe(int param);
}
common-
dependency 2.0
74
Transitive dependencies conflicts: exclusion
Conflicting transitive dependencies: When a project has multiple direct dependencies, each with its own set of transitive
dependencies, conflicts can arise if the transitive dependencies have overlapping or incompatible requirements.
first-project
POM.xml
second-project
common-
dependency 1.0
transitive
dependencies
third-project common-
dependency 2.0
Maven can resolve conflicts using:
• Nearest definition: Maven selects the version of Library B that is closest to your project in the dependency tree
• Exclude one of the conflicting versions of Library B from either Library A or Library C in your project's pom.xml.
exc
lus
ion
com.luxoft
second-project
1.0
com.luxoft
common-dependency

75Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic: Transitive dependencies conflicts
What happens when different dependencies require different versions of the same library?
a) Version Conflict
b) Duplicate dependency
c) Circular dependency
d) Maven resolves this type of problems automatically
76Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic: Transitive dependencies conflicts
What can be a consequence of unresolved transitive dependency conflicts?
a) Decreased performance
b) Build errors or runtime issues
c) Complicated dependency management
77Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic: Transitive dependencies conflicts
How can Maven resolve a transitive dependency conflict?
a) By always selecting the latest version of a dependency.
b) By defining the dependency closer to the root in the dependency tree.
c) By excluding older versions of the conflicting dependency from the project.
d) By excluding all versions of the conflicting dependency from the project.
78Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic: Transitive dependencies conflicts
Can a transitive dependency conflict be completely avoided in a Maven project?
a) Yes, by never using more than one direct dependency in a project.
b) Yes, by always using the latest version of a dependency in a project.
c) No, conflicting requirements can arise due to the nature of transitive dependencies.
d) Yes, but only by using Maven's dependency exclusion feature for every dependency in the project.
79Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic: Transitive dependencies conflicts
The project has 2 dependencies, A and B. A contains a transitive dependency on package C version 1.0. B
has a transitive dependency on package C version 2.0, and B uses new features introduced in version 2. Will
there be a version conflict?
a) There will not be a conflict, Maven will take a later version of the C package (2.0)
b) There will not be a conflict, Maven will take an earlier version of the C package (1.0)
c) There will be a conflict, it can be avoided by adding package C to when including package A
d) There will be a conflict, it can be resolved by adding package C to when including package B
e) There will be a conflict, it can be resolved by explicitly adding on C and specifying version
2.0
80
Visualization of the Dependency Tree
Hierarchy list of dependencies:
~/maven-project$ mvn dependency:tree
Extract dependencies to the specified folder
~/maven-project$ mvn dependency:unpack
Dependencies analysis (search for unused):
~/maven-project$ mvn dependency:analyze
Alphabetic list of dependencies:
~/maven-project$ mvn dependency:resolve
April 23, 2023 81
Properties
82
Properties
You can use properties in pom.xml to facilitate writing and configuring your project.
Properties can be accessed by using ${...}
Property types:
• Properties in the pom file: ${project.*}
• Predefined properties: settings.*, env.*, System.getProperty()
• Properties declared in an external file
83
Properties
...
/tmp

${temp.directory}

POM properties are defined with the tag
Example:
84
Properties in the POM file
3.0.6.RELEASE

org.springframework
spring-orm
${spring.version}

org.springframework
spring-aop
${spring.version}

org.springframework
spring-web
${spring.version}

Example:
85Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic: Properties
What is the syntax for using a property in pom.xml?
a) ${property-name}
b) ${property-name}
c) {{property-name}}
d) %property-name%
86Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic: Properties
Where else can you declare properties besides the pom.xml file?
a) in a separate property file
b) in the command line arguments when running Maven
c) in the environment variables
d) all of the above
87Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic: Properties
What is the purpose of using properties in a pom.xml file?
a) to facilitate writing and configuring the project
b) to store sensitive information such as passwords
c) to generate random values during build time
d) to create dynamic dependencies
88
Viewing Properties
POM File Structure
To check the property value, you
can use the goal evaluate in the
plugin maven-help-plugin
~/projects/maven-research$
mvn help:evaluate
[INFO] Searching repository for plugin with prefix: 'help'.
[INFO] ------------------------------------------------------------------------
[INFO] Building maven-research
[INFO] task-segment: [help:evaluate] (aggregator-style)
[INFO] ------------------------------------------------------------------------
[INFO] [help:evaluate {execution: default-cli}]
[INFO] No artifact parameter specified, using 'ua.ypitomets:maven-
research:jar:1.0-SNAPSHOT' as project.
[INFO] Enter the Maven expression i.e. ${project.groupId} or 0
to exit?:
${project.version}
[INFO] 1.0-SNAPSHOT
${project.url}
[INFO] http://maven.apache.org
${java.io.tmpdir}
[INFO]/tmp
89
System Properties
POM File Structure
To view all system properties you
can use the plugin maven-help-
plugin, with the goal system.
~/projects/maven-research$
mvn help:system
====================================================
System Properties
====================================================
java.runtime.name=Java(TM) SE Runtime Environment
sun.boot.library.path=/usr/lib/jvm/java-6-sun-1.6.0.26/jre/lib/i386
java.vm.version=20.1-b02
....
====================================================
Environment Variables
====================================================
COMPIZ_CONFIG_PROFILE=ubuntu
DISPLAY=:0
SSH_AUTH_SOCK=/tmp/keyring-i5CTfv/ssh
UBUNTU_MENUPROXY=libappmenu.so
LESSOPEN=| /usr/bin/lesspipe %s
...
90Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic: System Properties
How can you view all system properties in Maven using the plugin?
a) mvn help:properties
b) mvn system:list
c) mvn help:system
d) mvn properties:system
April 23, 2023 91
Super POM
92
Super POM
POM File Structure
Every POM file inherits super POM by default.
Super POM:
Super POM - virtual pom.xml inside Maven, which contains default configuration inherited by all
projects:
• Central repository information (id = central)
• File structure configuration in the field build
• Default versions of frequently used plugins
Viewing the resulting pom file:
${M2_HOME}/lib/maven-2.0.10-uber.jar
org/apache/maven/project/pom-4.0.0.xml
~$ mvn help:effective-pom
93Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic: POM File Inheritance
Which of the following is inherited by all projects from the Super POM?
a) Central repository URL (id = central)
b) Folder structure configuration
c) Default versions of frequently used plugins
d) Default groupId and artifactId
94Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic: POM File Inheritance
What is the purpose of the Super POM in Maven?
a) To define specific configuration for each project
b) To provide default configuration inherited by all projects
c) To set up project-specific repositories
d) To specify unique plugin versions for each project
95
POM inheritance
POM Inheritance
~$ mvn help:effective-pom
POM.xml
org.springframework.boot
spring-boot-starter-parent
3.0.5
POM.xml
spring-boot-starter-parent
org.springframework.boot
spring-boot-dependencies
3.0.5

POM.xml
spring-boot-dependencies
Super POM
merged together to effective POM
custom POM in Spring project
your project POM has the highest priority
DEMO: spring-project
96Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic: What is POM inheritance in Maven?
Which element in the POM file defines the parent project?
a)
b)
c)
d)
97Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic: What is POM inheritance in Maven?
What is POM inheritance in Maven?
a) It is a feature that allows a project to reuse configurations from another project.
b) It is a way to define the project build configuration.
c) It is a feature that enables the sharing of resources between multiple projects.
d) It is a feature that allows a project to inherit dependency versions from another project.
98Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic: What is POM inheritance in Maven?
What are the benefits of using POM inheritance in Maven?
a) It reduces duplication of configuration settings and ensures consistency across projects.
b) It allows for better collaboration between remote teams.
c) It simplifies project setup and deployment.
d) It ensures better performance.
99Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic: POM inheritance
What is the purpose of an effective POM?
a) To provide a merged view of all the configurations of the project
b) To generate a POM file for a Maven project
c) To provide the artifact IDs of all the dependencies
d) To list all the available Maven plugins
100Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic: POM inheritance
What configuration has the highest priority in POM inheritance?
a) Super POM
b) Spring project POM
c) spring-boot-dependencies
d) spring-boot-starter-parent
101Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic: POM inheritance
Which Spring Boot artifact contains a list of all the dependencies?
a) Super POM
b) spring-boot-starter-parent
c) spring-boot-dependencies
d) spring-core
April 23, 2023 102
Multi-Module Project
103
Multi-Module Maven Project
Maven Module - a standard maven project, except for some specific features.
• When create module it’s necessary to set a parent project.
• The parent project must have packaging=pom
• All the commands invoked on the parent project will be first executed on the child project.
104
Multi-Module Maven Project
Each subproject will have a parent element.
Example
ua.luxoft.plugins
multimodule
1.0-SNAPSHOT
pom
core
api
ui

multimodule
core
src/main/java
pom.xml
api
src/main/java
pom.xml
ui
src/main/java
pom.xml
pom.xml
105
POM File Inheritance
POM File Structure
Inheritance allows to avoid description duplicating.
Example:
Assume that you have several projects, all of which are using log4j library for logging.
ru.apache_maven
testproj1
1.0-SNAPSHOT
ru.apache_maven
parent
1.0-SNAPSHOT

jar

Use inheritance in that new project
Build the project: ~/projects/ua.luxoft-maven$ mvn install
106Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic: Multi-Module Maven Project
What is the purpose of the element in a POM file?
a) To define the project's parent directory
b) To specify the project's dependencies
c) To inherit configurations and dependencies from a parent POM file
d) To execute custom Maven plugins
107Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic: Multi-Module Maven Project
What must be the packaging of the parent project in a Multi-Module Maven Project?
a) war
b) jar
c) pom
d) zip
108Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic: Multi-Module Maven Project
What happens when you execute a command on the parent project in a Multi-Module Maven Project?
a) It is executed sequentially on every child project
b) It is executed simultaneously on all the child projects
c) It is not needed to execute the command on the child project
109Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic: Multi-Module Maven Project
What is the purpose of setting a parent project in a Multi-Module Maven Project?
a) To have multiple unrelated projects in one Maven Project
b) To ensure that all the child projects have a uniform structure
c) To make the child projects independent of each other
d) To avoid having a standard structure in the child projects
April 23, 2023 110
Maven Life Cycle
111
Maven Life Cycle
Maven Life Cycle
Life cycle comprises all the operations over
the module from the build initialization to
deployment.
Maven defines 3 life cycles:
1. clean
2. site
3. default
egg
adult
pupa
larva
112
Maven Life Cycle
Maven Life Cycle
Each step in the life cycle is called Phase
Each life cycle phase can have zero or several Goals
Lifecycle
Phase 1
plugin:goal
plugin:goal
Phase 2
plugin:goal
plugin:goal
113
Maven Life Cycle
Maven Life Cycle
Goal – a task that must be executed to get the project to a certain stage.
mvn clean dependency:copy-dependencies package
execute
lifecycle
clean
execute
copy-dependencies goal
from the dependency plugin
execute
all phases up to package
from the default lifecycle
114
Maven Life Cycle
Maven Life Cycle
Default: Clean:
validate
integration test
compile
test
package
verify
install
deploy
validate
Initialize
generate-sources
process-sources
generate-resources
process-resources
compile
process-classes
post-process
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
pre-clean
pre-site
clean
post-clean
site
post-site
site-deploy
Site:
jar goal
default-jar
package
jar

mvn compile
- validate
- compile
mvn test
- validate
- compile
- test
maven : executes
all phases up to and including , e.g.
115Luxoft, A DXC Technology Company. All rights reserved. © 2023
Plugins
Super POM
maven-jar-plugin maven-compiler-plugin
maven-surefire-
plugin
default-jar
package
jar

default-compile
compile
compile

default-test
test
test

You can generate effective POM
and see which plugins are
attached to which phases.
mvn help:effective-pom > effective-pom.xml
Goals and which phase they are connected to are defined in the Super POM
...
116
Maven Life Cycle, phases and goals
Maven Life Cycle
Concept Description Example Use case
Lifecycle
A sequence of phases that make up a
complete build process. There are three
default lifecycles: default, clean, and site.
Default lifecycle
A complete build process, including
compilation, testing, packaging, and
deployment.
Phase A step within a lifecycle that represents a specific stage of the build process.
compile, test,
package
Compile: Compile the source code.
Test: Run unit tests. Package: Package
compiled code into distributable format.
Goal A specific task that is bound to a phase and is responsible for executing a particular action.
compiler:compile,
surefire:test, jar:jar
compiler:compile: Compile Java source
code.
surefire:test: Run unit tests.
jar:jar: Create a JAR file.
Lifecycle: default
Phase: validate
goal goal goal goal
Phase: compile
goal goal goal
...
117Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic: Maven Life Cycle
What is a Maven Life Cycle?
a) A series of steps that define how a project is built, packaged, and deployed
b) A programming language used to build web applications
c) A tool for version control management
d) A database management system
118Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic: Maven Life Cycle
What is a Maven Life Cycle Phase?
a) A tool used to debug code
b) A programming language used to build an application
c) A storage system for the built project
d) A step in the life cycle that has zero or several goals
119Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic: Maven Life Cycle
What is a Maven Life Cycle Goal?
a) A software framework used for testing
b) A type of coding language
c) A task that must be executed within a phase
120Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic: Maven Life Cycle
What are the possible life cycles in Maven?
a) build, deploy, test, release
b) clean, site, default
c) initialize, update, finalize, refresh
121Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic: Maven Life Cycle
What are possible Maven phases?
a) deploy
b) compile
c) run
d) package
122Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic: Maven Life Cycle
Which phase of the Maven life cycle involves creating a distributable archive?
a) package
b) clean
c) install
d) validate
123Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic: Maven Life Cycle
What are the valid phases of Maven default Life Cycle?
a) compile, run, test, deploy
b) initialize, build, deploy, clean
c) validate, compile, test, package, install, deploy
d) create, build, deliver, test
April 23, 2023 124
Maven Profiles
125
Profiles
Profile – a custom set of configurations in the POM file.
• Profile contains configuration that will be added to the main configuration or replace it.
• Profiles are used to provide platform independency in scripts.
• Different profiles will lead to different build results.
126
Profiles example
Project Life Cycle
...
development
true

dev

com.h2database
h2
1.4.200
runtime

production
prod

org.postgresql
postgresql
42.2.23
runtime

development profile production profile
mvn install -P development
mvn install -P production
127Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic: Profiles
What is a profile in Maven?
a) A type of dependency
b) A custom set of configurations in the POM file
c) A project management tool
d) An IDE for Java development
128Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic: Profiles
Why do we need profiles in Maven?
a) To provide platform independency in scripts
b) To customize the build process
c) To add or replace configurations in the main configuration
d) To simplify the project structure
129Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic: Profiles
What does a profile contain in the POM file?
a) Additional configuration that will be added to the main configuration
b) Replacement configuration that will replace the main configuration
c) The entire main configuration
d) A list of dependencies
130
Profile Scopes
Project Life Cycle
There are 3 profile scopes:
• Project scope – profiles defined in the POM file
• User scope – profiles defined in settings.xml in the user directory
(%USER_HOME/.m2/settings.xml)
• Global scope – profiles defined in settings.xml in the Maven directory
(%M3_HOME/.m2/settings.xml)
There are different levels at which profiles can be defined, which determines their
scope of influence in the build process.
131Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic: Profile Scopes
Where are global scope profiles defined in Maven?
a) In the POM file
b) In the user directory
c) In the Maven directory
d) In the profiles.xml file
132Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic: Profile Scopes
Where are the user-defined profiles saved in Maven?
a) In the project descriptor file
b) In the global Maven directory
c) In the user's home directory
d) In the local repository
133
Profile Activation
mvn groupId:artifactId:goal -P p1,p2
Ways to activate profiles:
Explicitly – using the key -P
...
p1
p3

...

Using Maven configuration – tag
target/generated-sources/axistools/wsdl2java/org/apache/maven
target/generated-sources/axistsools/src

...

By checking of the files – tag
134Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic: Profile Activation
What is a profile activation?
a) The process of starting a Maven project
b) The process of identifying which profiles are active for a specific build
c) The process of deactivating a Maven profile
d) None of the above
135Luxoft, A DXC Technology Company. All rights reserved. © 2023
Question on topic: Profile Activation
What are the ways to activate profiles in Maven?
a) Explicitly – using the key -P
b) Using Maven configuration tag
c) By checking existence of the files with tag
d) All of the above
136
Profile Activation
Ways to activate profiles:
Windows XP
Windows
x86
5.1.2600

...

• System configuration
debug

...

• Using the existing properties
mvn groupId:artifactId:goal -Denvironment=test
137
Profile Activation
Environment Parameters
17

Profile Deactivation
~$ mvn groupId:artifactId:goal -P !profile-1,!profile-2
138
Profile Declaration
(1)
development
localhost

org.hsqldb
hsqldb
2.0.0

(2)
productionServer
not a localhost

postgresql
postgresql
9.0-801.jdbc4

.....

The contains the same
declarations as the tag
Example
139
Traps
...
appserverConfig
/path/

appserverConfig

...

...
org.myco.plugins
integration-plugin
1.0
${server.home}

...

...

Incorrect usage of profiles may lead to inoperability or platform-dependence of scripts!
1. Using profiles within the user scope (external profiles).
~/.m2/settings.xml pom.xml
140
Traps
...
dev-1
env
dev-1

path/to/server-1

dev-2
env
dev-2

path/to/server-2

...

...
org.myco.plugins
integration-plugin
1.0
${server.home}

...

...

2. Incomplete profile specification.
mvn -Denv=dev-2 integration-test
mvn -Denv=dev-1 integration-test
mvn -Denv=prod integration-test
141
Checking for Active Profiles
You can find out which of the profiles is active by using the command:
~$ mvn help:active-profiles
[INFO] There are no active profiles. OR [INFO] The following profiles are active:
- appServerConfig (source: settings.xml)
>mvn help:active-profiles -P appserverConfig-dev
[INFO]The following profiles are active: OR [INFO] The following profiles are active:
- appServerConfig-def (source: pom.xml) - appServerConfig (source: settings.xml)
- appServerConfig-def (source: pom.xml)
April 23, 2023 142© 2023 Luxoft, A DXC Technology Company. All rights reserved.
Thank you!
Please share your feedback.
Your opinion is important to us!
Luxoft, A DXC Technology Company. All rights reserved. © 2023


essay、essay代写