# Maven

Build and test the modules of Maven builds alongside the rest of a repository.

The Maven plugin lets Axle see the modules of the Maven builds in a repository, so they build and test in the same run as everything else, in dependency order, from the cache when nothing changed. It's written in Java and runs with the JDK every Maven build already has, straight from its source, so nothing is compiled or downloaded first.

## Add it

```sh
axle add maven
```

This writes it into the root `axle.toml`:

```toml
[plugins.maven]
source = "npm:@axle-rs/plugin-maven@0.1.0"
hash = "sha256-…"
```

It needs `java` from JDK 22 or newer on the `PATH`, and `mvn` on the `PATH` for the builds that have no Maven wrapper.

A repository with no JavaScript needs no `package.json`: the `axle.toml` at its root, the one that lists the plugin, is what Axle finds it by.

## What it finds

Every `pom.xml` that no other `pom.xml` lists starts a build, and the plugin follows its `<modules>`, or `<subprojects>` in Maven 4, and each module's `<parent>` through its `relativePath`. Each `pom.xml` it reaches is a project named after its `artifactId`, and `axle list` shows them with the kind `plugin`. A `pom.xml` inside a module's `src` folder, such as a test resource, is left out, and so are files Git ignores, folders named `target`, `build`, `node_modules` and `.git`, a `pom.xml` at the root of the repository and a folder that is already a package.

A module depends on another module the plugin found when it names its `groupId` and `artifactId` under `<dependencies>`, or as its `<parent>`, so its tasks wait for that module's. A `groupId` left out is taken from the parent, and `${project.groupId}` and `${project.parent.groupId}` are filled in. Dependencies that only `<dependencyManagement>` or a profile lists aren't read.

## Versions and the cache

Maven has no lockfile, so the versions a module uses are part of its `pom.xml` and its parents', which its tasks read anyway.

Each task reads its module's files, the files of the modules it depends on, every `pom.xml` from its folder up to the top of its build, and the build's `.mvn` folder, with `maven.config`, `jvm.config`, `extensions.xml` and the wrapper's settings. `MAVEN_ARGS` changes a task's hash, and tasks also get `JAVA_HOME`, `MAVEN_OPTS`, `MAVEN_HOME`, `M2_HOME` and the wrapper's `MVNW_*` variables.

The `build` task stores the module's `target` folder, so a task from the cache puts the jar back without running Maven. Maven decides what to compile again by comparing files' times, and Axle restores them as new, so a build that follows sees them as up to date.

The plugin's answers are kept until a `pom.xml`, `mvnw`, `mvnw.cmd` or `.gitignore` changes.

## Tasks

Each module gets two tasks, run with the Maven wrapper when the top of its build has one:

| Task    | Command                                                         |
| ------- | --------------------------------------------------------------- |
| `build` | `./mvnw -f <top>/pom.xml -pl :<module> -am package -DskipTests` |
| `test`  | `./mvnw -f <top>/pom.xml -pl :<module> -am test`                |

`-am` makes Maven build the modules this one depends on first, from the same build, so it never needs them installed in the local repository. It also means `test` runs the tests of those modules again. To test only the module, after its dependencies are installed with `mvn install`, give `test` its own `command` in `axle.toml`, such as `mvn test`, which runs in the module's folder. `test` waits for the module's own `build`, so the two never write to `target` at once.

`tasks` in the plugin's settings adds tasks or changes them for every module. A task is the goals and arguments the plugin passes to Maven after `-am`, a table with them in `run` and any [task settings](https://axlers.dev/docs/reference/configuration.md#tasks), or `false` to leave it out:

```toml
[plugins.maven]
source = "npm:@axle-rs/plugin-maven@0.1.0"
hash = "sha256-…"
tasks = { verify = "verify -Pci", test = { run = "test", cache = false }, build = false }
```

A project's own `axle.toml` and the root one change them as they would any task, and win over the plugin.

The tasks don't run in [batches](https://axlers.dev/docs/reference/plugins.md#batch-mode). One `mvn -pl :a,:b` would start the JVM once instead of twice, but the plugin could only tell which module failed by reading Maven's log, and Axle would cache whatever it got wrong.
