Getting started with Apache Maven
What is Maven?
Download and Installation
Configuring Maven
First Maven Project
What is a POM?
Using External Dependencies
Project Lifecycle Management
Using External Repositories
Using Plugins
https://notebookbft.wordpress.com/
2. Outline
● What is Maven?
● Download and Installation
● Configuring Maven
● First Maven Project
● What is a POM?
● Using External Dependencies
● Project Lifecycle Management
● Using External Repositories
● Using Plugins
2
3. What is Maven?
Maven is essentially a project management tool and as such provides a way to
help with managing:
● Builds
● Documentation
● Reporting
● Dependencies
● SCM
● Releases
● Distribution
3
5. Configuring Maven
Maven configuration occurs at 3 levels:
● Installation - this is configuration added once for a Maven installation
● User - this is configuration specific to a particular user
● Project - most static configuration occurs in pom.xml
You can specify your user configuration in
${user.home}/.m2/settings.xml.
● Configuring your Local Repository
● Configuring a Proxy
● Security and Deployment Settings
5
6. Configuring your Local Repository
The location of your local repository can be changed in your user configuration.
The default value is ${user.home}/.m2/repository/.
Note: The local repository must be an absolute path.
6
8. First Maven Project
To create our first Maven project we are going to use Maven's archetype
mechanism.
An archetype is defined as an original pattern or model from which all other things
of the same kind are made.
mvn -B archetype:generate
-DarchetypeGroupId=org.apache.maven.archetypes
-DgroupId=com.mycompany.app
-DartifactId=my-app
8
10. What is a POM? - Project Object Model
● It is an XML file that contains information about the project and configuration
details used by Maven to build the project.
10
11. What is a POM? contd.
XML
Artifact Descriptor
Hierarchy
Dependencies
11
13. Transitive Dependencies
There are two types of Maven
dependencies:
● Direct: These are dependencies defined
in your pom.xml file under the
<dependencies/> section.
● Transitive: These are dependencies
that are dependencies of your direct
dependencies.
13
14. Dependency Scope
Dependency scope is used to limit the transitivity of a dependency, and also to
affect the classpath used for various build tasks.
compile
This is the default scope, used if none is specified. Compile dependencies are
available in all classpaths of a project. Furthermore, those dependencies are
propagated to dependent projects.
provided
This is much like compile, but indicates you expect the JDK or a container to
provide the dependency at runtime. This scope is only available on the compilation
and test classpath, and is not transitive. 14
15. Dependency Scope contd.
runtime
This scope indicates that the dependency is not required for compilation, but is for
execution. It is in the runtime and test classpaths, but not the compile classpath.
test
This scope indicates that the dependency is not required for normal use of the
application, and is only available for the test compilation and execution phases.
This scope is not transitive.
system
This scope is similar to provided except that you have to provide the JAR which
contains it explicitly. The artifact is always available and is not looked up in a
repository. 15
16. Dependency Exclusions
Since maven resolves dependencies
transitively, it is possible for
unwanted dependencies to be
included in your project's classpath.
Therefore explicit dependency
exclusion can be used.
Excluded artifacts will not be added
to your project's classpath.
16
17. Project Lifecycle Management
Compiling application sources - mvn compile
Compile test sources and run unit tests - mvn test
Creating a JAR and installing it in my local repository - mvn package
-mvn install
Removing the target directory -mvn clean
17
18. Using External Repositories
A repository in Maven is used to hold
build artifacts and dependencies of
varying types.
There are strictly only two types of
repositories: local and remote
Offline: mvn -o package
Force Update: mvn install -U
18
19. Using Plugins
Whenever you want to customise the
build for a Maven project, this is done
by adding or reconfiguring plugins.
For this example, we will configure the
Java compiler to allow specific java
version ({java.version}) sources.
19