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 org.cliassured.maven.Maven;

// Use Maven version 3.9.11 as available in ~/.m2/wrapper/dists/apache-maven-3.9.11/a2d47e15
Maven.version("3.9.11")
    // If ~/.m2/wrapper/dists/apache-maven-3.9.11/a2d47e15 does not exist,
    // download it from
    // https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.11/apache-maven-3.9.11-bin.zip
    // much like Maven Wrapper would do.
    .installIfNeeded()
    // Obtain a CommandSpec with bin/mvn[.cmd] set as executable
    .mvn()
    // Call `mvn[.cmd] --version`
    .args("--version")
    .then()
        .stdout()
            .hasLines("Apache Maven 3.9.11 (3e54c93a704957b63ee3494413a2b544fd3d825b)")
    .execute()
    .assertSuccess();

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 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 use that information to create a new Mvn instance
Maven.fromMvnw()
    .installIfNeeded()
    // Obtain a CommandSpec with bin/mvn[.cmd] set as executable
    .mvn()
    // Call `mvn[.cmd] --version`
    .args("--version")
    .then()
        .stdout()
            .hasLines("Apache Maven 3.9.11 (3e54c93a704957b63ee3494413a2b544fd3d825b)")
    .execute()
    .assertSuccess();