# Gradle

Build and test the projects of Gradle builds alongside the rest of a repository.

The Gradle plugin lets Axle see the projects of the Gradle 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 rather than Kotlin, so it runs with the JDK every Gradle build already has, straight from its source, with no Kotlin compiler and nothing to download first.

## Add it

```sh
axle add gradle
```

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

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

It needs `java` from JDK 22 or newer on the `PATH`, and `gradle` on the `PATH` for the builds that have no Gradle 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

Each `settings.gradle` or `settings.gradle.kts` starts a build. Its root project and every project it names with `include`, in the folder `projectDir` gives it or the one its path names, are projects, named after the last part of their path, and `axle list` shows them with the kind `plugin`. A settings file inside another build's folder, such as `build-logic`'s, is left out, and so are files Git ignores, folders named `build`, `.gradle`, `node_modules` and `.git`, a project at the root of the repository and a folder that is already a package.

A project depends on another project of its build when its `build.gradle` or `build.gradle.kts` names it with `project(":lib")`, `project(path: ":lib")` or a type-safe accessor such as `projects.lib`, so its tasks wait for that project's.

The plugin reads the settings and build files as text rather than running Gradle, which takes seconds. So it doesn't see:

- dependencies that convention plugins in `buildSrc` or `build-logic` add, or that `subprojects` and `allprojects` blocks add;
- dependencies and projects computed in code, such as `include` in a loop;
- builds joined with `includeBuild`.

Add those with `implicitDependencies` in the project's `axle.toml`.

## Versions and the cache

When a project locks its dependencies, the plugin reads the versions from its `gradle.lockfile`, and Axle hashes them like a JavaScript lockfile. A new version misses the cache of the projects that use it and no others. `gradle.lockfile` itself isn't among the files every task reads, and `--affected` counts a change to it against every Gradle project.

Each task reads its project's files, the files of the projects it depends on, and from the build's folder the settings and root build files, `gradle.properties`, the version catalogs in `gradle`, the wrapper's settings, `buildSrc` and `build-logic`. `ORG_GRADLE_PROJECT_*` variables change a task's hash, and tasks also get `JAVA_HOME`, `GRADLE_USER_HOME`, `GRADLE_OPTS` and `JAVA_OPTS`.

Axle caches each task's result and output, not the `build` folder. Gradle already knows which of its outputs are up to date, and keeps its own build cache, so a task Axle runs again is quick. A task from the cache doesn't run Gradle at all.

The plugin's answers are kept until a settings or build file, `gradlew`, `gradle.lockfile`, a version catalog or `.gitignore` changes.

## Tasks

Each project gets two tasks, run with the Gradle wrapper at the top of its build when it has one:

| Task    | Command                      |
| ------- | ---------------------------- |
| `build` | `./gradlew :<path>:assemble` |
| `test`  | `./gradlew :<path>:test`     |

`tasks` in the plugin's settings adds tasks or changes them for every project. A task is the Gradle task and its arguments, which the plugin runs with the project's path in front of the task's name, 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.gradle]
source = "npm:@axle-rs/plugin-gradle@0.1.0"
hash = "sha256-…"
tasks = { check = "check --info", 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). The Gradle daemon already keeps a warm JVM between commands, so one `./gradlew :a:test :b:test` saves little over two, and the plugin could only tell which project failed by reading Gradle's log.
