Install and invoke Maven

0.0.1

The utility methods for installing and invoking Maven are available in cli-assured-maven artifact:

<dependency>
  <groupId>org.cli-assured</groupId>
  <artifactId>cli-assured-maven</artifactId>
  <version><!-- Use the latest --></version>
</dependency>

Find the latest version on Maven Central.

There are two ways how to specify the Maven version to install and run:

  • By version literal

  • By querying .mvn/wrapper/maven-wrapper.properties

Install and run a specific version of Maven

0.0.1

import java.util.regex.Pattern;
import org.cliassured.maven.InstalledMaven;
import org.cliassured.maven.Maven;

final String version = "3.9.16";
Maven.version(version)
    // Reuse the installation under ~/.m2/wrapper/dists,
    // or download the requested version from Maven Central.
    .installIfNeeded()
    // Obtain a CommandSpec with bin/mvn[.cmd] set as executable
    .mvn()
    // Call `mvn[.cmd] --version`
    .args("--version")
    .then()
        .stdout()
            .hasLinesMatching("Apache Maven\\s+" + Pattern.quote(version) + "(?:\\s|$)")
    .execute()
    .assertSuccess();

Custom Maven download repository

0.2.2

When installing Maven with Maven.version(String) or Maven.fromMvnw(), CLI Assured computes the default distribution URL using the first available repository URL from the following list:

  1. The mvnw.repo.url system property.

  2. The MVNW_REPOURL environment variable.

  3. https://repo.maven.apache.org/maven2 (Maven Central).

Set the system property in the configuration of Surefire or Failsafe Maven plugins, or set MVNW_REPOURL in the environment before starting the Java process:

export MVNW_REPOURL=https://repo.example.org/maven2

MVNW_REPOURL is also honored by mvnw and mvnw.cmd scripts since Maven Wrapper version 0.5.3 (released on 2019-02-25).

CLI Assured constructs the download URL using this template:

<repository-url>/org/apache/maven/apache-maven/<version>/apache-maven-<version>-bin.zip

The repository must also provide the corresponding .zip.sha512 file, which CLI Assured uses to verify the download.

An explicit MavenSpec.distributionUrl(String) still takes precedence over these repository settings. Maven.fromMvnw() uses the distributionUrl from .mvn/wrapper/maven-wrapper.properties as configured there.

Authentication when downloading Maven distribution

0.2.3

For repositories requiring HTTP Basic authentication, configure the following credentials:

Credential System property Environment variable

Username

mvnw.repo.username

MVNW_USERNAME

Password

mvnw.repo.password

MVNW_PASSWORD

Each system property takes precedence over its corresponding environment variable. Both resolved credentials must be nonempty for authentication to be used. The credentials apply to downloads of both the Maven ZIP distribution and its .zip.sha512 checksum, including explicitly configured distribution URLs and URLs read from Maven Wrapper configuration.

Set the system properties in the configuration of Surefire or Failsafe Maven plugins, or export the environment variables before starting the Java process:

export MVNW_USERNAME=my-user
export MVNW_PASSWORD=my-password

MVNW_USERNAME and MVNW_PASSWORD are also supported by the mvnw and mvnw.cmd shell scripts. The mvnw.repo.username and mvnw.repo.password system properties are specific to CLI Assured.

Install and run the Maven version configured for Maven Wrapper

0.0.1

In case you are about to invoke Maven inside a project having Maven Wrapper configured, you can let CLI Assured pick its version of Maven and also reuse the Maven installation available in ~/.m2/wrapper/dists.

import java.util.regex.Pattern;
import org.cliassured.maven.InstalledMaven;
import org.cliassured.maven.Maven;

// Find .mvn/wrapper/maven-wrapper.properties
// under the nearest ancestor of the current directory,
// extract the distribution URL from there
// find Maven version from the distribution URL
// and install that version if needed.
InstalledMaven maven = Maven.fromMvnw().installIfNeeded();
maven
    // Obtain a CommandSpec with bin/mvn[.cmd] set as executable
    .mvn()
    // Call `mvn[.cmd] --version`
    .args("--version")
    .then()
        .stdout()
            .hasLinesMatching("Apache Maven\\s+" + Pattern.quote(maven.version()) + "(?:\\s|$)")
    .execute()
    .assertSuccess();