Maven is a powerful build automation tool used primarily for Java projects, and it’s a key player in any DevOps toolkit for building, testing, and deploying code. Maven simplifies the process of managing dependencies, compiling code, running tests, and generating project reports. Understanding Maven commands can streamline DevOps workflows, automate tasks, and boost productivity.
Below is an overview of the most useful Maven commands for DevOps engineers, including how to use them effectively.
my-app % mvn --help
usage: mvn [options] [<goal(s)>] [<phase(s)>]
Options:
-am,--also-make If project list is specified,
also build projects required by
the list
-amd,--also-make-dependents If project list is specified,
also build projects that depend
on projects on the list
-B,--batch-mode Run in non-interactive (batch)
mode (disables output color)
-b,--builder <arg> The id of the build strategy to
use
-C,--strict-checksums Fail the build if checksums don't
match
-c,--lax-checksums Warn if checksums don't match
--color <arg> Defines the color mode of the
output. Supported are 'auto',
'always', 'never'.
-cpu,--check-plugin-updates Ineffective, only kept for
backward compatibility
-D,--define <arg> Define a user property
-e,--errors Produce execution error messages
-emp,--encrypt-master-password <arg> Encrypt master security password
-ep,--encrypt-password <arg> Encrypt server password
-f,--file <arg> Force the use of an alternate POM
file (or directory with pom.xml)
-fae,--fail-at-end Only fail the build afterwards;
allow all non-impacted builds to
continue
-ff,--fail-fast Stop at first failure in
reactorized builds
-fn,--fail-never NEVER fail the build, regardless
of project result
-gs,--global-settings <arg> Alternate path for the global
settings file
-gt,--global-toolchains <arg> Alternate path for the global
toolchains file
-h,--help Display help information
-itr,--ignore-transitive-repositories If set, Maven will ignore remote
repositories introduced by
transitive dependencies.
-l,--log-file <arg> Log file where all build output
will go (disables output color)
-llr,--legacy-local-repository UNSUPPORTED: Use of this option
will make Maven invocation fail.
-N,--non-recursive Do not recurse into sub-projects
-npr,--no-plugin-registry Ineffective, only kept for
backward compatibility
-npu,--no-plugin-updates Ineffective, only kept for
backward compatibility
-nsu,--no-snapshot-updates Suppress SNAPSHOT updates
-ntp,--no-transfer-progress Do not display transfer progress
when downloading or uploading
-o,--offline Work offline
-P,--activate-profiles <arg> Comma-delimited list of profiles
to activate
-pl,--projects <arg> Comma-delimited list of specified
reactor projects to build instead
of all projects. A project can be
specified by [groupId]:artifactId
or by its relative path
-q,--quiet Quiet output - only show errors
-rf,--resume-from <arg> Resume reactor from specified
project
-s,--settings <arg> Alternate path for the user
settings file
-t,--toolchains <arg> Alternate path for the user
toolchains file
-T,--threads <arg> Thread count, for instance 4
(int) or 2C/2.5C (int/float)
where C is core multiplied
-U,--update-snapshots Forces a check for missing
releases and updated snapshots on
remote repositories
-up,--update-plugins Ineffective, only kept for
backward compatibility
-v,--version Display version information
-V,--show-version Display version information
WITHOUT stopping build
-X,--debug Produce execution debug output
Maven Command Structure
The syntax for running Maven commands is:
mvn [options] [<goal(s)>] [<phase(s)>]
In Maven, goals and phases are the core concepts that drive the build process. Understanding how they relate to each other is crucial for effectively using Maven to manage and automate builds in software development. This blog will explain the difference between goals and phases, how they work together, and how to use them to streamline your builds.
What Are Maven Phases?
Maven operates through a build lifecycle, which consists of multiple phases. Each phase represents a specific stage in the build process. Maven has three main lifecycles, but the default lifecycle is the most commonly used, and it includes several important phases, such as:
- validate: Validates the project is correct and all necessary information is available.
- compile: Compiles the source code of the project.
- test: Runs tests using a suitable testing framework (e.g., JUnit).
- package: Packages the compiled code into a distributable format, such as a JAR or WAR.
- verify: Verifies the results of integration tests to ensure quality.
- install: Installs the package into the local repository, making it available to other projects locally.
- deploy: Copies the final package to the remote repository for sharing with other developers or deploying in production.
Each of these phases represents a specific step in the software development lifecycle, and running a single phase triggers all previous phases as well. For example, if you run the package
phase, Maven will first execute the validate
, compile
, and test
phases before packaging the code.
Example:
To run the package phase, use:
mvn package
This command will trigger the phases in this order:
validate
compile
test
package
What Are Maven Goals?
A goal is a specific task that is bound to a particular phase or can be executed independently. Unlike phases, which represent stages in the build lifecycle, goals represent individual actions, such as compiling code or running tests.
Some commonly used goals are:
- compile: Compiles the project’s source code.
- test: Executes the test cases.
- clean: Cleans up the project by removing files generated during previous builds.
- install: Installs the project into the local repository for reuse in other projects.
Goals can be bound to phases, which means when a particular phase is executed, Maven will execute the goals associated with it. For instance, when you run the compile
phase, Maven will automatically execute the compiler:compile
goal. Each phase is a sequence of goals, and each goal is responsible for a specific task.
We can list all goals bound to a specific phase and their plugins using the command:
mvn help:describe -Dcmd=compile
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------------< com.mycompany.app:my-app >----------------------
[INFO] Building my-app 1.0-SNAPSHOT
[INFO] from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- help:3.5.0:describe (default-cli) @ my-app ---
[INFO] 'compile' is a phase corresponding to this plugin:
org.apache.maven.plugins:maven-compiler-plugin:3.13.0:compile
It is a part of the lifecycle for the POM packaging 'jar'. This lifecycle includes the following phases:
* validate: Not defined
* initialize: Not defined
* generate-sources: Not defined
* process-sources: Not defined
* generate-resources: Not defined
* process-resources: org.apache.maven.plugins:maven-resources-plugin:3.3.1:resources
* compile: org.apache.maven.plugins:maven-compiler-plugin:3.13.0:compile
* process-classes: Not defined
* generate-test-sources: Not defined
* process-test-sources: Not defined
* generate-test-resources: Not defined
* process-test-resources: org.apache.maven.plugins:maven-resources-plugin:3.3.1:testResources
* test-compile: org.apache.maven.plugins:maven-compiler-plugin:3.13.0:testCompile
* process-test-classes: Not defined
* test: org.apache.maven.plugins:maven-surefire-plugin:3.2.5:test
* prepare-package: Not defined
* package: org.apache.maven.plugins:maven-jar-plugin:3.4.1:jar
* pre-integration-test: Not defined
* integration-test: Not defined
* post-integration-test: Not defined
* verify: Not defined
* install: org.apache.maven.plugins:maven-install-plugin:3.1.2:install
* deploy: org.apache.maven.plugins:maven-deploy-plugin:3.1.2:deploy
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.386 s
[INFO] Finished at: 2024-09-27T17:18:24+05:30
[INFO] ------------------------------------------------------------------------
Customizing Goals and Phases
A Maven plugin is a group of goals; however, these goals aren’t necessarily all bound to the same phase. Maven allows you to customize how goals and phases work together by configuring them in your pom.xml
file. You can bind additional goals to a phase or even skip default goals for certain phases.
Example:
Here’s how you can bind a custom goal to a phase in the pom.xml
:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>custom-clean</id>
<phase>clean</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
In this case, the maven-clean-plugin
is bound to the clean
phase.
List all goals in a specific plugin:
mvn <PLUGIN>:help
mvn jar:help
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------------< com.mycompany.app:my-app >----------------------
[INFO] Building my-app 1.0-SNAPSHOT
[INFO] from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- jar:3.4.2:help (default-cli) @ my-app ---
[INFO] Apache Maven JAR Plugin 3.4.2
Builds a Java Archive (JAR) file from the compiled project classes and
resources.
This plugin has 3 goals:
jar:help
Display help information on maven-jar-plugin.
Call mvn jar:help -Ddetail=true -Dgoal=<goal-name> to display parameter
details.
jar:jar
Build a JAR from the current project.
jar:test-jar
Build a JAR of the test classes for the current project.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.607 s
[INFO] Finished at: 2024-09-27T17:28:35+05:30
[INFO] ------------------------------------------------------------------------
Acknowledgments: