Maven - ilya-khadykin/notes-outdated GitHub Wiki

TODO:

Convention over configuration

What is Maven?

  • Maven is a build tool
  • Dependency management tool
  • Project management tool
  • Standardized approach to building software
  • Command line tool
  • IDE integration

Maven provides developers ways to manage following:

  • Builds
  • Documentation
  • Reporting
  • Dependencies
  • SCMs
  • Releases
  • Distribution
  • mailing list

Convention over Configuration

Maven uses Convention over Configuration which means developers are not required to create build process themselves.

Item Default
source code ${basedir}/src/main/java
resources ${basedir}/src/main/resources
Tests ${basedir}/src/test
distributable JAR ${basedir}/target
Complied byte code ${basedir}/target/classes

In order to build the project, Maven provides developers options to mention life-cycle goals and project dependencies (that rely on Maven pluging capabilities and on its default conventions). Much of the project management and build related tasks are maintained by Maven plugins.

How Does Maven Work?

How Does Maven Work

Project Object Model (POM)

POM stands for Project Object Model. It is fundamental Unit of Work in Maven. It is an XML file. It always resides in the base directory of the project as pom.xml.

The POM contains information about the project and various configuration detail used by Maven to build the project(s).

POM also contains the goals and plugins. While executing a task or goal, Maven looks for the POM in the current directory. It reads the POM, gets the needed configuration information, then executes the goal. Some of the configuration that can be specified in the POM are following:

  • project dependencies
  • plugins
  • goals
  • build profiles
  • project version
  • developers
  • mailing list

Before creating a POM, we should first decide the project group (groupId), its name(artifactId) and its version as these attributes help in uniquely identifying the project in repository.

POM is an .xml file

  • describes, configures and customizes a Maven Project
  • Maven reads the pom.xml file to build a project
  • defines the "address" for the project artifact using a coordinate system
  • specifies project information, plugins, goals, dependencies and profiles
Node Description
groupId This is an Id of project's group. This is generally unique amongst an organization or a project. For example, a banking group com.company.bank has all bank related projects.
artifactId This is an Id of the project.This is generally name of the project. For example, consumer-banking. Along with the groupId, the artifactId defines the artifact's location within the repository.
version This is the version of the project.Along with the groupId, It is used within an artifact's repository to separate versions from each other. For example:
com.company.bank:consumer-banking:1.0
com.company.bank:consumer-banking:1.1

Super POM

All POMs inherit from a parent (despite explicitly defined or not). This base POM is known as the Super POM, and contains values inherited by default.

Maven use the effective pom (configuration from super pom plus project configuration) to execute relevant goal. It helps developer to specify minimum configuration detail in his/her pom.xml. Although configurations can be overridden easily.

An easy way to look at the default configurations of the super POM is by running the following command:

mvn help:effective-pom

Repositories

  • hold build artifacts and dependencies of varying types
  • Local repositories (local cache)
  • remote repositories
  • local repository takes precedence during dependency resolution

The Central Repository

Plugins and Goals

  • Plugin is a collection of goals
  • Example: compiler plugin
  • Goals perform the actions in Maven builds
  • All work is done via plugins and goals
  • Called independently or as part of a lifecycle phase

Lifecicle and phases

  • Lifecycle is a sequence of named phases
  • Phases are executed sequentially
  • 3 lifecycles: clean, default, site
  • Executing a phase executes all previous phases

Executing install phase:

mvn install

Installation

Follow the instructions on the official website - http://maven.apache.org/

pom.xml example

<project>
	<modelVersion>4.0.0</modelVersion>
	<artifactId>maven-hello-world</artifactId>
	<groupId>com.example.maven.demo</groupId>
	<version>1.0</version>
	<packaging>jar</packaging>
</project>

Build with mvn package

Maven Standard Directory Layout

More info - https://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html

Inheritance in Maven and Base pom.xml

Base pom.xml is located in C:\apache-maven-3.3.9\lib\maven-model-builder-3.3.9.jar (Windows)

Environments in Maven

You can configure your environment using <profiles>

<profiles>
  <profile>
    <id>production</id>
  </profile>
</profiles>

Run created production profile with mvn -Pproduction package

Environment Variables in Maven

Environment variable should be set up in OS

<activation>
  <property>
    <name>env.PACKAGE_ENV</name>
    <value>PROD</value>
  <property>
</activation>

Passing environment variable to Maven

mvn -DmyVariable=someValue install

Commands

mvn site - create a static web site about the project based on information from pom.xml mvn help:effective-pom - see the actual pom.xml with all inherited settings mvn archetype:generate - create pom.xml from template

References

Proxy settings

You can configure proxy settings in ${MAVEN_HOME}\conf\settings.xml

    <proxy>
      <id>optional</id>
      <active>true</active>
      <protocol>http</protocol>
      <username>proxyuser</username>
      <password>proxypass</password>
      <host>proxy.host.net</host>
      <port>80</port>
      <nonProxyHosts>local.net|some.host.com</nonProxyHosts>
    </proxy>

Local repository

Local repository is stored in ${USER_HOME}/.m2/repository (~/.m2/repository) by default

User defined settings

settings.xml placed in ${USER_HOME}/.m2/ (~/.m2/)

⚠️ **GitHub.com Fallback** ⚠️