• Home
  • LLMs
  • Python
  • Docker
  • Kubernetes
  • Java
  • Maven
  • All
  • About
Maven | Getting Started with Maven
  1. Introduction
  2. Creating a Maven Project
  3. The Configuration File of a Maven Project: "pom.xml"
  4. Maven Project Attributes
  5. Variables
  6. Default Configuration of a Maven Project
  7. Maven Plugin Goals
  8. Executing a Plugin Goal in Maven
  9. Executing Multiple Goals
  10. Maven Build Lifecycle Phases
  11. Goals Associated by Default with a Phase
  12. Packaging: Goals Associated by Default with a Phase
  13. Associating Goals with a Phase

  1. Introduction
    See the following page for a detailed description of Maven: http://maven.apache.org/what-is-maven.html

    See the following page to install and configure Maven: Installing and Configuring Maven

    Maven is a project management tool based on the concept of a "Project Object Model" (POM).
    Maven allows you to build a project, generate reports, generate documentation, and more.

    Maven is based on the following concepts:
    • Standardized directory structure (source, test, resources, ...)
    • Declarative dependency management (pom.xml)
    • Plugins
    • Archetypes (templates)

    Integration with external services:
    • Maven SCM Plugin (source code management)
    • Maven Release Plugin
    • Maven Wagon Plugin (HTTP, FTP, ...)
  2. Creating a Maven Project
    To create a Maven project, you can use the "mvn archetype:generate" command.
    It uses the "archetype" plugin and the "generate" goal.
    $ mvn archetype:generate \
      -DarchetypeGroupId=org.apache.maven.archetypes \
      -DarchetypeArtifactId=maven-archetype-quickstart \
      -DarchetypeVersion=1.5 \
      -DgroupId=mtitek.maven.samples \
      -DartifactId=mtitek-maven-samples \
      -Dpackage=mtitek.maven.samples \
      -Dversion=1.0.0-SNAPSHOT \
      -DinteractiveMode=false
    Notes:
    - On Linux, the character "\" allows you to write a command over multiple lines.
    - On Windows, you should use the character "^".

    If you run the "mvn archetype:generate" command, a list of available archetypes (project templates) will be displayed:
    $ mvn archetype:generate
    [INFO] Scanning for projects...
    [INFO]
    [INFO] ------------------< org.apache.maven:standalone-pom >-------------------
    [INFO] Building Maven Stub Project (No POM) 1
    [INFO] --------------------------------[ pom ]---------------------------------
    [INFO]
    [INFO] >>> archetype:3.4.0:generate (default-cli) > generate-sources @ standalone-pom >>>
    [INFO]
    [INFO] <<< archetype:3.4.0:generate (default-cli) < generate-sources @ standalone-pom <<<
    [INFO]
    [INFO]
    [INFO] --- archetype:3.4.0:generate (default-cli) @ standalone-pom ---
    [INFO] Generating project in Interactive mode
    [INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven.archetypes:maven-archetype-quickstart:1.0)
    Choose archetype:
    1: remote -> am.ik.archetype:elm-spring-boot-blank-archetype (Blank multi project for Spring Boot + Elm)
    2: remote -> am.ik.archetype:graalvm-blank-archetype (Blank project for GraalVM)
    ...
    3562: remote -> za.co.absa.hyperdrive:component-archetype_2.11 (-)
    3563: remote -> za.co.absa.hyperdrive:component-archetype_2.12 (-)
    Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 2253:
  3. The Configuration File of a Maven Project: "pom.xml"
    Each Maven project comes with a configuration file named "pom.xml", which contains the project's properties: its identifier, group ID, version, packaging type, dependencies, build options, and more.

    This file is located at the root of the project. Here is an example of the "pom.xml" file for the "mtitek-maven-samples" project:
    <project xmlns="http://maven.apache.org/POM/4.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>mtitek.maven.samples</groupId>
        <artifactId>mtitek-maven-samples</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    
        <!-- <packaging>jar</packaging> --> <!-- if packaging is not specified, the default value is jar -->
    </project>
    Maven commands are always executed on the effective version of the "pom.xml" file.

    The final pom.xml file is the result of merging:
    • the project's pom.xml file,
    • the parent projects' pom.xml files,
    • and the super POM.

    In addition to the values defined in the project's own properties, the final POM inherits:
    • properties from parent projects,
    • properties from the super POM,
    • properties from Maven's configuration files.

    To display the effective version of the "pom.xml" file, you can run the mvn help:effective-pom command from the project folder.

    In Eclipse (m2e), simply open the project's "pom.xml" file and click the "Effective POM" tab.

    Note: The Maven super POM (/org/apache/maven/model/pom-4.0.0.xml) is located within the JAR file: ${MAVEN_HOME}/lib/maven-model-builder-3.9.9.jar

    See the file: pom-4.0.0.xml
  4. Maven Project Attributes
    A Maven project is identified by the following four attributes: groupId, artifactId, version, packaging

    To modify these attributes, you need to edit them in the "pom.xml" file of your Maven project.

    • groupId
      A Maven project belongs to a group identified by a unique identifier (groupId).
      A group can contain multiple projects.
      The group identifier should typically be a sequence of words separated by dots.
      The groupId can consist of the company's domain name followed by a series of words identifying a set of projects.

      For example: org.apache.maven.plugins

      The words in the group identifier correspond to directories created in the folder: "${user.home}/.m2/repository/"

      For example: ${user.home}/.m2/repository/org/apache/maven/plugins
      |+ ${user.home}
         |+ .m2
            |+ repository
               |+ org
                  |+ apache
                     |+ maven
                        |+ plugins
    • artifactId
      A project is identified by an identifier (artifactId) unique within a group of projects.
      Typically, the project identifier is a sequence of words separated by the "-" character.
      These words can correspond to part of the words in the group identifier, plus words specific to the project.

      For example: maven-help-plugin

      The project identifier corresponds to a directory created in the group folder: "${user.home}/.m2/repository/[GROUP_PATH]/"

      For example: ${user.home}/.m2/repository/org/apache/maven/plugins/maven-help-plugin
      |+ ${user.home}
         |+ .m2
            |+ repository
               |+ org
                  |+ apache
                     |+ maven
                        |+ plugins
                           |+ maven-help-plugin
    • version
      A project can have multiple versions.

      A project's version is typically defined as follows: MajorVersion.MinorVersion.IncrementalVersion-qualifier

      The values of MajorVersion, MinorVersion, and IncrementalVersion are numeric.
      The value of qualifier is a string.

      Examples: 1, 1.0, 1.0.1, 1.0.1-SNAPSHOT, 1.0.1-23, 1.0.1-beta, 1.0.1-beta-23

      The version that Maven treats as the most recent is the one with the highest MajorVersion value. If those are the same, it looks at the highest MinorVersion. If that's also the same, it checks the IncrementalVersion. If all of those match, Maven finally compares the qualifier values. Since qualifiers are just strings, it’s important to keep their format consistent across versions— otherwise, Maven might mistakenly pick an older version.

      Note that versions with a qualifier are considered older than the same version without a qualifier.

      Also note that versions not following Maven's standard will be compared as strings. This can result in Maven selecting an older version instead of a newer one. It can also affect the behavior of some Maven plugins (release, version, etc.).

      Adding SNAPSHOT to a version allows Maven to assign a timestamp to the project's components when they are deployed. Thus, when referencing a component as a dependency, Maven will fetch the version with the most recent timestamp.

      Each new version of the project corresponds to a directory created in the folder: "${user.home}/.m2/repository/[GROUP_PATH]/[ARTIFACT_ID]/"
      For example: ${user.home}/.m2/repository/org/apache/maven/plugins/maven-help-plugin/2.1.1
      |+ ${user.home}
         |+ .m2
            |+ repository
               |+ org
                  |+ apache
                     |+ maven
                        |+ plugins
                           |+ maven-help-plugin
                              |+ 2.1.1
    • packaging
      The packaging attribute defines the project's type and specifies how it will be built.
      The value of this attribute can be: "jar" (default), "war", "ejb", "ear", "pom", "maven-plugin", ...
  5. Variables
    Maven allows the use of predefined or custom variables within the pom.xml file.
    If a variable reference (either a POM model element or a property) is defined in a child pom.xml file, its value will override the value defined in a parent project.

    Any POM model element with a simple value can be referenced as a variable.
    For example, to use the <project><version> element as a variable, you should reference it as follows: ${project.version}

    To reference properties defined in the settings.xml file, you must use the "settings." prefix (for example: ${settings.localRepository}).

    To reference environment variable values, use the "env." prefix (for example: ${env.HOME}).

    System properties can be referenced directly (for example: ${user.home}).

    You can also directly reference a property defined in the pom.xml file (within <properties>) as: ${property1}

    Maven provides three predefined variables:
    • project.basedir: The path to the project directory (For example: $HOME/dev/mtitek-maven-samples).

    • project.baseUri: The project directory path, represented as a URI (For example: file:$HOME/dev/mtitek-maven-samples/).

    • maven.build.timestamp: The (UTC) date and time when the build started.

    The build timestamp format can be customized by declaring the maven.build.timestamp.format property:
    <project>
      <properties>
        <maven.build.timestamp.format>yyyy-MM-dd'T'HH:mm:ss'Z'</maven.build.timestamp.format> <!-- default value -->
      </properties>
    </project>
    The date format must follow the syntax defined by SimpleDateFormat.
  6. Default Configuration of a Maven Project
    Here is the default configuration for Maven projects:
    • The source code must be placed in the folder: "${project.basedir}/src/main/java/".

    • The unit test source code must be placed in the folder: "${project.basedir}/src/test/java/".

    • Resource files must be placed in the folder: "${project.basedir}/src/main/resources/".

    • Unit test resource files must be placed in the folder: "${project.basedir}/src/test/resources/".

    • Maven generates the project's compiled bytecode in the folder: "${project.basedir}/target/classes/".

    • Maven generates the compiled test bytecode in the folder: "${project.basedir}/target/test-classes/".

    • Maven generates the packaged project file (.jar, .war, .ear) in the folder: "${project.basedir}/target/".

    To verify this configuration, you can display the effective version of the "pom.xml" file.

    Below is a view of the effective "pom.xml" file of the "mtitek-maven-samples" project:
    <build>
        <sourceDirectory>$HOME/dev/mtitek-maven-samples/src/main/java</sourceDirectory>
        <testSourceDirectory>$HOME/dev/mtitek-maven-samples/src/test/java</testSourceDirectory>
    
        <outputDirectory>$HOME/dev/mtitek-maven-samples/target/classes</outputDirectory>
        <testOutputDirectory>$HOME/dev/mtitek-maven-samples/target/test-classes</testOutputDirectory>
    
        <resources>
            <resource>
                <directory>$HOME/dev/mtitek-maven-samples/src/main/resources</directory>
            </resource>
        </resources>
    
        <testResources>
            <testResource>
                <directory>$HOME/dev/mtitek-maven-samples/src/test/resources</directory>
            </testResource>
        </testResources>
    
        <directory>$HOME/dev/mtitek-maven-samples/target</directory>
    
    <!-- ... -->
  7. Maven Plugin Goals
    Maven provides services (compilation, build, reporting, etc.) using plugins.
    A plugin is a program that implements features grouped into tasks (or units of work).
    In Maven, these tasks are called goals.
    A plugin exposes its goals using the "plugin.xml" file located in the "META-INF\maven" directory of the plugin's JAR file.

    For example, the "plugin.xml" file of the "maven-help-plugin-2.1.1" plugin has the following format:
    <?xml version="1.0" encoding="UTF-8" ?>
    <plugin>
        <name>Maven Help Plugin</name>
    
        <description>...</description>
    
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-help-plugin</artifactId>
        <version>2.1.1</version>
    
        <goalPrefix>help</goalPrefix>
    
        <!-- ... -->
    
        <mojos>
            <mojo>
                <goal>describe</goal>
    
                <!-- ... -->
    
                <parameters>
                    <parameter>
                        <name>artifactId</name>
                        <type>java.lang.String</type>
                        <required>false</required>
    
                        <!-- ... -->
                    </parameter>
    
                    <!-- ... -->
                </parameters>
    
                <configuration>
                    <artifactId implementation="java.lang.String">${artifactId}</artifactId>
    
                    <!-- ... -->
                </configuration>
    
                <!-- ... -->
            </mojo>
    
            <!-- ... -->
        </mojos>
    
        <dependencies>
            <!-- ... -->
        </dependencies>
    </plugin>
  8. Executing a Plugin Goal in Maven
    A plugin specifies a prefix (or alias) using the "goalPrefix" element.
    For example, the "maven-help-plugin" uses the following prefix: <goalPrefix>help</goalPrefix>

    Maven maintains a list of plugins and their prefixes in the "maven-metadata-central.xml" file.
    There are two copies of this file:
    • One is located in the folder "${user.home}/.m2/repository/org/apache/maven/plugins/", which contains the list of core plugins.

    • The other is located in the folder "${user.home}/.m2/repository/org/codehaus/mojo/", which contains the list of non-core plugins.

    This file has the following format:
    <?xml version="1.0" encoding="UTF-8" ?>
    <metadata>
        <plugins>
            <plugin>
                <name>Maven Help Plugin</name>
                <artifactId>maven-help-plugin</artifactId>
                <prefix>help</prefix>
            </plugin>
    
            <!-- ... -->
        </plugins>
    </metadata>
    To execute a plugin goal, you use the plugin prefix followed by the goal name.
    For example, to execute the "help" goal of the "maven-help-plugin" (which has the prefix help), use the following syntax:
    $ mvn help:help
    You can also use the plugin coordinates ("groupId":"artifactId":"version") to execute a goal:
    $ mvn org.apache.maven.plugins:maven-help-plugin:help
    $ mvn org.apache.maven.plugins:maven-help-plugin:3.3.0:help
    If a project defines multiple executions (with different configurations) for a goal in the pom.xml file, you can specify an execution by appending its identifier (id) after the goal name, separated by the character '@':
    $ mvn plugin-prefix:plugin-goal@goal-execution-id
    To pass values to goal parameters, use the syntax "-Dparam_name=param_value":
    $ mvn help:help -Ddetail=true
    $ mvn org.apache.maven.plugins:maven-help-plugin:3.3.0:help -Ddetail=true
    All writable parameters of the plugin can be initialized in the project's pom.xml file in the "<configuration>" section of the plugin:
    ...
    <configuration>
      <detail>true</detail>
    </configuration>
    ...
    Parameters that can be initialized either via the command line or in the pom.xml file's "<properties>" section are those that define a "property" attribute (User property).

    For example, the "detail" parameter can be initialized on the command line:
    $ mvn help:help -Ddetail=true
    ...
    Available parameters:
      detail (Default: false)
      If true, display all settable properties for each goal.
      User property: detail
    ...
    It can also be initialized in the project's pom.xml file in the "<properties>" section:
    ...
    <properties>
      <detail>true</detail>
    </properties>
    ...
  9. Executing Multiple Goals
    In some cases, you may want to group the execution of several goals into a single command line.
    For example, to execute both the "compile" goal of the "compiler" plugin and the "jar" goal of the "jar" plugin in one command, use the syntax: mvn compiler:compile jar:jar

    Here is the output of running this command on the "mtitek-maven-samples" project:
    [INFO] ------------------------------------------------------------------------
    [INFO] Building mtitek-maven-samples 1.0.0-SNAPSHOT
    [INFO] ------------------------------------------------------------------------
    [INFO]
    [INFO] --- maven-compiler-plugin:2.3.2:compile (default-cli) @ mtitek-maven-samples ---
    [INFO]
    [INFO] --- maven-jar-plugin:2.3.2:jar (default-cli) @ mtitek-maven-samples ---
    [INFO]
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    You can see that the build process executed:
    • First, the "compile" goal of the "maven-compiler-plugin" (version 2.3.2),

    • And then the "jar" goal of the "maven-jar-plugin" (version 2.3.2).

    The order in which goals are executed depends on their position in the command line.
    The order is important for correct execution, as one goal may depend on the result of another.
    For example, you cannot package a Java project before compiling it.
  10. Maven Build Lifecycle Phases
    Executing multiple goals in a single command is useful, but it can be impractical in scenarios where you need different command lines for each project, especially when managing goal-specific parameters.

    Maven provides a way to use aliases that correspond to build lifecycles of a Maven project.
    Each lifecycle is made up of several phases, and each phase can include multiple goals.

    See the following page for more details on the phases in each lifecycle: http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html

    Maven defines three built-in lifecycles:
    • clean: handles project cleaning.

    • default: handles project deployment.

    • site: handles the creation of project documentation (site).

    The phases in the "clean" lifecycle are:
    • pre-clean
    • clean
    • post-clean

    The phases in the "default" lifecycle are:
    • validate
    • initialize
    • generate-sources
    • process-sources
    • generate-resources
    • process-resources
    • compile
    • process-classes
    • 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

    The phases in the "site" lifecycle are:
    • pre-site
    • site
    • post-site
    • site-deploy

    The phases in each lifecycle are defined in a specific order, and they are executed in that order.
    Additionally, when you specify a particular phase on the command line, Maven ensures that all phases preceding it (in the predefined order) are executed first.

    To execute the "install" phase and all phases leading up to it, simply run: mvn install
    Here is the output of this command on the "mtitek-maven-samples" project:
    [INFO] ------------------------------------------------------------------------
    [INFO] Building mtitek-maven-samples 1.0.0-SNAPSHOT
    [INFO] ------------------------------------------------------------------------
    [INFO]
    [INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ mtitek-maven-samples ---
    [INFO]
    [INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ mtitek-maven-samples ---
    [INFO]
    [INFO] --- maven-resources-plugin:2.5:testResources (default-testResources) @ mtitek-maven-samples ---
    [INFO]
    [INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ mtitek-maven-samples ---
    [INFO]
    [INFO] --- maven-surefire-plugin:2.10:test (default-test) @ mtitek-maven-samples ---
    [INFO]
    [INFO] --- maven-jar-plugin:2.3.2:jar (default-jar) @ mtitek-maven-samples ---
    [INFO]
    [INFO] --- maven-install-plugin:2.3.1:install (default-install) @ mtitek-maven-samples ---
    [INFO]
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
  11. Goals Associated by Default with a Phase
    Some phases have goals associated with them by default.

    • Phases in the "clean" lifecycle:
      phase                    goal
      ----------------------   -------------------------------------
      clean                    clean:clean
    • Phases in the "site" lifecycle:
      phase                    goal
      ----------------------   -------------------------------------
      site                     site:site
      site-deploy              site:deploy
  12. Packaging: Goals Associated by Default with a Phase
    One factor that influences the execution of phases in the "default" lifecycle is the packaging type (jar, war, ...):
    Each packaging type may require the execution of all or part of the lifecycle phases.
    The selected packaging type assigns specific goals to each phase.

    • "jar", "war", "ejb", "rar": These packaging types require the execution of the following phases and goals:
      phase                    goal
      ----------------------   -------------------------------------
      process-resources        resources:resources
      compile                  compiler:compile
      process-test-resources   resources:testResources
      test-compile             compiler:testCompile
      test                     surefire:test
      package                  jar:jar / war:war / ejb:ejb / rar:rar
      install                  install:install
      deploy                   deploy:deploy
    • "ear": This packaging type requires the execution of the following phases and goals:
      phase                    goal
      ----------------------   -------------------------------------
      generate-resources       ear:generate-application-xml
      process-resources        resources:resources
      package                  ear:ear
      install                  install:install
      deploy                   deploy:deploy
    • "pom": This packaging type requires the execution of the following phases and goals:
      phase                    goal
      ----------------------   -------------------------------------
      package                  site:attach-descriptor
      install                  install:install
      deploy                   deploy:deploy
    • "maven-plugin": This packaging type requires the execution of the following phases and goals:
      phase                    goal
      ----------------------   -------------------------------------
      generate-resources       plugin:descriptor
      process-resources        resources:resources
      compile                  compiler:compile
      process-test-resources   resources:testResources
      test-compile             compiler:testCompile
      test                     surefire:test
      package                  jar:jar + plugin:addPluginArtifactMetadata
      install                  install:install
      deploy                   deploy:deploy
  13. Associating Goals with a Phase
    By default, Maven associates one or more goals from Maven plugins with a lifecycle phase.
    You can view the default configuration for your project by examining the effective version of the "pom.xml" file.
    You can also see the details of the executed goals by adding the "-X" option to the "mvn" command.

    The default configuration can be modified in the "pom.xml" file.
    You can also add new goals from other plugins and associate them with a specific phase of the selected packaging (jar, war, ejb, ear, ...) for your project.

    For example, for a project with "jar" packaging, Maven will associate the "jar" goal of the "maven-jar-plugin" with the "package" phase (id: "default-jar").

    In the following example, we modify the default execution of the "maven-jar-plugin" (id: "default-jar") to add configuration to the "jar" goal, renaming the generated ".jar" file to "${project.artifactId}-default-${project.version}.jar".

    We also associate an execution to the "package" phase for the "run" goal of the "maven-antrun-plugin" (id: "echo-package-name"): this displays the project coordinates "groupId":"artifactId":"packaging":"version".
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.0.2</version>
    
                <executions>
                    <execution>
                        <id>default-jar</id>
    
                        <configuration>
                            <finalName>${project.artifactId}-default-${project.version}</finalName>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
    
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.8</version>
    
                <executions>
                    <execution>
                        <id>echo-package-name</id>
                        <phase>package</phase>
    
                        <goals>
                            <goal>run</goal>
                        </goals>
    
                        <configuration>
                            <target>
                                <echo>project coordinates: ${project.groupId}:${project.artifactId}:${project.packaging}:${project.version}
                                </echo>
                            </target>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
© 2025  mtitek