Training Guide ‐ Maven Fundamentals - vinhtbkit/bkit-kb GitHub Wiki

Preface

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

Qualification Criteria

  • Installing Maven on your OS
  • Maven Lifecycle
  • Maven Plugins
  • Dependency management and its working mechanism

Maven Overview

Installing Maven on your OS

Maven Lifecycle

1. Understanding the Maven lifecycle, phases, and goals

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 more goals can be executed.

2. Understanding the pom file

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

3. Settings

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

4. Profiles

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

Dependency

1. The structure of the tag

2. Transitive Dependencies

3. Dependency Scope

4. Dependency Management

5. Dependency Version

  • The mechanism for downloading versions of artifacts.
  • Understanding SNAPSHOT dependencies

Quizzes

  • 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 this A dependency, there are other dependencies as shown below. Can I use the D dependency in my project? If yes, which version of the D dependency will be used?
dependency

References

Archetypes

1. Introduction

  • What is Archetype?
  • Using an Archetype
  • Provided Archetypes
  • What makes up an Archetype? (OPTIONAL)

2. Creating Archetypes (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

Quizzes

  • The command mvn archetype:generate will perform what action?

References

Plugins

1. Configuration

  • Build plugin
  • Reporting plugin

2. Plugins bound to Lifecycles

  • 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

3. Code Quality Plugins (OPTIONAL)

  • Maven JaCoCo plugin
  • Maven SonarQube plugin

4. Create a plugin (OPTIONAL)

  • Mojo

Quizzes

  • When running the mvn test command, which plugin will be used?

References

Assignment

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.
⚠️ **GitHub.com Fallback** ⚠️